I am developing a C program using POSIX (so it also runs in Linux) to open the serial port and write data to it. I am having a heck of a problem trying to figure out what exactly is going on. My OSX version is is 10.9, Arduino version is 1.0.5 and my Arduino hardware is a Mega 2560 R3.
If I open the Arduino serial monitor I can send/recieve bytes and my Arduino does what its supposed to do. Awesome! So I exit the Arduino IDE and switch to my little C project in xcode. If I then run my code in xcode, I get the following error in the terminal and the program hangs:
Trying to open: /dev/tty.usbmodemfa131
Port open - : Undefined error: 0
This happens no matter what I try to do! Reboot, unplug the arduino, etc. Even screen cant open the port for writing.
Frustraited, tired and about to give up, I open the Arduino IDE to try one more thing in the serial monitor. I forgot that I left the xcode project running in debug, stuck on its error. Suddenly my arduino project came to life and the C code began to work just as it should. I check the xcode terminal and my program exited normally. So I exit the Arduino IDE and try to run my xcode project again. No dice, it hangs on the error code. But again, as soon as I open the Arduino serial monitor the code works.
So here I am scratching my head trying to figure out why the Arduino IDE's serial monitor allows the serial port to be opened. I was under the impression that once a resource is opened by open() its locked. I am new to OSX so my only explanation is that the Arduino IDE uses a different driver or possibly I/O kit (which I am not familiar with).