Pages: [1]   Go Down
Author Topic: Problem reading large arrays from arduino on Processing  (Read 2344 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everybody, I am having a problem with my project. To sup up i want to sent long arrays to processing to being processed there.
So I have started with my code but the code only works with the array sent to Processing is short. Once I increase the  number of elements of the array the code dosn't works how I expect.  I am using arduino DUE

First of all, my Processing code:

Code:
import processing.serial.*;

int H = 2;
int W = 10;

Serial myPort;

void setup() {
  myPort = new Serial(this, "COM5", 115200);

}

void draw() {
 
  myPort.write('F');

  byte[] inBuffer = new byte[H*W*4+4];

  while (myPort.available() > 0) {
      myPort.readBytes(inBuffer);
     if (inBuffer != null&& (inBuffer[0] == 0x46) && (inBuffer[1] == 0)) {
      print(inBuffer[H*W*4+3]);
    }
  }
}

The code sends the caracter F in order to arduino send the array. Then it comproves the first and the second byte of the array to make sure it is the data we want. And aflter that, to debug I print the last byte which is espected to be -1 (0xFF)

Here my arduino code:
Code:
#define H 2        //Used to sized the array
#define W 10

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

void loop()
{
  if(Serial.available()>0)
 {
  if('F'==Serial.read())
  {
    Serial.write(0x46);          //Header
    Serial.write(byte(0x00));    //Header
    for(int k=0;k<W*H*4;k++)
    Serial.write(8);             //Data
    Serial.write(byte(0x00));    //End
    Serial.write(0xFF);          //End  
  }  
}
}

The arduino code waits until it recives the char F and them send the headers, the data, and the end elements of the array.


So when I try the code with W and H with low numbers, processing prints -1 (the last element of the array) but when I increase W and H it doesn't work, processing print sometiems -1 sometimes 0 or 8..  The problem occurs when the string is larger than mor or less 130 bytes

What is the problem?

Thanks in advance, Aitor
      
« Last Edit: November 23, 2013, 10:06:11 am by aturcitu » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 614
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should NOT be calling functions, like strcmp(), that expect strings when what you pass it is NOT a string.

A string is a NULL terminated array of chars. What you have is NOT a NULL terminated array of chars.

The code you posted does something. You didn't bother saying what it does.

You want the code to do something. You didn't bother saying what that is.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply, I have updated the main post with more info about what the Processing code does and what my code should do!

Thank you smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have updated and simplificad the question!
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 614
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are sending data using a very slow transfer mechanism (Serial.write()) and expecting that all the data will arrive instantly. That won't happen.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't care about the time it takes.
How can I make it to go slowlier but getting the right data? I can't do it! If I use delays Processing seems to get lost!

Thanks
Logged

Pages: [1]   Go Up
Jump to: