简体   繁体   English

阅读串行 Python

[英]Read Serial Python

I am reading serial data on my Raspberry Pi with the console:我正在使用控制台读取 Raspberry Pi 上的串行数据:

stty -F /dev/ttyUSB0 1:0:9a7:0:3:1c:7f:15:4:5:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
cat < /dev/ttyUSB0 &
echo -n -e '\x2F\x3F\x21\x0D\x0A' > /dev/ttyUSB0

And I am getting data line for line:我正在获取数据线:

/ISk5MT174-0001
0.9.1(210832)
0.9.2(1160808)
0.0.0(00339226)
0.2.0(1.03)
C.1.6(FDF5)
1.8.1(0004250.946*kWh)
1.8.2(0003664.811*kWh)
2.8.1(0004897.813*kWh)
2.8.2(0000397.465*kWh)
F.F.0(0000000)
!

Now I am trying to do this with python:现在我试图用 python 来做到这一点:

import serial

SERIALPORT = "/dev/ttyUSB0"
BAUDRATE = 300

ser = serial.Serial(SERIALPORT, BAUDRATE)

print("write data")
ser.write("\x2F\x3F\x21\x0D\x0A")

time.sleep(0.5)
numberOfLine = 0

while True:
  response = ser.readline()
  print("read data: " + response)

  numberOfLine = numberOfLine + 1 
  if (numberOfLine >= 5):
    break

ser.close()

But I only get "write data" and no response from my USB0 device.但我只得到“写入数据”,而我的 USB0 设备没有响应。

Any suggestions?有什么建议?

Kind Regards亲切的问候

I'm guessing your device is the same as discussed here: https://www.loxforum.com/forum/faqs-tutorials-howto-s/3121-mini-howto-z%C3%A4hlerauslesung-iskra-mt174-mit-ir-schreib-lesekopf-und-raspberry我猜你的设备和这里讨论的一样: https : //www.loxforum.com/forum/faqs-tutorials-howto-s/3121-mini-howto-z%C3%A4hlerauslesung-iskra-mt174-mit -ir-schreib-lesekopf-und-raspberry

If so, you need to know that by default, pySerial opens ports with 8 databits and no parity.如果是这样,您需要知道默认情况下,pySerial 打开具有 8 个数据位且无奇偶校验的端口。 (see: https://pythonhosted.org/pyserial/pyserial_api.html -> __init__) (参见: https : //pythonhosted.org/pyserial/pyserial_api.html -> __init__)

So, at the very least you want to:所以,至少你想:

ser = serial.Serial(SERIALPORT, BAUDRATE, SEVENBITS, PARITY_EVEN)

Perhaps you also need to set other flags, but I don't read stty :) To see what that string of numbers means, run the first stty command and then run:也许您还需要设置其他标志,但我不阅读 stty :) 要查看该数字字符串的含义,请运行第一个 stty 命令,然后运行:

stty -F /dev/ttyUSB0 -a

It'll output the settings in human readable form, that might bring you closer to a solution.它将以人类可读的形式输出设置,这可能会让您更接近解决方案。

Good luck!祝你好运!

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

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