3

I just finished a small project written in C, where I read a data stream from a serial port and parse the incoming data. The software is written for POSIX systems (using termios) and follows the standard steps to working with serial i/o

  1. Opening the serial device using open()
  2. Configuring communication parameters (termios)
  3. Set blocking mode on file handle (fcntl)
  4. Perform read() on serial interface.
  5. Perform close() on serial interface when done.

Other than the socket parts, the code is straight ANSI C.

My question is, how involved would it be to make the code work on a windows platform. The port would not be written by me, I'd only like to give an indication to others who might be interested in porting it (i.e. trivial, not so trivial, rip your eyes out insanity inducing).

Also if someone has Windows with "Windows Services for UNIX", would they be able to use the code without modifying it?

So, if anyone has experience with this could you please share.

bing
  • 443
  • 6
  • 11

1 Answers1

3

It should be pretty easy to do. The names are very different, but the sequence of calls and concepts are very similar.

What you are looking for is the DCB structure which should be used with the SetComState() function to set baudrate, stopbits etc. Then use SetCommTimeouts() and set the timeout values in the COMMTIMEOUTS structure to make subsequent read calls blocking.

Here is a short introduction as a pretty PDF. (Backup.)

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
  • 1
    Thanks, the DCB struct pretty much maps directly to a termios struct. Since it is normal in *nix systems for everything to be a represented as a file I was worried that it would be a problem in windows. But you can declare a file handle in windows too and use it in pretty much the same way, so I'm happy. Thanks – bing Feb 08 '12 at 13:49