Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
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 withSerial port defaultSerialSettings{ commSpeed = CS2400 } $ \s -> do send s $ B.pack "AT\r" recv s 10 >>= print
Alternatively, use handles to perform IO:
import System.IO import System.Hardware.Serialport let port = "COM3" -- Windows let port = "/dev/ttyUSB0" -- Linux hWithSerial port defaultSerialSettings $ \h -> do hPutStr h "AT\r" hGetLine h >>= print
Synopsis
- data CommSpeed
- data StopBits
- data Parity
- data FlowControl
- data SerialPort
- data SerialPortSettings = SerialPortSettings {
- commSpeed :: CommSpeed
- bitsPerWord :: Word8
- stopb :: StopBits
- parity :: Parity
- flowControl :: FlowControl
- timeout :: Int
- defaultSerialSettings :: SerialPortSettings
- setSerialSettings :: SerialPort -> SerialPortSettings -> IO SerialPort
- getSerialSettings :: SerialPort -> SerialPortSettings
- hOpenSerial :: FilePath -> SerialPortSettings -> IO Handle
- openSerial :: FilePath -> SerialPortSettings -> IO SerialPort
- closeSerial :: SerialPort -> IO ()
- withSerial :: FilePath -> SerialPortSettings -> (SerialPort -> IO a) -> IO a
- hWithSerial :: FilePath -> SerialPortSettings -> (Handle -> IO a) -> IO a
- send :: SerialPort -> ByteString -> IO Int
- recv :: SerialPort -> Int -> IO ByteString
- flush :: SerialPort -> IO ()
- setDTR :: SerialPort -> Bool -> IO ()
- setRTS :: SerialPort -> Bool -> IO ()
Types
Supported baudrates
data FlowControl Source #
Instances
Eq FlowControl Source # | |
Defined in System.Hardware.Serialport.Types (==) :: FlowControl -> FlowControl -> Bool # (/=) :: FlowControl -> FlowControl -> Bool # | |
Read FlowControl Source # | |
Defined in System.Hardware.Serialport.Types readsPrec :: Int -> ReadS FlowControl # readList :: ReadS [FlowControl] # readPrec :: ReadPrec FlowControl # readListPrec :: ReadPrec [FlowControl] # | |
Show FlowControl Source # | |
Defined in System.Hardware.Serialport.Types showsPrec :: Int -> FlowControl -> ShowS # show :: FlowControl -> String # showList :: [FlowControl] -> ShowS # |
data SerialPort Source #
Instances
Show SerialPort Source # | |
Defined in System.Hardware.Serialport.Posix showsPrec :: Int -> SerialPort -> ShowS # show :: SerialPort -> String # showList :: [SerialPort] -> ShowS # |
Configure port
You don't need the get or set functions, they are used by openSerial
data SerialPortSettings Source #
SerialPortSettings | |
|
Instances
Eq SerialPortSettings Source # | |
Defined in System.Hardware.Serialport.Types (==) :: SerialPortSettings -> SerialPortSettings -> Bool # (/=) :: SerialPortSettings -> SerialPortSettings -> Bool # | |
Read SerialPortSettings Source # | |
Defined in System.Hardware.Serialport.Types | |
Show SerialPortSettings Source # | |
Defined in System.Hardware.Serialport.Types showsPrec :: Int -> SerialPortSettings -> ShowS # show :: SerialPortSettings -> String # showList :: [SerialPortSettings] -> ShowS # |
defaultSerialSettings :: SerialPortSettings Source #
Most commonly used configuration
- 9600 baud
- 8 data bits
- 1 stop bit
- no parity
- no flow control
- 0.1 second receive timeout
:: SerialPort | The currently opened serial port |
-> SerialPortSettings | The new settings |
-> IO SerialPort | New serial port |
Configure the serial port
getSerialSettings :: SerialPort -> SerialPortSettings Source #
Get configuration from serial port
Serial methods
Device
hOpenSerial :: FilePath -> SerialPortSettings -> IO Handle Source #
Open and configure a serial port returning a standard Handle
:: FilePath | Serial port, such as |
-> SerialPortSettings | |
-> IO SerialPort |
Open and configure a serial port
closeSerial :: SerialPort -> IO () Source #
Close the serial port
withSerial :: FilePath -> SerialPortSettings -> (SerialPort -> IO a) -> IO a Source #
Safer device function, so you don't forget to close the device
hWithSerial :: FilePath -> SerialPortSettings -> (Handle -> IO a) -> IO a Source #
Like withSerial
but using Handle
Sending & receiving
recv :: SerialPort -> Int -> IO ByteString Source #
Receive bytes, given the maximum number
flush :: SerialPort -> IO () Source #
Flush buffers