Recently I've started playing around with an Arduino Uno (rev 3), and I've set up a communication bridge between the Arduino and my computer using Python. So I send a command in Python ("ping" for instance), which is then read and interpreted. I've had it working before, using the Python commands to control a motor, but today it became non-responsive. However, only if I boot the serial monitor from the Arduino IDE, it suddenly accepts my commands again. Further inspection reveals that the Arduino is probably reading empty or corrupted data, interpreting it as ð, à and ÿ and such.

Here is an excerpt of the Python code I'm using:

import sys, glob, serial, time, re

class interface:

     timeout = 2    # 10 second time-out

    def __init__(self):
        port = "/dev/ttyACM0"

    def connect(self, port="/dev/ttyACM0"):
        global arduino
        arduino = serial.Serial(port, baudrate=115200, bytesize=8, parity=serial.PARITY_NONE, stopbits=1, timeout=0.1)  # Establishing connection
        time.sleep(3) # Allowing the connection to settle
        print "Connection established..."

    def ping(self):
        msg = self.listen_arduino()
        print msg
        if msg == "ping_ok":
            print "pinged"
            return True
        return False

    def listen_arduino(self):
        timeout = self.timeout
        matcher = re.compile("\n")
        buff = ""
        t0 = time.time()
        buff += arduino.read(128)

        while (time.time() - t0) < timeout and (not matcher.search(buff)):
            buff += arduino.read(128)
        return buff.strip()

And here is the Arduino C code:

String readString;

void setup() {
    Serial.begin(115200); // use the same baud-rate as the python side

void loop() {
    readString = listen(); // listen for any commands
    if (readString.length() > 0) {

        Serial.println(readString); // print the command

        if(readString == "ping") {

String listen() {
    String readString;
    while (!Serial.available());
    while (Serial.available()) {
        delay(1); // Allow the buffer to fill
        if (Serial.available() > 0) {
            char c = Serial.read();
            readString += c;
    return readString;

I suspect there is something fishy with the connection I'm making on the Python side, as the Arduino IDE serial monitor works just fine. I hope anyone can spot the problem in this code, I'm willing to provide more details on request.


  • Try with one letter. See if that helps.
    – Avamander
    Commented Sep 4, 2015 at 15:18
  • change the code to Serial.available()>0 because by default the output is -1, this could be the reason you are receiving junk values. Commented Sep 4, 2015 at 18:26

1 Answer 1


I've discovered that there was another program trying to access the Arduino, interfering with the code above and causing corruption of the signal. After terminating the intrusive program, the code worked just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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