jeudi 30 janvier 2020

Xcode C++ code in cocoa app about serial port get operation not permitted

I build a code in Xcode console with C++ project works perfectly before:

https://i.stack.imgur.com/LNVpV.jpg

//* Open port, and connect to a device
const char devicePathStr[] = "/dev/tty.usbserial-A104RXG4";
const int baudRate = 9600;
int sfd = openAndConfigureSerialPort(devicePathStr, baudRate);
if (sfd < 0) {
    if (sfd == -1) {
        printf("Unable to connect to serial port.\n");
    }
    else { //sfd == -2
        printf("Error setting serial port attributes.\n");
    }
    return 0;
}

// * Read using readSerialData(char* bytes, size_t length)

// * Write using writeSerialData(const char* bytes, size_t length)

// * Remember to flush potentially buffered data when necessary

// * Close serial port when done
const char dataToWrite[]="abcd";
char databuffer[1024];

while(1){
    readSerialData(databuffer, 4);
    sleep(2);
    writeSerialData(databuffer, 4);
    sleep(2);

}

After this build, I tried to migrate it to my Xcode cocoa application with C++ wrappers below.

https://i.stack.imgur.com/NNOis.png

I am pretty sure my Wrapper works fine with test C++ code. That means, I can call C++ function from my ViewController.swift.

But there's one strange thing happened. I am not able to open connection with the following code.

sfd = open(portPath, (O_RDWR | O_NOCTTY | O_NDELAY));
if (sfd == -1) {
    printf("Unable to open serial port: %s at baud rate: %d\n", portPath, baudRate);
    printf("%s", std::strerror(errno));
    return sfd;
}

There error message returns :

Unable to open serial port: /dev/tty.usbserial-A104RXG4 at baud rate: 9600

Operation not permitted

I've tried change me app sandbox configuration, set up my system preference to grant access to my app, also I disable my rootless. (csrutil disable with command + R) But the problem still persists. https://i.stack.imgur.com/rfD0e.png

https://i.stack.imgur.com/hwA6r.png

I wanna ask that

  1. Why my code on Xcode C++ project works fine but fail on swift's cocoa app on Xcode?

  2. How to solve the "Operation not permitted" Issue.

My Xcode version is 11.3.1 and Mac OS is 10.14.6 Mojave.

Aucun commentaire:

Enregistrer un commentaire