Cross platform (Linux, Windows and Mac OS) serial port interface.
import System.IO
import System.Hardware.Serialport
import qualified Data.ByteString.Char8 as B
let port = "COM3" -- Windows
let port = "/dev/ttyUSB0" -- Linux
withSerial port defaultSerialSettings $ \s -> do
send s $ B.pack "AT\r"
recv s 10 >>= print
Concurrently read and write a serial port at 19200 baud using hWithSerial
:
import Control.Concurrent.Async ( concurrently_ )
import Control.Monad ( forever )
import System.Hardware.Serialport
import System.IO
com :: String -> IO ()
com portPath = hWithSerial portPath serialPortSettings $ \hndl -> do
hSetBuffering stdin NoBuffering
hSetBuffering stdout NoBuffering
concurrently_ (readUart hndl) (writeUart hndl)
where
readUart hndl = forever $ putChar =<< hGetChar hndl
writeUart hndl = forever $ hPutChar hndl =<< getChar
serialPortSettings :: SerialPortSettings
serialPortSettings = defaultSerialSettings{ commSpeed = CS19200 }
- Arduino Leonardo + Sparkfun FTDI breakout board.
- Connections: TX, RX and GND
- Upload arduino code using Arduino IDE or avrdude
- Configure cabal to build the tests:
cabal configure --enable-tests
. - Build:
cabal build
- Run the tests:
cabal test --test-options="/dev/ttyACM0 /dev/ttyUSB0"