Problem reading from serial - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem reading from serial (/thread-7628.html) |
Problem reading from serial - python_beginner - Jan-18-2018 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/pyserial_api.html I get: flushInput() NameError: name 'flushInput' is not defined RE: Problem reading from serial - j.crater - Jan-18-2018 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/pyserial_api.html#serial.Serial.reset_input_buffer RE: Problem reading from serial - python_beginner - Jan-18-2018 Thank you, I changed the command, but I still get: reset_input_buffer() NameError: name 'reset_input_buffer' is not definedI cannot understand! I import serial and time. What is wrong? RE: Problem reading from serial - j.crater - Jan-18-2018 You need to do ser.reset_input_buffer()reset_input_buffer() is a method of Serial class. RE: Problem reading from serial - nilamo - Jan-18-2018 (Jan-18-2018, 12:05 PM)python_beginner Wrote: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.line = ser.readline(); while line: print(line), RE: Problem reading from serial - python_beginner - Jan-19-2018 Thank you both your help. It works now. But it prints Escape characters with the data. Is there a way to avoid that? |