[racket] Beginner: advice COM port I/O

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Fri Dec 30 19:24:04 EST 2011

There are a number of things that could be going wrong, due to RS232 and 
terminal device subtleties, and the OS abstractions.

Assuming that your bit rate, data bits, parity, and stop bit are 
compatible, and you're using the right cable (null-modem or straight 
through, and enough of the pins wired through rather than unconnected 
like in some cables), and you're not confused by something like flow 
control or full/half duplex (and I'm assuming it's ASCII)...

The very first thing I'd do from the Racket side is to send exact bytes 
rather than strings, just because bytes are the right way to do it.

Then I'd be sure that I was sending whatever newline convention the 
device requires: after the #"V00" bytes, does it require a CR byte 
and/or an LF byte?

The very next thing I'd look at on the Racket side is that I'd quickly 
peek at the implementation of "open-input-output-file".  (From your 
description, it sounds like it is correctly not buffering the output, 
and that EOF doesn't look like it can be right if it's doing a blocking 
read with the semantics I'd expect, but I'd just make sure I knew the 
implementation, since the documentation makes this look easier than I'd 
expect it to be.)

If it's not working by this point, I'd pick one of the open source 
libraries that does work with it, and see what they're doing 
differently.  Maybe try to find one that's not using Win32/MFC/etc. 
calls, but works using standard C and POSIX calls.  RS232 is a pain in 
the butt, so I think this is a good time to first do cargo cult 
engineering -- go through the magical incantations that seemed to work 
once before -- and then after it seems to work you can figure out the 
*why* so that you can have as robust of a solution as you need.

If all else fails, you can use the Racket FFI to make example Win calls 
you find in MSDN or on some Web page, but good to try the elegant 
"open-input-output-file" first.


Posted on the users mailing list.