SwiftyModbus

public class SwiftyModbus

Libmodbus wrapper class

  • libmodbus error

    See more

    Declaration

    Swift

    public struct ModbusError : Error
  • Error recovery options for setErrorRecovery function

    See more

    Declaration

    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() throws

    Return 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

    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() 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) throws

    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) throws -> [UInt8]

    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) throws -> [UInt8]

    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) throws -> UInt16

    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) throws -> [UInt16]

    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) throws -> [UInt16]

    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) throws

    Parameters

    addr

    address of the remote device

    status

    boolean 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]) throws

    Parameters

    addr

    address of the remote device

    status

    array 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) throws

    Parameters

    addr

    address of the remote device

    value

    value 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]) throws

    Parameters

    addr

    address of the remote device

    data

    array 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) throws

    Parameters

    addr

    address of the remote device

    maskAND

    and mask

    maskOR

    or 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

    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).