Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem reading from serial
#1
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
Reply
#2
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
Reply
#3
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?
Reply
#4
You need to do

ser.reset_input_buffer()
reset_input_buffer() is a method of Serial class.
Reply
#5
(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.
Reply
#6
Thank you both your help. It works now. But it prints Escape characters with the data. Is there a way to avoid that?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,987 Aug-24-2023, 07:56 AM
Last Post: gowb0w
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,071 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Excel file reading problem max70990 1 896 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Loop reading csv file problem faustineaiden 1 1,568 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  Help reading data from serial RS485 korenron 8 13,987 Nov-14-2021, 06:49 AM
Last Post: korenron
  Reading Serial data Moris526 6 5,394 Dec-26-2020, 04:04 PM
Last Post: Moris526
  Strange Problem when reading from excel faryad13 3 2,392 Nov-04-2020, 03:48 PM
Last Post: DeaD_EyE
  Reading integers from a file; the problem may be the newline characters JRWoodwardMSW 2 1,971 Jul-14-2020, 02:27 AM
Last Post: bowlofred
  Reading serial data and saving to a file Mohan 1 7,539 May-25-2020, 04:18 PM
Last Post: pyzyx3qwerty
  Problem reading data from file player1681 2 3,747 May-22-2020, 08:57 AM
Last Post: player1681

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020