standardsemiconductor / serialport

Cross platform Haskell library for using the serial port
https://hackage.haskell.org/package/serialport
Other
4 stars 8 forks source link
haskell serial-communication

Serialport

Haskell CI Hackage Hackage Dependencies

Cross platform (Linux, Windows and Mac OS) serial port interface.

Sample Usage

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 }

Tests

Setup

Prepare Arduino

Prepare haskell test program

Running the tests