Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an Arduino speaking to a Pi over a USB serial connection. The Pi tells the Arduino what to do, and the Arduino will give the PI some data if requested (like sensor data) ending with the line "ok". This Let's the Pi know the command has been executed.

I was starting to use strings for more complicated command parameters, but when i try to read the incoming serial data from the Pi via the Arduino, I am instead getting the last thing the Arduino sent to the PI, and not what the Pi is sending. Because unrelated data is getting read, the Arduino thinks that there is a constant throughput of invalid commands which are echos of the strings that it is sending out.

Here is a bit of code:

String readString; //raw output from the serial connection
String command; //command to be executed
bool execute = 0; //triggers command

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

void doneOk () {
Serial.print(ok);
}

void loop() {

   while (Serial.available()) {
   delay(10);  
     if (Serial.available() >0) {
   char c = Serial.read(); //read each character
   readString += c; //add each character to the string

   if (c == '\r') {
    //set command to read string
    command = readString;
    execute = 1; //enables the execution of a command
    readString = ""; //reset readString to blank
   }

//execute the command
if (execute == 1){
 if (command == "f\r") {
  moveForward();
  doneOk();

.. blah blah more code

Here is the whole thing on gist if that helps: https://gist.github.com/jillytot/e21fcebcbe6b4ac66ad6

share|improve this question
    
It's difficult, or impossible, to say without seeing all the code. Please make a Minimal, Complete, and Verifiable example. The data type of command and readString are useful to know. – Nick Gammon Sep 10 '15 at 20:47
    
I added the data types! Sorry bout that! Thanks! – Jillian Sep 10 '15 at 21:13
    
This sounds more like a problem with the Pi than the Arduino - the Pi is echoing back what the Arduino has sent it. Have you disabled getty on the Pi? – Majenko Sep 10 '15 at 21:14
    
And now the compilable example? Where does execute become a value other than 1 for example? However what @Majenko said may explain it. – Nick Gammon Sep 10 '15 at 21:15
1  
This is actually a pretty big program, but anything that is relevant to the serial communication has been posted. I am pretty sure i have isolated it to the Arduino's side. Maybe someone else has a thought, but thanks for trying! – Jillian Sep 10 '15 at 22:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.