1

I'm using a Arduino Uno Rev 3 connected over usb to a Raspberry Pi Modell B+ running Rasbian.

Here ist the arduino code:

int input = 0;
String command = ""; // beinhaltet den aktuellen befehl
int Action = 1;

void setup() {
Serial.begin (9600);
}

void loop() {
  if (Serial.available() > 0) 
  {
        input = Serial.read();
    char currentChar = input;

    if(currentChar  == '!') // frage nach trennzeichen
    {
      Serial.println(command);
      //Serial.write(Action);
      Action = command.toInt();

      command = ""; // befehl wieder leeren
    }else
    {
      command = command + currentChar ; // falls noch kein trennzeichen vorhanden, erweitere kommando
    }
    }
}

Here is the Raspberry Pi code (in python)

import serial
from time import sleep as sleep
ser = serial.Serial('/dev/ttyACM0',9600)
s = [0]
while True:
        ser.write(b'2!')
        a =  ser.read(1)
        print a
sleep(2)

It always works if once after the code was uploaded to the arduino, but if I stop the script on the pi and restart it it stops working. What is the problem here?

  • Does the PI close the serial port as it exits? If it doesn't can it open it again? – Code Gorilla Jul 12 '16 at 12:11
  • It works on my PC running Ubuntu, although it prints tons of CR and LF. You may have noticed that you are overflowing the Arduino's input buffer. – Edgar Bonet Jul 12 '16 at 12:17
  • @EdgarBonet how do I prevent the input buffer from overflowing? – jonathan Jul 12 '16 at 12:41
  • Speak slowly, e.g. by moving the sleep(2) inside the loop. You may also want to read the whole answer (3 bytes) in each iteration. – Edgar Bonet Jul 12 '16 at 12:49
  • You do know that the Uno resets, when you create a new serial connection to it, right? – Gerben Jul 12 '16 at 12:55
2

"It always works if once after the code was uploaded to the arduino...." Whenever you you reset the arduino the link between the arduino and the rasperry pi is broken momentarily thus causing the code on the raspberry pi to hang/ffreeze and not responding/ showing any valid results.

Try giving a keyboard interrupt by ( Ctrl + Z ) and restart the python script and that should work just fine!

|improve this answer|||||

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.