python - 无法在 esp8266 上使用 UART 从 LE-01MR 电表和使用 rs485 读取数据
问题描述
我尝试从 LE-01MR 电表读取数据,并根据其文档(https://www.fif.com.pl/en/usage-electric-power-meters/517-electricity-consumption-meter-le- 01mr.html),我必须发送命令 0x03 才能从中读取数据。但是,我做不到,我的结果总是无,根据 micropython 文档,它告诉我我超时了(https://docs.micropython.org/en/latest/library/machine.UART .html#machine-uart)。我用另一台连接到电线的计算机测试了接线是否正确,并且我能够从中读取数据,因此接线应该不是问题。我将 esp8266 的 tx/rx 引脚连接到 tx/rx(分别)。这是我的代码,任何帮助将不胜感激:
import utime
from machine import UART
from machine import Pin
import uos
def test():
print("modbus")
uos.dupterm(None, 1)
modbus = UART(0)
modbus.init(9600, parity=None, stop=1, timeout=500, timeout_char=2, tx=Pin(1), rx=Pin(3))
print("Reading from modbus: {}".format(modbus))
print("Can read: {}".format(modbus.any()))
while True:
modbus.write(b'\x03')
result = modbus.read(4)
print("Value of reading: {}, type of {}".format(result, type(result)))
utime.sleep(0.5)
uart = UART(0, 115200)
uos.dupterm(uart, 1)
再一次感谢你!
解决方案
我觉得你对这个任务的理解有点离题。
Modbus 是一个成熟的协议,您需要做的是发送 Modbus 命令 03(读取保持寄存器)。你正在做的只是发送 hex 0x03
。
您应该首先在 YouTube 或 Google 上找到一个好的 Modbus 教程。
完成此操作后,您可能会意识到您的微控制器需要一个 Modbus 库。您可能想从这里开始。我不能保证它会与您的设置一起使用,但它看起来很有希望。如果您愿意,当然可以编写自己的 Modbus 协议实现。
如果您在开始使用库后需要更多详细信息,请随时编辑您的问题。
祝你好运,玩得开心你的项目。
推荐阅读
- python - Python:调整雷达图上的 x 轴标签
- node.js - Laravel 套接字订阅
- python - Python多个两个相同的数据框以形成一个新的数据框
- php - 上传文件时我只得到数组中的第一张图像,我想要数组中的所有图像。使用 Codeigniter
- c# - 在 C# 中递归上传到 FTP 服务器
- javascript - 使用默认 Next.js 开发服务器代理到后端
- amazon-web-services - 在 S3 存储桶中配置 CORS 无效
- python - 将 mypy 与实例属性的延迟初始化一起使用
- angular - 如何在 Jhipster 中创建一个简单的非 CRUD 屏幕?
- rxjs - 在返回 HttpResponse 时映射 HttpClient 响应数据