Modbus client library for Crystal
-
Add the dependency to your
shard.yml
:dependencies: modbus: github: scttnlsn/modbus.cr
-
Run
shards install
require "modbus"
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]
To run the tests:
crystal spec
Modbus protocol info: