开发者

How do I send & receive via RS-232 using Python?

I would like to send but especially receive serial commu开发者_如何学运维nication using the RS-232 standard. I would like to write a python program to receive data and then write it to files when they come down the line.

How do i write a program in Python to block while waiting for data to arrive instead of spinning in a loop and checking?


Try pySerial


select


Use this code:

def read_data():
    data = ser.read(1)
    if data:
        n = ser.inWaiting()
        if n > 0: data += ser.read(n)
        return data

ser = serial.Serial(...)
print read_data()

You can experiment with timeout param for make the function more efficient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜