Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically talking to a Serial Port in OS X or Linux

I have a Prolite LED sign that I like to set up to show scrolling search queries from a apache logs and other fun statistics. The problem is, my G5 does not have a serial port, so I have to use a usb to serial dongle. It shows up as /dev/cu.usbserial and /dev/tty.usbserial .

When i do this everything seems to be hunky-dory:

stty -f /dev/cu.usbserial
speed 9600 baud;
lflags: -icanon -isig -iexten -echo
iflags: -icrnl -ixon -ixany -imaxbel -brkint
oflags: -opost -onlcr -oxtabs
cflags: cs8 -parenb

Everything also works when I use the serial port tool to talk to it.

If I run this piece of code while the above mentioned serial port tool, everthing also works. But as soon as I disconnect the tool the connection gets lost.

#!/usr/bin/python

import serial

ser = serial.Serial('/dev/cu.usbserial', 9600, timeout=10) 
ser.write("<ID01><PA> \r\n") 
read_chars = ser.read(20)
print read_chars

ser.close()

So the question is, what magicks do I need to perform to start talking to the serial port without the serial port tool? Is that a permissions problem? Also, what's the difference between /dev/cu.usbserial and /dev/tty.usbserial?


Nope, no serial numbers. The thing is, the problem persists even with sudo-running the python script, and the only thing that makes it go through if I open the connection in the gui tool that I mentioned.

like image 200
deadprogrammer Avatar asked Aug 06 '08 21:08

deadprogrammer


People also ask

How can I tell if a serial port is being used Linux?

Use the setserial command to check and use serial ports. The setserial is a program designed to set and/or report the configuration information associated with a serial port.

How to send data through serial port?

When the computer wants to send a byte out the serial port (to the external cable) the CPU sends the byte on the bus inside the computer to the I/O address of the serial port. The serial port takes the byte, and sends it out one bit at a time (a serial bit-stream) on the transmit pin of the serial cable connector.

How do I open a Windows serial terminal?

The fastest way to get to the command line in Windows is to click on the start menu, type cmd into the search field, and press Enter. This will open up a blank MS-DOS command line prompt. To be able to issue Serial commands, you must first enter PowerShell. Type powershell to get into PowerShell command mode.


1 Answers

/dev/cu.xxxxx is the "callout" device, it's what you use when you establish a connection to the serial device and start talking to it. /dev/tty.xxxxx is the "dialin" device, used for monitoring a port for incoming calls for e.g. a fax listener.

like image 71
Lily Ballard Avatar answered Oct 14 '22 09:10

Lily Ballard