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'm trying to send an array back and forth between Processing and an Arduino UNO. But I think data is being lost along the way. I simply iterate over the array in Processing and send the Values separately. A new set of data is indicated by the character <. On the Arduino the data gets added to a string and sent back for testing. When I print out the string in processing, some data changed to -1.

The array contains {1,2,3,4,5,6,7,8}

The Output looks like this:

12345678
12345678
1234-1-1-1-1
12345678
12345-1-1-1

Is there something I'm missing in terms of implementation, or is this simply a hardware related problem (like slightly different clock speeds)? Is there any way to improve this?

Arduino Sketch:

int srr[8];

int content; // Data received from the serial port
void setup() {
  Serial.begin(9600); // Start serial communication at 9600 bps
}

 void loop() {
   String str = "val:";
   int test;
   if (Serial.available()){ // If data is available to read,
     content = Serial.read();
   }

   if (content == 60){
     for(int i=0; i<=7; i++)
   {
       str+=Serial.read();
   }
   Serial.println(str);
   }
}

Processing

Serial myPort;  // Create object from Serial class
int arr[]={1,2,3,4,5,6,7,8};  //The array to send

String str;
void setup() 
{
  size(200,200); //make our canvas 200 x 200 pixels big
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  //write
  myPort.write("<");
  for (int i =0; i<=7;i++){
    myPort.write(arr[i]);

  }
  //read
  if ( myPort.available() > 0){  // If data is available,
    str = myPort.readStringUntil('\n');         // read it and store it in val
    println(str); //print it out in the console
  } 

}

share|improve this question

migrated from electronics.stackexchange.com Jun 16 '15 at 20:26

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

up vote 1 down vote accepted

Ok, let's look at your Arduino program a bit at a time (for that is where the problem lies).

if (Serial.available()) { // If data is available to read,
    content = Serial.read();
}

If there is at least one character available in the buffer then read one character into the variable content.

if (content == 60) {

If the variable content is 60 (better to use '>' so you can see what it is), then ...

 for(int i=0; i<=7; i++) {
     str+=Serial.read();
 }

... read 8 characters from the serial buffer whether there are any characters in there or not.

That last bit is the crunch point. You have an unknown number of characters in the buffer. You had at least one, and that one you read into content. You don't then check to see if there are the required 8 more characters available (or wait in each iteration of the loop for each character to actually arrive) before trying to read that character in.

If you take a moment to read the documentation on Serial Read you will see this:

Returns

the first byte of incoming serial data available (or -1 if no data is available) - int

The simplest fix is just to make your loop blocking so it waits for a character to arrive before reading it:

for(int i=0; i<=7; i++) {
     while (!Serial.available());  // Wait for a character to arrive
     str+=Serial.read();  // Read it
}
share|improve this answer

Your Answer

 
discard

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

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