[racket] How do I set baud/parity/stop for a serial port?

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Mon Aug 22 00:36:41 EDT 2011

Steven Zins wrote at 08/21/2011 11:53 PM:
> How do I set baud/parity/stop in Racket for a serial port in 
> Windows/Linux/Mac. Eg for "\\.\COM4"

I bet you're going to need to make some Windows system calls using the 
Racket FFI.  I don't know what the best documentation is for the Windows 
calls, but Google found these:

Hopefully you can use Racket ports.  If you can, you'll probably want to 
make sure buffering is disabled ("file-stream-buffer-mode").  Then 
you'll probably either want to use "sync" or separate threads to avoid 
blocking your entire program on port I/O.

If you happen to be new to RS232, and talking with your device doesn't 
work the first time, and reliably, you're in for a treat, because this 
stuff is ancient.  (Ancient, like, one printer I owned, the user manual 
told you to open up the printer housing and cut a trace on the circuit 
board, depending on whether you wanted RS232 or current loop, apparently 
in a time when what would be an ordinary thing for a user to do.)  If 
you don't know exactly what parameters the device you're talking with 
wants, I think a fairly conservative first-guess is 9600/8/N/1.  If that 
doesn't work, I'd try 7/E/1.  Then, going back and adjusting the baud 
(look up online what typical baud rates are).  If doing terminal 
communications, characters sent by the terminal may or may not get 
echoed back (look up "duplex").  You might also have to deal with Ctrl-S 
/ Ctrl-Q flow-control (perhaps only not sending it, or perhaps using 
and/or handling it), and with NULs.  Hopefully you don't have to deal 
directly with the UART like we did in the '80s, but you *could* do that 
from Racket with the FFI if you had to.  (I'm not quite that old; 
anything else I knew about RS232 was forgotten once I hit puberty.)  If 
you happen to actually be using USB, and this old COM-port stuff (RS232) 
doesn't work completely for your device, then you might need to make 
USB-specific calls.  That, and your towel, will prepare you for any 


Posted on the users mailing list.