ruby - ModBus TCP communication using Ruby: failing to implement HelloWorld
问题描述
I am able to communicate with a ventilation installation (Helios KWL EC 500 W which supports holding registers only, english description starts at 50% of the file) using the modpoll utility v3.4. But I failed to transfer the very first communication to Ruby and the rmodbus library v1.3.3.
With modpoll, I may request some temperature value with the command
./modpoll -m tcp -a 180 <ipaddr> 0x7630 0x3031 0x3034 0x0000
and then read the data using
./modpoll -m tcp -a 180 -t4:hex -c 8 -0 -1 <ipaddr>
Protocol configuration: MODBUS/TCP
Slave configuration...: address = 180, start reference = 1 (PDU), count = 8
Communication.........: x.x.x.x, port 502, t/o 2.00 s, poll rate 1000 ms
Data type.............: 16-bit register (hex), output (holding) register table
-- Polling slave...
[1]: 0x7630
which outputs 8 16bit registers as stated as example in the Helios modbus documentation. As very first step, I tried to move the read part to Ruby. However, my Ruby code times out:
require 'rmodbus'
ModBus::TCPClient.new(ipaddr, 502) do |client|
client.with_slave(1) do |slave|
slave.debug = true
puts slave.holding_registers[180..187]
end
end
and throws the exception
/var/lib/gems/2.3.0/gems/rmodbus-1.3.3/lib/rmodbus/slave.rb:241:in `rescue in query':
Timed out during read attempt (ModBus::Errors::ModBusTimeout)
from /var/lib/gems/2.3.0/gems/rmodbus-1.3.3/lib/rmodbus/slave.rb:232:in `query'
from /var/lib/gems/2.3.0/gems/rmodbus-1.3.3/lib/rmodbus/slave.rb:164:in `read_holding_registers'
What's wrong?
I am not sure if / how to use the parameters output by modpoll "address =180" and "start reference =1". Is "address" equivalent to "holding register #"?
解决方案
好吧,这个有点傻。对于记录(以及其他可能想使用 rmodbus 与他们的 Helios 交谈的人):
slave.debug = 1
打开输出发送到 modbus slave 的字节流的调试。第一个字节序列应该是:事务号(2 个字节),协议说明符(2 个字节,始终为零),以下消息的大小(2 个字节),单元标识符(1 个字节,对于 Helios KWL 始终为 180)。
从站需要使用其单元标识符 180 进行初始化,而不是 1:
client.with_slave(180) do |slave|
推荐阅读
- javascript - 如何识别一个 YouTrack 工作流程是否由另一个工作流程触发
- sql - SQL - 查找与多个/可变表变量行的组合关联的外键 ID
- javascript - JS | Switch 语句 | 如何创建密码
- angular - 如何使用角度模式验证空格之间的连字符
- database - 略有不同实体的数据库架构
- google-sites - Google 网站无法正确访问外部网址
- android - Cardview - 透明背景的圆角
- python - 如何使用 Pytesseract 文本识别改进 OCR?
- amazon-web-services - 如何在 AWS SAM 中向私有 API 网关添加标签
- javascript - HTML 输入文件 capture=camera 用于多个文件