简体   繁体   中英

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

I would like to send but especially receive serial communication 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?

尝试pySerial

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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