views:

3043

answers:

4

I wonder if and how it's possible to control the RS-232 handshaking lines directly from win32 (the old C-API).

I'd like to interface with an external piece of hardware, and two simple data-lines would be sufficient for my needs.

So - is there a API for win32 that lets me read and write the state of the four status lines? In normal serial communication the handshaking lines are driven by the UART automatically (if hardware handshaking is enabled).

I remember that it was trivial under DOS. One just had to program the UART directly. Has this functionality survived in win32 somehow?

A: 

You can still do similar types of programming only to get to protected hardware you will need to implement a device driver. I assume that this has gotten easier since the 1980s when I was doing the same type of work.

Richard
+3  A: 

You can control RTS and DTR using SetCommState(). You can also set things up to have hardware or the driver handle hardware flowcontrol (CTS and/or DSR), or you can set things up using SetCommMask() so you get events when those signals change.

A decent overview is here: http://msdn.microsoft.com/en-us/library/ms810467.aspx

Note that the Win32 Serial Comm API and/or driver can be finicky, so be prepared to have to do some debugging of what's going on on the wire.

Michael Burr
exactly what I was looking for. thanks.
Nils Pipenbrinck
A: 

Is microsoft actually doing hardware handshaking now? For many years NT, win2000 and XP did not do handshaking in the hardware. Instead when the fifo reached a certain point the device driver would manually change the cts line. Which means it was incredibly simple to cause the device driver to lose data, grab a window with the mouse and do circles around the screen for example (making sure you take that window off the left side of the screen on all or some of the passes). Alt-enter to take a command prompt to/from full screen was an easy way to cause a data loss. Or anything else that causes enough of an interrupt latency. Basically microsofts hardware flow control is not hardware but software flow control, even if the hardware does have hardware flow control capabilities microsft's drivers were not setting that bit. SeaLevel eventually did support that bit, well sorta, you had to put the right unrelated settings in SetCommState() to enable it.

As far as your program controlling the signals, use SetCommState().

dwelch
It's been a while since I worked on this stuff, but my memory is that the 8250/16550 UARTs that are standard issue in the PC world do not handle hardware handshaking on-chip. This is not an issue of the Win driver - it can only support what the hardware has. The driver has to get status during device interrupt and toggle/sense the handshaking lines itself. SeaLevel or Hayes had (or have) more intelligent hardware and their drivers would take advantage of that. Also, it's certainly true that early Windows drivers were really, really bad, but they eventually got to be more or less good enough.
Michael Burr
+1  A: 

I ran across this tutorial when I had to do a project to communicate with the RS232 port. It's a complete example on how to open the port, set some properties including the timeouts, read/write and close the port. Even though your project is probably over by now, I hope this will remain useful as it remains in the SO archives.

Mike