Safe Haskell | None |
---|---|
Language | Haskell98 |
Haskell bindings to the C modbus library https://libmodbus.org/
Synopsis
- data Context
- newtype Addr = Addr Int
- newtype Baud = Baud Int
- data Parity
- newtype DataBits = DataBits Int
- newtype StopBits = StopBits Int
- new_rtu :: FilePath -> Baud -> Parity -> DataBits -> StopBits -> IO Context
- data SerialMode
- rtu_get_serial_mode :: Context -> IO SerialMode
- rtu_set_serial_mode :: Context -> SerialMode -> IO ()
- data RTS
- rtu_get_rts :: Context -> IO RTS
- rtu_set_rts :: Context -> RTS -> IO ()
- rtu_get_rts_delay :: Context -> IO Int
- rtu_set_rts_delay :: Context -> Int -> IO ()
- data IPAddress
- newtype Port = Port Int
- new_tcp :: IPAddress -> Port -> IO Context
- data Node
- newtype Service = Service String
- new_tcp_pi :: Node -> Service -> IO Context
- newtype DeviceAddress = DeviceAddress Int
- broadcastAddress :: DeviceAddress
- set_slave :: Context -> DeviceAddress -> IO ()
- connect :: Context -> IO ()
- set_debug :: Context -> Bool -> IO ()
- data Timeout = Timeout {}
- get_byte_timeout :: Context -> IO Timeout
- set_byte_timeout :: Context -> Timeout -> IO ()
- get_response_timeout :: Context -> IO Timeout
- set_response_timeout :: Context -> Timeout -> IO ()
- data ErrorRecoveryMode
- set_error_recovery :: Context -> ErrorRecoveryMode -> IO ()
- type RegisterVector = IOVector Word16
- mkRegisterVector :: Int -> IO RegisterVector
- class RegisterData t where
- fromRegisterVector :: RegisterVector -> IO t
- toRegisterVector :: t -> IO RegisterVector
- read_registers :: RegisterData t => Context -> Addr -> RegisterVector -> IO t
- read_input_registers :: RegisterData t => Context -> Addr -> RegisterVector -> IO t
- write_registers :: Context -> Addr -> RegisterVector -> IO ()
- write_register :: Context -> Addr -> Word16 -> IO ()
- write_and_read_registers :: Context -> Addr -> RegisterVector -> Addr -> RegisterVector -> IO ()
- type BitVector = IOVector Bit
- mkBitVector :: Int -> IO BitVector
- class BitData t where
- fromBitVector :: BitVector -> IO t
- toBitVector :: t -> IO BitVector
- type Bit = Word8
- boolBit :: Bit -> Bool
- bitBool :: Bool -> Bit
- read_bits :: BitData t => Context -> Addr -> BitVector -> IO t
- read_input_bits :: BitData t => Context -> Addr -> BitVector -> IO t
- write_bits :: Context -> Addr -> BitVector -> IO ()
- write_bit :: Context -> Addr -> Bit -> IO ()
Equivilance to the C library
Functions in this module are named the same as those in the C library, but without the leading "modbus_". You may wish to import this module qualified as Modbus to make the names match up.
See the C library documentation for details about the use of any function. https://libmodbus.org/documentation/
When a function in the C library returns a special value on error, this module will instead throw an exception.
This module has been tested with version 3.1.4 of the C library. It may also work with other versions.
Example
This example reads some of the registers of an Epever solar charge controller. It shows how the binary library can be used to decode the modbus registers into a haskell data structure.
import System.Modbus import Data.Binary.Get main = do mb <- new_rtu "/dev/ttyS1" (Baud 115200) ParityNone (DataBits 8) (StopBits 1) set_slave mb (DeviceAddress 1) connect mb regs <- mkRegisterVector 5 b <- read_input_registers mb (Addr 0x3100) regs print $ runGet getEpever b data Epever = Epever { pv_array_voltage :: Float , pv_array_current :: Float , pv_array_power :: Float , battery_voltage :: Float } deriving (Show) getEpever :: Get Epever getEpever = Epever <$> epeverfloat -- register 0x3100 <*> epeverfloat -- register 0x3101 <*> epeverfloat2 -- register 0x3102 (low) and 0x3103 (high) <*> epeverfloat -- register 0x3104 where epeverfloat = decimals 2 <$> getWord16host epeverfloat2 = do l <- getWord16host h <- getWord16host return (decimals 2 (l + h*2^16)) decimals n v = fromIntegral v / (10^n)
Core data types
A modbus device context.
The context will automatically be closed and freed when it is garbage collected.
An address on a modbus device.
RTU Context
new_rtu :: FilePath -> Baud -> Parity -> DataBits -> StopBits -> IO Context Source #
Create a modbus Remote Terminal Unit context.
The FilePath is the serial device to connect to.
data SerialMode Source #
Instances
Eq SerialMode Source # | |
Defined in System.Modbus (==) :: SerialMode -> SerialMode -> Bool # (/=) :: SerialMode -> SerialMode -> Bool # | |
Show SerialMode Source # | |
Defined in System.Modbus showsPrec :: Int -> SerialMode -> ShowS # show :: SerialMode -> String # showList :: [SerialMode] -> ShowS # |
rtu_set_serial_mode :: Context -> SerialMode -> IO () Source #
TCP (IPv4) Context
IPv4 address to connect to. In server mode, use AnyAddress to listen to any addresses.
TCP PI (IPv4 and IPv6) Context
Host name or IP address to connect to. In server mode, use AnyNode to listen to any addresses.
Service name/port number to connect to.
Configuration
newtype DeviceAddress Source #
The address of a modbus device.
Instances
Eq DeviceAddress Source # | |
Defined in System.Modbus (==) :: DeviceAddress -> DeviceAddress -> Bool # (/=) :: DeviceAddress -> DeviceAddress -> Bool # | |
Show DeviceAddress Source # | |
Defined in System.Modbus showsPrec :: Int -> DeviceAddress -> ShowS # show :: DeviceAddress -> String # showList :: [DeviceAddress] -> ShowS # |
set_slave :: Context -> DeviceAddress -> IO () Source #
Set the address of the modbus device that the Context should communicate with.
data ErrorRecoveryMode Source #
ErrorRecoveryNone | |
ErrorRecoveryLink | Reconnect after response timeout. |
ErrorRecoveryProtocol | Clean up ongoing communication. |
ErrorRecoveryLinkProtocol | Combine both. |
set_error_recovery :: Context -> ErrorRecoveryMode -> IO () Source #
Accessing registers
type RegisterVector = IOVector Word16 Source #
A mutable vector that is used as a buffer when reading or writing registers of a modbus device.
mkRegisterVector :: Int -> IO RegisterVector Source #
Allocates a vector holding the contents of a specified number of registers.
The values are initialized to 0 to start.
class RegisterData t where Source #
Types that can hold modbus register data.
Of these, RegisterVector
is the most efficient as it avoids
allocating new memory on each read or write. But it can be more useful
to get a ByteString and use a library such as cereal or binary to
parse the contents of the modbus registers.
fromRegisterVector :: RegisterVector -> IO t Source #
toRegisterVector :: t -> IO RegisterVector Source #
Instances
RegisterData ByteString Source # | |
Defined in System.Modbus | |
RegisterData ByteString Source # | |
Defined in System.Modbus | |
RegisterData RegisterVector Source # | |
Defined in System.Modbus | |
RegisterData (Vector Word16) Source # | |
Defined in System.Modbus fromRegisterVector :: RegisterVector -> IO (Vector Word16) Source # toRegisterVector :: Vector Word16 -> IO RegisterVector Source # |
read_registers :: RegisterData t => Context -> Addr -> RegisterVector -> IO t Source #
Reads the holding registers from the modbus device, starting at the Addr, into the RegisterVector buffer.
read_input_registers :: RegisterData t => Context -> Addr -> RegisterVector -> IO t Source #
Reads the input registers from the modbus device, starting at the Addr, into the RegisterVector buffer.
write_registers :: Context -> Addr -> RegisterVector -> IO () Source #
Writes the registers to the modbus device, starting at the Addr.
write_and_read_registers Source #
:: Context | |
-> Addr | address to write to |
-> RegisterVector | data to write |
-> Addr | address to read from |
-> RegisterVector | data to read |
-> IO () |
Accessing bits/coils
type BitVector = IOVector Bit Source #
A mutable vector that is used as a buffer when reading or writing bits (coils) of a modbus device.
mkBitVector :: Int -> IO BitVector Source #
Allocates a vector holding the specified number of bits.
The bits are set to start.
class BitData t where Source #
Types that can hold modbus bit data.
Of these, BitVector
is the most efficient as it avoids
allocating new memory on each read or write. But it can be easier
to use a Vector of Bool.
fromBitVector :: BitVector -> IO t Source #
toBitVector :: t -> IO BitVector Source #
read_bits :: BitData t => Context -> Addr -> BitVector -> IO t Source #
Reads the bits (coils) from the modbus device, starting at the Addr, into the BitVector.
read_input_bits :: BitData t => Context -> Addr -> BitVector -> IO t Source #
Reads the input bits from the modbus device, starting at the Addr, into the BitVector.