[Haskell] COM port IO on a Windows machine?

Peter Pudney Peter.Pudney at unisa.edu.au
Fri Mar 19 11:33:20 EST 2004


Axel,

Thanks for your reply. But I am still having problems.

I have written a short script to test the serial IO:

>  module SerialIO where

>  import IO

>  test :: IO ()
>  test
>    = do
>        com2 <- openFile "COM2" ReadWriteMode
>        hSetBuffering com2 LineBuffering
>        isOpen <- hIsOpen com2
>        putStrLn $ "hIsOpen = " ++ show isOpen
>        putStrLn "About to send..."
>        hPutStrLn com2 "l311"
>        putStrLn "Hoorah!"
>        hClose com2


Here is what I get when I run the test function using GHCi 5.04.2:

*SerialIO> test
hIsOpen = True
About to send...
*** Exception: failed
Action: hPutChar
Handle: {loc=COM2,type=duplex {read-write},binary=False,buffering=line}
Reason: No error
File: COM2


It appears to be connecting to COM2, but fails when it tries to output a
character. The failure leaves the COM2 port connected, so I have to quit GHCi
before I can use COM2 from HyperTerminal.

I have also tried opening the file in WriteMode, and opening the port as
"COM2:19200,8,N,1,P", but get the same failure each time?

Do you have any other suggestions?


Peter






-----Original Message-----
From: Axel Simon [mailto:A.Simon at kent.ac.uk]
Sent: Thursday, 18 March 2004 4:54 AM
To: Peter Pudney
Cc: 'haskell at haskell.org'
Subject: Re: [Haskell] COM port IO on a Windows machine?


On Wed, Mar 17, 2004 at 05:46:25PM +1030, Peter Pudney wrote:
> I have a microcontroller connected to the COM2 port on a Windows machine, and
> want my Haskell program to interact with it. My Haskell program is working
> nicely on my Mac, with interaction using stdin and stdout, but I am not
> familiar with Windows and a quick Google search did not give me any
information
> on how to get Haskell IO happening through a Windows serial port.
> 
> * How should I open the COM2 port? IO Mode? Buffering?
> 
> * How do I write and read?
> 
> 
> I presume I can set the port parameters fusing something like:
> 
>    ...; e <- system "MODE COM2 19200,*,N,1,P"; ...

You can also set these default parameters somewhere in the Control Panel. In 
case the openFile function is implemented under Windows by calling 
"CreateFile" in the Windows API, then you can just call openFile with the
name "COM2:19200,8,N,1,P" or simliar and use standard read and write 
functions on files.

Maybe this helps,
Axel


More information about the Haskell mailing list