SwiftyModbusResult
public class SwiftyModbusResult
Libmodbus wrapper class with Result
-
libmodbus error
See moreDeclaration
Swift
public struct ModbusError : Error
-
Error recovery options for setErrorRecovery function
See moreDeclaration
Swift
public struct ErrorRecoveryMode : OptionSet
-
Create a SwiftyModbus class for TCP Protocol
Declaration
Swift
public init(address: String, port: Int)
Parameters
address
IP address or host name
port
port number to connect to
-
Set debug flag of the context. When true, many verbose messages are displayed on stdout and stderr.
Declaration
Swift
public var debugMode: Bool { get set }
-
Set the slave number
Declaration
Swift
public func setSlave(_ slave: Int32)
Parameters
slave
slave number (from 1 to 247) or 0xFF (MODBUS_TCP_SLAVE)
-
Establish a connection to a Modbus server
Declaration
Swift
public func connect() -> Result<Void, ModbusError>
Return Value
esult
-
Close the connection established
Declaration
Swift
public func disconnect()
-
Set socket of the modbus context
Declaration
Swift
public func setSocket(socket: Int32)
Parameters
socket
socket handle
-
Get socket of the modbus context
Declaration
Swift
public func getSocket() -> Int32
Return Value
socket handle
-
Get/set the timeout TimeInterval used to wait for a response and connect
Declaration
Swift
public var responseTimeout: TimeInterval { get set }
-
Get/set the timeout interval between two consecutive bytes of the same message
Declaration
Swift
public var byteTimeout: TimeInterval { get set }
-
Retrieve the current header length
Declaration
Swift
public var headerLength: Int32 { get }
Return Value
header length from the backend
-
Flush non-transmitted data and discard data received but not read
Declaration
Swift
public func flush() -> Result<Void, ModbusError>
Return Value
Result
-
Set the error recovery mode to apply when the connection fails or the byte received is not expected.
Declaration
Swift
public func setErrorRecovery(mode: ErrorRecoveryMode) -> Result<Void, ModbusError>
Parameters
mode
ErrorRecoveryMode optionSet
-
Read the status of the bits (coils) to the address of the remote device. The function uses the Modbus function code 0x01 (read coil status).
Declaration
Swift
public func readBits(addr: Int32, count: Int32) -> Result<[UInt8], ModbusError>
Parameters
addr
address of the remote device
count
count of the bits (coils)
Return Value
Result with array of unsigned bytes (8 bits) set to TRUE(1) or FALSE(0).
-
Read the status of the input bits to the address of the remote device. The function uses the Modbus function code 0x02 (read input status).
Declaration
Swift
public func readInputBits(addr: Int32, count: Int32) -> Result<[UInt8], ModbusError>
Parameters
addr
address of the remote device
count
count of the input bits
Return Value
Result with array of unsigned bytes (8 bits) set to TRUE(1) or FALSE(0).
-
Read the content of the one holding register by address of the remote device. The function uses the Modbus function code 0x03 (read holding registers).
Declaration
Swift
public func readRegister(addr: Int32) -> Result<UInt16, ModbusError>
Parameters
addr
address of the remote device
Return Value
Result with register value as UInt16
-
Read the content of the holding registers to the address of the remote device. The function uses the Modbus function code 0x03 (read holding registers).
Declaration
Swift
public func readRegisters(addr: Int32, count: Int32) -> Result<[UInt16], ModbusError>
Parameters
addr
address of the remote device
count
count of the holding registers
Return Value
Result with array as unsigned word values (16 bits).
-
Read the content of the input registers to the address of the remote device. The function uses the Modbus function code 0x04 (read input registers).
Declaration
Swift
public func readInputRegisters(addr: Int32, count: Int32) -> Result<[UInt16], ModbusError>
Parameters
addr
address of the remote device
count
count of the input registers
Return Value
Result with array as unsigned word values (16 bits).
-
Write the status at the address of the remote device. The function uses the Modbus function code 0x05 (force single coil).
Declaration
Swift
public func writeBit(addr: Int32, status: Bool) -> Result<Void, ModbusError>
Parameters
addr
address of the remote device
status
boolean status to write
Return Value
Result
-
Write the status of the bits (coils) at the address of the remote device. The function uses the Modbus function code 0x0F (force multiple coils).
Declaration
Swift
public func writeBits(addr: Int32, status: [UInt8]) -> Result<Void, ModbusError>
Parameters
addr
address of the remote device
status
array of statuses
Return Value
Result
-
Write the value to the holding register at the address of the remote device. The function uses the Modbus function code 0x06 (preset single register).
Declaration
Swift
public func writeRegister(addr: Int32, value: UInt16) -> Result<Void, ModbusError>
Parameters
addr
address of the remote device
value
value of holding register
Return Value
Result
-
Write to holding registers at address of the remote device. The function uses the Modbus function code 0x10 (preset multiple registers).
Declaration
Swift
public func writeRegisters(addr: Int32, data: [UInt16]) -> Result<Void, ModbusError>
Parameters
addr
address of the remote device
data
array of values to be writteb
Return Value
Result
-
Modify the value of the holding register at the remote device using the algorithm: new value = (current value AND ‘and’) OR (‘or’ AND (NOT ‘and’)) The function uses the Modbus function code 0x16 (mask single register).
Declaration
Swift
public func maskWriteRegister(addr: Int32, maskAND: UInt16, maskOR: UInt16) -> Result<Void, ModbusError>
Parameters
addr
address of the remote device
maskAND
and mask
maskOR
or mask
Return Value
Result
-
Write and read number of registers in a single transaction The function uses the Modbus function code 0x17 (write/read registers).
Declaration
Swift
public func writeAndReadRegisters(writeAddr: Int32, data: [UInt16], readAddr: Int32, readCount: Int32) -> Result<[UInt16], ModbusError>
Parameters
writeAddr
address of the remote device to write
data
data array to write
readAddr
address of the remote device to read
readCount
count of read data
Return Value
Result with array as unsigned word values (16 bits).