【问题标题】:RS485 communication with Python使用 Python 进行 RS485 通信
【发布时间】:2021-05-31 22:16:11
【问题描述】:

这是我的问题: 我需要通过 RS485 向设备发送十六进制命令以使其工作。 我可以使用 RealTerm 与此设备完美通信,我发送请求并收到一致的响应。

但是现在我想用 python 脚本发送它们,但我不知道我做错了什么,但是当我尝试用我的脚本向它发送命令时,我没有得到设备的任何响应。

这是脚本:

import serial           # import the module
ComPort = serial.Serial('COM3') # open COM3
ComPort.baudrate = 9600 # set Baud rate to 9600
ComPort.bytesize = 8    # Number of data bits = 8
ComPort.parity   = 'N'  # No parity
ComPort.stopbits = 1    # Number of Stop bits = 1

data = bytearray(b'\xfa\x02\x02\x2a\xfe\x0c')

No = ComPort.write(data)

print(data)                      # print the data
dataIn = ComPort.readline()        # Wait and read data
print(dataIn)                      # print the received data

ComPort.close()         # Close the Com port 

【问题讨论】:

  • 对不起,我早该说这个了。目标是可以通过 rs485 控制的压力调节器
  • 对不起
  • 其实我只是希望能够在 COM 上写我的命令并收到答案。

标签: python serial-port communication rs485


【解决方案1】:

我找到了解决问题的方法, 只是我的阅读没有通过,我使用了 .read(6) 并且现在可以使用。

【讨论】:

    猜你喜欢
    • 2017-08-13
    • 2017-01-08
    • 2019-06-15
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多