Bright Sparks NZ
Posts: 20
Joined: Fri Dec 21, 2012 8:11 pm

Flush Serial Buffer

Wed Jul 02, 2014 6:03 am

Hi

1/. Would be interested in how to clear the input serial buffer (and output ?) as the following program example starts. When (re)starting the program the Pi seems to have happily accumulated everything and anything seen on the UART Rx line. This can be time consuming / messy esp if processing and forwarding data off the UART as we are doing to a web server etc...

import serial, time #this imports the libraries needed

port = serial.Serial("/dev/ttyAMA0", baudrate=2400)
#This sets up the serial port ttyAMA0 GPIO
#baudrate is the bits per second.

while True:
rcv = port.readline()
#read buffer until cr/lf

#if not null then...
if(rcv):
print ('Serial # = ' + repr(rcv))
#Echo the buffer bytes back to screen
#&c.

2/. Can someone indicate where and how such a clear buffer command would go in the above example so it runs once on startup. I imagine it would be somewhere between the port command and the port.readline() command I imagine.

For anyone interested the above port.readline() example works FAB with serial data arriving at the GPIO ad hoc / variable length and responds to the 13/10 LF/CR very nicely. Highly recommended when conversing and talking to picaxe etc. The read byte, read bytes and timeout functions have been 'discovered' already. These took weeks to pull out of the www all over the place and usual fault finding and time consuming trial and error. Is there a code refference for this pyserial that is easy to find and use with syntax and examples some place. I imagine there will be an 'open' and 'close' command as well that would pretty much do the same thing and keep things tidy and under control.

Thanks

~ Andrew

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Flush Serial Buffer

Wed Jul 02, 2014 6:10 am

Here you go:
http://pyserial.sourceforge.net/pyserial_api.html

flushInput() just before your while loop.

Bright Sparks NZ
Posts: 20
Joined: Fri Dec 21, 2012 8:11 pm

Re: Flush Serial Buffer

Tue Jul 08, 2014 11:51 pm

Hi Still trying to crack this one. It is most likely the mode or class (whatever that means) or environment I am running things in ? This is just to set up simple classroom demo scripts running IDLE and minimal hardware and basic concepts.

+++++++++++++++++++
import serial, time #this imports the libraries needed

port = serial.Serial("/dev/ttyAMA0", baudrate=2400)

while True:

flushinput()
#clear serial buffer to remove junk and noise

rcv = port.readline()
#read buffer until cr/lf

#if not null then...
if(rcv):
print ('Serial # = ' + repr(rcv))
#Echo the serial buffer bytes up to the CRLF back to screen

Gives

Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>

Traceback (most recent call last):
File "/home/pi/Documents/Python Pit/Serial/Read Cicadacom.py", line 14, in <module>
flushinput()
NameError: name 'flushinput' is not defined
>>>

Ideas or another work-around ?
~ Andrew

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Flush Serial Buffer

Wed Jul 09, 2014 12:05 am

flushInput() is a method of the Serial object, so in your example you'd need to use port.flushInput()
(and make sure you capitalise it correctly too!)

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Flush Serial Buffer

Wed Jul 09, 2014 1:38 am

And flushinput() must go *before* the while loop! Otherwise you will continually empty the real contents.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Flush Serial Buffer

Wed Jul 09, 2014 11:51 am

ame wrote:And flushinput() must go *before* the while loop!
LOL, I missed that!
@BrightSparks if you put

Code: Select all

[code]  
[/code] tags around your code, it'll preserve the indentation and make it much easier to read.

Return to “Python”