The following example illustrates python serial communication / python read serial port
What you need to have
Connect the device you want to read the SERIAL logs to the system
To know the port is working, cross check by opening the port and check the logs in TeraTerm
If TeraTerm already using the port, you cannot read the logs from SERIAL port, so make sure before executing the script, stop the TeraTerm
What the script will do
We use python module 'serial'
Create a serial object by passing port, baudrate, bytesize, parity etc.,
To continuously read the logs, use Python Thread concept
What you need to have
Connect the device you want to read the SERIAL logs to the system
To know the port is working, cross check by opening the port and check the logs in TeraTerm
If TeraTerm already using the port, you cannot read the logs from SERIAL port, so make sure before executing the script, stop the TeraTerm
What the script will do
We use python module 'serial'
Create a serial object by passing port, baudrate, bytesize, parity etc.,
To continuously read the logs, use Python Thread concept
from serial import * from threading import Thread text_received = '' def receiveSerialDataFromPort(ser): global text_received read_buffer = '' while True: read_buffer += ser.read(ser.inWaiting()) if '\n' in read_buffer: text_received, read_buffer = read_buffer.split('\n')[-2:] print text_received if __name__ == '__main__': ser = Serial( port='COM23', baudrate=230400, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, interCharTimeout=None ) Thread(target=receiveSerialDataFromPort, args=(ser,)).start()
No comments:
Post a Comment