1

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?

Poojan
  • 29
  • 4
  • For starter, did u make sure that the Serial settings matches with the one you are receiving from(in your case Arduino, if I understood you well) ? – Iron Fist Sep 05 '17 at 07:45
  • yes I have checked them already – Poojan Sep 05 '17 at 09:59
  • So, you are trying to read data from Arduino? Can u post Arduino's code as well? – Iron Fist Sep 05 '17 at 10:37
  • And did you try to communicate with Arduino only (without ZigBee), just to check communication is established properly between them? – Iron Fist Sep 05 '17 at 10:38
  • Yes I have checked the communication between them. The problem is that, I am receiving the strings but I can't read them all in a second – Poojan Sep 06 '17 at 06:08
  • Try to introduce some delay in your while loop, after `print x` add `time.sleep(0.2)`, this will probably give more time to the `ser.inWaiting` to properly read the number of bytes before reading the next. – Iron Fist Sep 06 '17 at 13:36

1 Answers1

0

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
tomlogic
  • 11,489
  • 3
  • 33
  • 59