Method for simple python programs to read serial port information

  • 2020-04-02 14:39:00
  • OfStack

This article illustrates how a simple python program can read serial port information. Share with you for your reference. Specific analysis is as follows:

This code needs to call the serial module to read serial port data through a while loop


import time
import serial
ser = serial.Serial( # The following parameters are modified as the case may be 
  port='COM1',
  baudrate=9600,
  parity=serial.PARITY_ODD,
  stopbits=serial.STOPBITS_TWO,
  bytesize=serial.SEVENBITS
)
data = ''
while ser.inWaiting() > 0:
  data += ser.read(1)
if data != '':
  print data

I hope this article has helped you with your Python programming.


Related articles: