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:
    inWait = arduino.inWaiting()

while True:
    data = arduino.readline()
    if readOne <0:
        readOne = readOne + 1
        data = ""
    elif data:
        outf = open(fname,fmode)
        dataStr = str(data)
        dataStr = dataStr[2:]
        dataStr = dataStr[:-5]

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)






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

1 Answer 1


Have you disabled using the console for kernel logging, etc. (those numbers could be kernel timestamps)? Although I think that "ADC Range: " thing has to be from the Arduino itself since there's no ADC involved with the pi. So you might want to ask on Arduino.SE about that.

Anyway, make sure you've removed console=ttyAMA0,115200 from /boot/cmdline.txt (and do NOT leave any line breaks in that file!). Recently this has changed from ttyAMA0 to serial0 to make it compatible with the Pi 3; anything on a stock system that isn't tty1 and includes a baud rate after a comma (115200) is probably for a serial console.

On wheezy you also have to remove:

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

From the end of /etc/inittab. That won't be there on jessie, as the new init system (systemd) will start a login on the serial console if it exists -- or not, if it doesn't (see in particular the "Serial Terminals" section of this guide linked from that link).

You'll have to reboot after all that.

  • Its systemctl disable serial-getty@ttyAMA0.service && systemctl stop serial-getty@ttyAMA0.service to disable the serial getty on latest raspbian and archlinuxarm. Jul 8, 2016 at 20:38
  • @MichaelDaffin Actually you shouldn't have to do anything beyond removing the console from cmdline.txt; I've edited to reflect this, as I recently discovered the reason it didn't "seemed to be enabled" on the images I checked when this was first written is that systemd automatically starts a login getty if the kernel is using a serial console. I do seem to remember it having entries in /etc/systemd/system directories for this at one point so perhaps that feature was added in the recent-ish past.
    – goldilocks
    Jul 8, 2016 at 22:08
  • See this archlinux wiki page, keeping in mind that what GRUB does there is the same thing as is accomplished in cmdline.txt. Primary documention is here: freedesktop.org/software/systemd/man/… This is definitely the case on current versions of Raspbian (tested).
    – goldilocks
    Jul 8, 2016 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.