Skip to content

standardsemiconductor/serialport

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  • Upload arduino code using Arduino IDE or avrdude

Prepare haskell test program

  • Configure cabal to build the tests: cabal configure --enable-tests.
  • Build: cabal build

Running the tests

  • Run the tests: cabal test --test-options="/dev/ttyACM0 /dev/ttyUSB0"