简体   繁体   English

通过python中的rs485进行通信

[英]Communicate via rs485 in python

I am new in python. 我是python的新手。 I am trying to catch data from temperature sensor via RS485 in python 我正在尝试通过python中的RS485从温度传感器捕获数据

Here my code: 这是我的代码:

ser = serial.Serial(
port='COM3',
baudrate=19200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS)

print ser.write('{02RDD}\r')

Response: >> 8 回应:>> 8

The response is always string length (ex ser.write("abcd"), response is "4") no matter what I send. 无论我发送什么,响应始终为字符串长度(例如,ser.write(“ abcd”),响应为“ 4”)。

Please help me how to solve this problem, Thanks 请帮我解决这个问题,谢谢

If you want to read from the serial port use ser.read() or ser.readline() . 如果要从串行端口读取,请使用ser.read()ser.readline()

The ser.write() , which you have used, only returns the number of bytes sent, which should correspond to the length of the string. 您使用的ser.write()仅返回发送的字节数,该字节数应与字符串的长度相对应。 So it seems to work correctly. 因此,它似乎可以正常工作。

You should maybe go though the examples that are given in the pyserial documentation to make youself more familiar with the library. 您也许应该仔细阅读pyserial文档中提供的示例,以使自己更加熟悉该库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM