Skip to content

Latest commit

 

History

History
66 lines (43 loc) · 1.18 KB

README.md

File metadata and controls

66 lines (43 loc) · 1.18 KB

modbus.cr

Modbus client library for Crystal

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      modbus:
        github: scttnlsn/modbus.cr
  2. Run shards install

Usage

require "modbus"

Documentation

API (master)

Example

stty -F /dev/ttyUSB0 cs8 19200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts -hupcl
require "modbus"

serial_port = File.open("/dev/ttyUSB0", "r+")
rtu_client = Modbus::RTUClient.open(serial_port)

# read 4 coils starting at address 1
coils : BitArray = rtu_client.read_coils(1, 4)
puts coils

require "socket"

socket = TCPSocket.new("example.modbus.host", 502)
tcp_client = Modbus::TCPClient.new(socket)

# read 1 register starting at address 123
registers = tcp_client.read_holding_registers(123, 1)
puts registers[0]

Testing

To run the tests:

crystal spec

Resources

Modbus protocol info: