https://github.com/scttnlsn/modbus.cr
Modbus client library for Crystal
https://github.com/scttnlsn/modbus.cr
crystal modbus
Last synced: 12 months ago
JSON representation
Modbus client library for Crystal
- Host: GitHub
- URL: https://github.com/scttnlsn/modbus.cr
- Owner: scttnlsn
- License: mit
- Created: 2020-04-08T15:07:32.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-14T15:44:34.000Z (over 5 years ago)
- Last Synced: 2025-02-13T14:44:35.419Z (about 1 year ago)
- Topics: crystal, modbus
- Language: Crystal
- Homepage:
- Size: 43.9 KB
- Stars: 2
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# modbus.cr
Modbus client library for Crystal
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
modbus:
github: scttnlsn/modbus.cr
```
2. Run `shards install`
## Usage
```crystal
require "modbus"
```
## Documentation
[API (master)](https://scttnlsn.github.io/modbus.cr/)
## Example
```bash
stty -F /dev/ttyUSB0 cs8 19200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts -hupcl
```
```crystal
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:
* https://www.fernhillsoftware.com/help/drivers/modbus/modbus-protocol.html
* https://minimalmodbus.readthedocs.io/en/stable/modbusdetails.html