[Haskell-cafe] Serial Communications in Haskell
Bas van Dijk
v.dijk.bas at gmail.com
Wed Aug 29 08:28:41 EDT 2007
On 8/29/07, Esa Ilari Vuokko <eivuokko at gmail.com> wrote:
> On 8/29/07, Ronald Guida <ronguida at mindspring.com> wrote:
> > 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
> At the moment, I think it is easier to just use the same Windows API directly,
> rather than try to bend GHC's IO system to do it "right".
> > 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.
> Unfortunately, it goes via extra hoops and uses c runtime instead of
> Windows API directly. This extra layer makes sure it's hard to predict
> exactly what gets done (and with what options) sometimes.
> And even if it did, it'd probably use overlapping io, does that work with
> COM 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
> > proceed.
> I haven't done much work on COM ports, but the little I did, I used bindings
> in Win32 and probably some of my own, and didn't use System.IO/Handle
> at all. Maybe problems with GHC's IO system are easy to solve, but I have
> no idea and wasn't inclined to find out.
> Best regards,
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
Ideally, we would have something like pyserial (
http://pyserial.sourceforge.net ) for Haskell. It provides a nice
portable abstraction over serial communication. See for example the
Bas van Dijk
More information about the Haskell-Cafe