SwiftyModbus

2.2.1

Swift wrapper for the libmodbus library with promises
DimaRU/SwiftyModbus

What's New

2.2.1

2023-07-17T06:57:20Z

Fix Package definition file errors for Xcode 15/Swift 5.9.

SwiftyModbus

This is a Swift wrapper class for the libmodbus library. Now support only TCP modbus connection. Support both macOS and Linux.

How To Get Started

  • Install libmodbus library
brew install libmodbus

or

apt install libmodbus-dev
  • Add
    .package(url: "https://github.com/DimaRU/SwiftyModbus.git", .upToNextMajor(from: "2.1.0")) to your dependencies.

SwiftyModbus now have three encapsulations

  • PromiseKit: dependencies: [.product(name: "SwiftyModbusPromise", package: "SwiftyModbus")]
  • Result: dependencies: [.product(name: "SwiftyModbus", package: "SwiftyModbus")]
  • Throwable: dependencies: [.product(name: "SwiftyModbus", package: "SwiftyModbus")]

Documentation

Autogenerated documentation https://dimaru.github.io/SwiftyModbus/

Examples

With PromiseKit:

import Foundation
import SwiftyModbusPromise
import PromiseKit

let modbus = SwiftyModbusPromise(address: "127.0.0.1", port: 503)
modbus.responseTimeout = 1.5      // Set timeout for 1.5 sec
print(modbus.responseTimeout)

firstly {
    modbus.connect()
}.then {
    modbus.readRegisters(addr: 102, count: 10)
}.done { registers in
    print(registers)
}.then {
    modbus.writeRegisters(addr: 102, data: [1,2,3,4,5,6,7,8,9,10])
}.then {
    modbus.readInputRegisters(addr: 102, count: 10)
}.done { registers in
    print(registers)
}.then {
    modbus.writeBits(addr: 104, status: [0,1,0,1])
}.then {
    modbus.readBits(addr: 104, count: 4)
}.done { bits in
    print(bits)
}.then {
    modbus.disconnect()
}.catch { error in
    print(error)
}.finally {
    exit(0)
}

RunLoop.main.run()

Throwable:

import Foundation
import SwiftyModbus

let modbusQueue = DispatchQueue(label: "queue.modbus")

modbusQueue.async {
    modbusIO()
}
RunLoop.main.run()

func modbusIO() {
    let modbus = SwiftyModbus(address: "127.0.0.1", port: 503)
    modbus.responseTimeout = 1.5      // Set response timeout for 1.5 sec
    print("Modbus responce timeout:", modbus.responseTimeout)

    do {
        try modbus.connect()
        let registers = try modbus.readRegisters(addr: 102, count: 10)
        print(registers)
        try modbus.writeRegisters(addr: 102, data: [1,2,3,4,5,6,7,8,9,10])
        let inputRegisters = try modbus.readInputRegisters(addr: 102, count: 10)
        print(inputRegisters)
        try modbus.writeBits(addr: 104, status: [0,1,0,1])
        let bits = try modbus.readBits(addr: 104, count: 4)
        print(bits)
        modbus.disconnect()
        exit(0)
    } catch {
        print(error)
        exit(1)
    }
}

You may test this example with diagslave

sudo ./diagslave -m tcp -p 503

Sample modbus project

https://github.com/DimaRU/SampleSwiftyModbus.git

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

Last updated: Mon Oct 21 2024 14:13:20 GMT-0900 (Hawaii-Aleutian Daylight Time)