Python Forum

Full Version: Problem reading from serial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I'm new in python. I use an Arduino to sensing, and I wrote a small python program to read the data from usb. I use Python 3.5. The program is this:

#!/usr/bin/env python
import serial,time
ser = serial.Serial(
    port='COM8',
    baudrate=115200,
    bytesize=serial.EIGHTBITS,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE
)
line = ser.readline();

while line:
    print(line),
#    flushInput()
#ser.close()
When I execute it, I get only the first sentence repeatedly of what Arduino sends, not the rest of of the calculations. So I suppose I should flush the Serial buffer. When I flush the serial with the command flushInput() as I read from here: http://pyserial.readthedocs.io/en/latest...l_api.html
I get:

flushInput()
NameError: name 'flushInput' is not defined
Hello and welcome to Python and our forums!
There seems to be a little change in API, see if this will fix your error:
http://pyserial.readthedocs.io/en/latest...put_buffer
Thank you,

I changed the command, but I still get:

    reset_input_buffer()
NameError: name 'reset_input_buffer' is not defined
I cannot understand! I import serial and time. What is wrong?
You need to do

ser.reset_input_buffer()
reset_input_buffer() is a method of Serial class.
(Jan-18-2018, 12:05 PM)python_beginner Wrote: [ -> ]
line = ser.readline();
 
while line:
    print(line),
You read one line, then enter an infinite loop and print that one line repeatedly. If you want to read more lines, do the reading inside the while loop.
Thank you both your help. It works now. But it prints Escape characters with the data. Is there a way to avoid that?