Tuesday, March 20, 2007

serial ports and Mac os X

It is pretty straightforward to add a serial port to your Mac using a serial to RS232 converter. I bought a USBG-232 converter from usbgear, that has a foot cable, and lights so you can check the connection at the hardware level.

Don't bother with the drivers on their website, which are outdated. Go to FTDI, the company that makes the chip inside. They have up-to-date drivers even for intel Macs.

To talk to your serial device, you could get goSerial, but I wanted to do some on-the-fly analysis using python (of course). For this, I use pyserial. You just have to make sure that you link to the serial device correctly, as the FTDI drivers call it something nonstandard.

For example, here is a little script that uses pyserial to echo serial mouse output:

import serial

ser = serial.Serial('/dev/cu.usbserial-A5001uBe', 1200, \
bytesize=7, parity='N', stopbits=1)

while 1:
c1 = ser.read()
c2 = ser.read()
c3 = ser.read()
print oct(ord(c1)), oct(ord(c2)), oct(ord(c3))

No comments:

Post a Comment