how to read 100 or more strings per second through py serial port

114 views Asked by At

Program to receive data from serial port-

import serial
import time 

ser = serial.Serial(
    port='/dev/ttyAM0',
    baudrate=57600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1)

while 1:
    BytesToRead = ser.inWaiting()
    x = ser.read(BytesToRead)
    print x

Input - @1,12,5,0:0:1# these types of 100 strings per second output - Mixed data with some data missing and jumbled

Note - The strings are received from arduino via zigbee. What am i doing wrong? Is there any delay problem or I am reading the strings wrongly through the serial port?

1

There are 1 answers

0
tomlogic On

If there aren't any bytes to read, then you shouldn't try to read or print them.

while 1:
    BytesToRead = ser.inWaiting()
    if BytesToRead > 0:
        x = ser.read(BytesToRead)
        print x