Several questions on Arduino  + Mac OS X

Hi, I managed to make a Diecimila talk to my app on Mc OS X. I have the Arduino code send the read out of each analog pin wrapped as a MIDI message. That's all I need to do: read bytes from the Arduino. But I have three major problems:

  1. I have a timer on my Mac code (Carbon) which reads the serial port every 20 milisecs. Each time it fires, the amount of bytes in the buffer may be different, and not necessarily aligned with the message size (3 bytes), so that sometimes, a message gets split between one read and the next, making it hard to synchronize with the Arduino sends. Signaling the end of the message is not really useful, beacuse the data I want is already wrapped as a MIDI message, and any bytes I put in between messages might still be left behind. How do people deal with this?

  2. When I am ready to stop reading the Arduino data I block the file descriptor with tcdrain() and then close it. However, this doesn't seem to close the device, because after that I cannot reopen it or do anythong with it, unless I reboot the Mac (!), not without first force-shutting-it-down (!!!).

  3. After searching for days and reading lots of info on serial programming for BSD and Mac OS, I still can't figure out which device file I should use: cu.usbserial... or tty.usbserial... . What are the differences between them? So far I could only talk to the cu... file. Could the busy-device problem described above be relaed to this choice? How?

Thanks for reading all this.Any help would be really great.

  1. After searching for days and reading lots of info on serial programming for BSD and Mac OS, I still can't figure out which device file I should use: cu.usbserial... or tty.usbserial... . What are the differences between them?

Searching the net for "cu vs tty" may help or leave you more confused (it was apparently originally something about the difference between using the device for incoming and outgoing connections). My understanding was that tty* is the one to use but I could be misinformed.

--Phil.

sounds like you need to re-buffer the data so you can consume it on your chosen 3 byte boundary.

never heard of tcdrain() before reading this post. I simply close the fd when I'm through reading.

Don't know all the differences between /dev/cu.* and /dev/tty., but the other poster is correct, the /dev/cu. device was intended for use with cu, a program used to "call up" (i.e. dial out) another computer. With the FTDI device, we've got a well written driver and they both seem to work fine (note: never try to use both at the same time; bad things may happen). Other usb/rs232 devices have one that will work well, but the other doesn't. I use /dev/tty.* from long habit.

-j

Well, every info and example I found suggests using /dev/tty... but I can never open it. I get an invalid device when I try to open that file. I managed to open the cu. device and except for this "hanging" thing at the end, it transfered the data ok.

You don't have the Arduino IDE's serial monitor open at the same time, do you?

sorry, have to ask.

-j

No, I always double-check that, to make sure. AFAIK serial devices can not be used by more than one process at the same time...

This leads me to one more question. The Arduino IDE seems to handle both devices without any problems (one at a time that is) . Maybe I could look in it's source code to see what it is I am doing wrong...Is it open?