I have a Pi B reading serial data over USB from an Arduino. A python sketch logs it to file. Whenever I initiate the logging program I get a bunch of garbage characters before the real data starts to come through. How can I prevent this. I have added code that waits for the buffer to be empty before logging begins but this only works sporadically.
import serial
import time
start = str(time.time())
fname = '/home/pi/datalogs/serialLog_UNIX' + start + '.txt'
#fname = '/home/pi/datalogs/serialLog2.txt'
fmode = 'a'
readOne = 0
# Windows version
#arduino = serial.Serial('COM1', 115200, timeout=.1)
# Linux version
arduino = serial.Serial('/dev/ttyUSB0', 57600, timeout=.1)
inWait = 4
while inWait >0:
arduino.flushInput()
arduino.flush()
arduino.flushOutput()
inWait = arduino.inWaiting()
while True:
data = arduino.readline()
if readOne <0:
readOne = readOne + 1
data = ""
elif data:
print(data)
outf = open(fname,fmode)
outf.write(str(time.time()))
outf.write(',')
dataStr = str(data)
dataStr = dataStr[2:]
dataStr = dataStr[:-5]
print(dataStr)
outf.write(dataStr)
outf.write('\n')
outf.flush()
The first line of output looks like this:
1455004141.0082998,\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00ADC Range: +/- 6.144V (1 bit = 3mV)
1455004141.9993854,0,14.63,32
1455004143.4013093,2,14.43,32
1455004144.8026342,2,14.33,31
1455004146.2038803,2,14.49,31
1455004147.6060522,2,14.45,31
N.B. I have abbreviated the \x00 sequence as it is repeated a hundred times or more. You can see the end of one read before the pattern starts and you can see text - "ADC Range: +/- 6.144V (1 bit = 3mV)" - printed during void setup() of the Arduino sketch coming through immediately after the pattern and then the serial data starts coming through from void loop().
Cheers, Chris