我是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文件中编写了这个程序并运行它。现在,如果我想测试这个应用程序以检查我发送的字符串是否正确(例如:超级终端之类的),我该怎么做呢?有人能给我指路吗?
发布于 2012-01-23 21:07:51

使用虚拟串行端口进行测试。
对于Windows,我使用com0com,对于Linux,我使用socat。然后,使用Putty可视化您的发送。
发布于 2017-05-25 22:53:04
另一种测试物理串行端口的快速方法是拿起电线/螺丝刀、鳄鱼钳或手中的任何东西,将RX和TX (接收和发送)桥接在一起。在这一点上,你发送出去的所有东西都会回传给你。之后,YOu可以使用下面的代码片段来接收它:
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桥接在一起。很多教程将向你展示如何做到这一点。
https://stackoverflow.com/questions/8971112
复制相似问题