簡體   English   中英

RS485通訊與Python

[英]RS485 communication with Python

這是我的問題:我需要通過 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 

我為我的問題找到了解決方案,只是我的閱讀沒有通過,我使用了.read(6),它現在可以工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM