I'm currently trying to send data to Arduino through a Mac application. The code in my Arduino Uno is as follows:
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);
}
}
Here is my code in the Xcode project:
serialFileDescriptor = open(
"/dev/tty.usbmodemfa131",
O_RDWR |
O_NOCTTY |
O_NONBLOCK );
struct termios options;
ioctl(serialFileDescriptor, TIOCEXCL);
fcntl(serialFileDescriptor, F_SETFL, 0);
tcgetattr(serialFileDescriptor, &options);
cfmakeraw(&options);
speed_t baudRate = 9600;
ioctl(serialFileDescriptor, IOSSIOSPEED, &baudRate);
NSLog (@"before");
sleep (5);
NSLog (@"after");
int val = 255;
write(serialFileDescriptor, val, 1);
NSLog (@"after2");
So when I run the application, it waits five seconds, but then it freezes. Console output:
before
after
So what am I doing wrong here?
UPDATE: So, when I comment on this line
fcntl(serialFileDescriptor, F_SETFL, 0);
the program does not freeze, but my Arduino still does not receive any data.