[Haskell-cafe] Serial Communications in Haskell

Ronald Guida ronguida at mindspring.com
Tue Aug 28 22:19:37 EDT 2007

I'm on a Windows box and I'm looking for a way to talk to a serial
port (for example, RS-232) from Haskell.  I couldn't find a library to
do this, so I am wondering how to create one.

I have a fairly thorough understanding of how to open and use a serial
port with the Windows API.  In particular, to open a serial port, I
have to use CreateFile, which is the same API call that opens files.
In fact, if I call openFile from GHC, and pass "COM1:" as the
filename, then I can get a writable serial port.

 > module Serial
 >     where
 > import System.IO
 > main = do
 >   h <- openFile "COM1:" ReadWriteMode
 >   hPutStrLn h "Hello World"

I can't read from the port (I always get an immediate EOF), and I have
no way of configuring things like the baud rate or the parity
settings.  Nevertheless, this demonstrates that openFile can at least
open the serial port.

What I would like to do is create some functions that would allow me
to open and configure a serial port, and get a Handle back so that I
can use the existing IO functions like hGetChar and hPutChar.  I am
assuming that hGetChar eventually calls win32::ReadFile and hPutChar
eventually calls win32::WriteFile.  These same two API calls would
work for serial ports.

In Windows, there are 23 API functions that apply specifically to
serial ports.  Out of these 23 functions, only a few of them are
actually necessary if I just want to send and receive data.

Of course, I don't know how to call Windows API functions from Haskell,
and I have no idea how to hook things to the IO library so that I can
use a Handle for a serial port.  I'm looking for some advice on how to

-- Ron

More information about the Haskell-Cafe mailing list