Cocoa: IOKit: Freeze when trying to send a message

Hello,

Im still not able to send messages through Cocoa to my Arduino Uno - Arduino code:

void setup()
{
  pinMode (2, OUTPUT);
  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);
  
  Serial.begin (9600);
}

void loop ()
{
  digitalWrite (2, HIGH);
  
  if (Serial.available () > 0)
  {
    int c = Serial.read();
    
    if (c == 255)
    {
      digitalWrite (3, HIGH);
    }
    else
      digitalWrite (4, HIGH);
  }
}

ObjC code:

// open the serial like POSIX C
    serialFileDescriptor = open(
                                "/dev/tty.usbmodemfa131",
                                O_RDWR |
                                O_NOCTTY |
                                O_NONBLOCK );
    
    struct termios options;
    
    // block non-root users from using this port
    ioctl(serialFileDescriptor, TIOCEXCL);
    
    // clear the O_NONBLOCK flag, so that read() will
    //   block and wait for data.
    fcntl(serialFileDescriptor, F_SETFL, 0);
    
    // grab the options for the serial port
    tcgetattr(serialFileDescriptor, &options);
    
    // setting raw-mode allows the use of tcsetattr() and ioctl()
    cfmakeraw(&options);
    
    speed_t baudRate = 9600;
    
    // specify any arbitrary baud rate
    ioctl(serialFileDescriptor, IOSSIOSPEED, &baudRate);
    
    NSLog (@"before");
    sleep (5); // Wait for the arduino to restart
    NSLog (@"after");
    
    int val = 255;
    write(serialFileDescriptor, val, 1);
    NSLog (@"after2");

My output in the console is:

before
after

But then nothing happens...

Well you might want to step through that last part in the XCode debugger to see where it leaves the tracks.

This looks suspicious:

int val = 255;
    write(serialFileDescriptor, val, 1);
    NSLog (@"after2");

Shouldn't there be a & in front of val in the write command?

    write(serialFileDescriptor, &val, 1);

-br

Hey,

i've added it but still nothing happens and the arduino freezes.

So this is what happens when i run the code:

Arduino gets restarted
OUTPUT: before
Sleeping 5 seconds
OUTPUT: after
Freeze at the line where "write" is

When i comment this code, the arduino doesnt freeze but i still dont receive a message:

fcntl(serialFileDescriptor, F_SETFL, 0);