Writing Serial Port Data to a Text File

Need to write incoming bytes from a serial port to a text file? On a Unix or Linux machine (or OSX), you can do the following:

Open the Terminal Program (usually under /Applications/Utilities/Terminal)

Get the name of the serial port by typing ls /dev/tty.* You should get a list like this:

/dev/tty.Bluetooth-Modem                /dev/tty.Keyserial1
/dev/tty.Bluetooth-PDA-Sync             /dev/tty.usbserial-A10015y4

Make sure you know what directory you’re in. You might want to change directories to the desktop like this: cd Desktop

To start sending serial data into a file called datafile.txt, type screen -L (serial port name). For example, if you’re using the serial port /dev/tty.usbserial-A10015y4 above, you’d type: screen -L /dev/tty.usbserial-A10015y4 Anything coming in the serial port will automatically get written toa file called screen.0, in the current working directory. To stop sending, type control-C.

A few suggestions to make this go smoother:

Send your data in ASCII rather than raw binary format. That way you can read it yourself, in addition to having a program parse it.

Consider how much data you’re writing to a file. Sending every 10 milliseconds or less will fill the file very quickly. Your maximum file size under OSX is 2 gigabytes. That’s 2 billion bytes. If you’re sending three ten-bit sensor readings, comma-delimited, with a newline and carriage return at the end:

1023,1023,1023\r\n

That’s 16 bytes. If you’re sending that every 10 milliseconds, that’s 1600 bytes a second. That’ll take about 173.6 hours to fill 2GB. That’s about a week.

Inspired by Adams: Simon and Parrish. Thanks to Kate Hartman and Rob Faludi for the updates.