"Operation not permitted" error trying to open serial port

When I try to open a serial port (Arduino attached via USB) from a Cocoa App using plain C code in objective C function:


    int fd;
    fd = open("/dev/cu.usbmodem1421", O_RDWR | O_NOCTTY | O_NDELAY );
    int errornumber = errno;

     if( fd == -1 ){
        printf("Nope:%i\n", errornumber);
     }else{
          printf("Yip!");
     }


i get the error code 1, Operation Not Permitted. But, when I try exactly the same thing from a Command Line Tool project, with Objective-C selected as the language, and run it, it works fine.


Any ideas why it does not work in a Cocoa App would be greatly appreciated. Cocoa Objective-C code is started by being linked up to a button, printf output shows that the code is running when the button is pressed, and that open attempt is made, and the error no is 1.


When run in a command line too, I get the Yip! output.


Thanks in advance,

JR

Accepted Answer

SOLVED - I turned off the App Sandbox when running the app. This allowed me to open the file.

But the question is : do you need to sandbox the app or not ?


If yes, you have to ask user permission for accessing the resource.

Sandboxed apps can interact with the serial port by way of the

com.apple.security.device.serial
entitlement. See Table 4-7 of the Entitlement Key Reference for details.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
"Operation not permitted" error trying to open serial port
 
 
Q