serialport-0.4.7: Cross platform serial port library.

Safe HaskellNone

System.Hardware.Serialport

Contents

Description

This module provides the serial port interface.

 import qualified Data.ByteString.Char8 as B
 import System.Hardware.Serialport
 let port = "COM3"          -- Windows
 let port = "/dev/ttyUSB0"  -- Linux
 s <- openSerial port defaultSerialSettings { commSpeed = CS2400 }
 send s $ B.pack "AT\r"
 recv s 10 >>= print
 closeSerial s

Or use the experimental interface with standard handles:

 import System.IO
 import System.Hardware.Serialport
 let port = "COM3"           -- Windows
 let port = "/dev/ttyUSB0"   -- Linux
 h <- hOpenSerial port defaultSerialSettings
 hPutStr h "AT\r"
 hGetLine h >>= print
 hClose h

Synopsis

Types

data StopBits Source

Constructors

One 
Two 

data Parity Source

Constructors

Even 
Odd 
NoParity 

Instances

Configure port

You don't need the get or set functions, they are used by openSerial

data SerialPortSettings Source

Constructors

SerialPortSettings 

Fields

commSpeed :: CommSpeed

baudrate

bitsPerWord :: Word8

Number of bits in a word

stopb :: StopBits

Number of stop bits

parity :: Parity

Type of parity

flowControl :: FlowControl

Type of flowcontrol

timeout :: Int

Timeout when receiving a char in tenth of seconds

defaultSerialSettings :: SerialPortSettingsSource

Most commonly used configuration

  • 9600 baud
  • 8 data bits
  • 1 stop bit
  • no parity
  • no flow control
  • 0.1 second receive timeout

setSerialSettingsSource

Arguments

:: SerialPort

The currently opened serial port

-> SerialPortSettings

The new settings

-> IO SerialPort

New serial port

Configure the serial port

getSerialSettings :: SerialPort -> SerialPortSettingsSource

Get configuration from serial port

Serial methods

Device

hOpenSerial :: FilePath -> SerialPortSettings -> IO HandleSource

Open and configure a serial port returning a standard Handle

openSerialSource

Arguments

:: FilePath

Serial port, such as /dev/ttyS0 or /dev/ttyUSB0

-> SerialPortSettings 
-> IO SerialPort 

Open and configure a serial port

closeSerial :: SerialPort -> IO ()Source

Close the serial port

withSerial :: String -> SerialPortSettings -> (SerialPort -> IO a) -> IO aSource

Safer device function, so you don't forget to close the device

Sending & receiving

sendSource

Arguments

:: SerialPort 
-> ByteString 
-> IO Int

Number of bytes actually sent

Send bytes

recv :: SerialPort -> Int -> IO ByteStringSource

Receive bytes, given the maximum number

flush :: SerialPort -> IO ()Source

Flush buffers

Line control

setDTR :: SerialPort -> Bool -> IO ()Source

Set the Data Terminal Ready level

setRTS :: SerialPort -> Bool -> IO ()Source

Set the Ready to send level