SwiftyModbus
public class SwiftyModbus
Libmodbus wrapper class
-
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
addressIP address or host name
portport 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
slaveslave number (from 1 to 247) or 0xFF (MODBUS_TCP_SLAVE)
-
Establish a connection to a Modbus server
Declaration
Swift
public func connect() throwsReturn Value
esult<Void
-
Close the connection established
Declaration
Swift
public func disconnect() -
Set socket of the modbus context
Declaration
Swift
public func setSocket(socket: Int32)Parameters
socketsocket handle
-
Get socket of the modbus context
Declaration
Swift
public func getSocket() -> Int32Return 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() throws -
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) throwsParameters
modeErrorRecoveryMode 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) throws -> [UInt8]Parameters
addraddress of the remote device
countcount 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) throws -> [UInt8]Parameters
addraddress of the remote device
countcount 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) throws -> UInt16Parameters
addraddress 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) throws -> [UInt16]Parameters
addraddress of the remote device
countcount 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) throws -> [UInt16]Parameters
addraddress of the remote device
countcount 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) throwsParameters
addraddress of the remote device
statusboolean status to write
-
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]) throwsParameters
addraddress of the remote device
statusarray of statuses
-
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) throwsParameters
addraddress of the remote device
valuevalue of holding register
-
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]) throwsParameters
addraddress of the remote device
dataarray of values to be writteb
-
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) throwsParameters
addraddress of the remote device
maskANDand mask
maskORor mask
-
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) throws -> [UInt16]Parameters
writeAddraddress of the remote device to write
datadata array to write
readAddraddress of the remote device to read
readCountcount of read data
Return Value
Result with array as unsigned word values (16 bits).
SwiftyModbus Class Reference