Skip to content

Utility for convenient packet generation for Modbus protocol in Swift

License

Notifications You must be signed in to change notification settings

ivanesik/ModSwift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ModSwift

Swift Xcode MIT


Summary

This library create for easy generate modbus commands on swift

Features

  • Modbus Master mode
  • .tcp mode

Requirements

Functions

  • Read coil (one bit) register: readCoilStatus = 0x01

  • Read discrete (one bit) register: readDiscreteInputs = 0x02

  • Read holding (16 bit) register: readHoldingRegisters = 0x03

  • Read input (16 bit) register: readInputRegisters = 0x04

  • Write coil (one bit) register: forceSingleCoil = 0x05

  • Write holding (16 bit bit) register: presetSingleRegister = 0x06

  • Write multiply coil (one bit) registers: forceMultipleCoils = 0x0F

  • Write multiply holding (16 bit bit) registers: presetMultipleRegisters= 0x10

Installation

Download repository and add ModSwift folder in your project

Usage

Setup

    var modbus = ModSwift()             // Default .tcp, slave address = 0x00
    modbus.setSlave(slaveAdress: 10)    // Set modbus slave address to 0x0A
    modbus.setMode(.rtu)                // Set package generate to ModbusRTU mode
    modbus.setTransactionId(15)         // Set modbus transaction id to 0x000E (just in .tcp mode)
    modbus.setProtocolId(3)             // Set modbus protocol id to 0x0003 (just in .tcp mode)

Package create

var modbus = ModSwift()
let data = modbus.readCoilStatus(startAddress: 0x010D, numOfCoils: 0x0019)
print(data as NSData) //<00010000 00060B01 010D0019>

let data = modbus.readHoldingRegisters(startAddress: 0x010D, numOfCoils: 0x0019)
print(data as NSData) //<00010000 00060B03 010D0019>

TODO

  • Read reseave packages
  • Slave mode
  • Modbus RTU mode
  • Modbus ASCII mode
  • CocoaPods

License

MIT licensed.