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?
1 Answer 1
"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!
sleep(2)
inside the loop. You may also want to read the whole answer (3 bytes) in each iteration.