4

我是 Pyserial 和硬件领域的新手。我正在尝试运行http://pyserial.sourceforge.net/shortintro.html#opening-serial-ports中给出的示例应用程序

import serial

ser = serial.Serial(0)  # open first serial port
print ser.portstr       # check which port was really used
ser.write("hello")      # write a string
ser.close()

我已经在 python 文件中编写了这个程序并运行它。现在,如果我想测试这个应用程序以检查我是否发送了正确的字符串(例如:超级终端或其他东西),我该怎么做。谁能指导我?

4

2 回答 2

2

虚拟串口

使用虚拟串行端口进行测试。

对于 Windows,我在 Linux 上使用com0comsocat。然后,使用 Putty 可视化您的发送。

于 2012-01-23T13:07:50.910 回答
2

测试物理串行端口的另一种快速方法是使用电线/螺丝刀、鳄鱼夹或您手头的任何东西,并将 RX 和 TX(接收和发送)桥接在一起。那时,您发送的所有内容都会循环返回给您。之后,您可以在此处使用此代码段接收它:

import serial

ser = serial.Serial(0, timeout = 1)  # open first serial port
print ser.portstr       # check which port was really used
ser.write("hello")      # write a string
msg = ser.read("100") #read the content of the input buffer until you get 100 byte or a timeout event
print(msg) #print the content you might need to decode it print(decode(msg))
ser.close()

此代码正常工作的关键方面是将 RX 和 TX 桥接在一起。很多教程会告诉你如何做到这一点。

于 2017-05-25T14:53:03.837 回答