We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › processing to arduino (again)
Page Index Toggle Pages: 1
processing to arduino (again) (Read 790 times)
processing to arduino (again)
Oct 7th, 2008, 7:09pm
 
hello,
i got some problems to get some integers to my new arduinoBT.
the syntax of 'my' little protocol is:
1 handshake
2 bytes (to make 1 int on the arduino)
1 handshake
2 bytes (to make 1 int on the arduino)
1 byte used as a flag
1 handshakes

here is the processing code:
import processing.serial.*;

Serial myPort;
PFont font;
byte[] arrayOne = {49,24,1,49,60,126,49,55,36,49,49};


void setup(){
 size(400, 300);
 background(0);
 font = createFont("Verdana", 14);


 println(Serial.list());
 myPort = new Serial(this, Serial.list()[Serial.list().length - 1], 115200); //the highest connected COM port is always my Arduino
 myPort.buffer(11); //buffer 10 bytes of data before calling serialEvent()
}

void draw()
{
 background(0);
 textFont(font, 14);
 text("Press 1 for message", 25, 25);

}

void keyPressed()
{
 if(int(key) == 49)
 {
   myPort.write(arrayOne);
   println("sending");
   println(arrayOne);
 }
}

void serialEvent(Serial myPort)
{
 byte[] inBuffer = new byte[11];
 while (myPort.available() > 0)
 {
   inBuffer = myPort.readBytes();
   myPort.readBytes(inBuffer);
 }
 if (inBuffer != null)
 {
   print("inBuffer ");
   println(inBuffer);
   myPort.clear();
 }
}

and here the arduino code:


byte bufferArray[10];
char index;


void setup(){
 //timer = millis();
 Serial.begin(115200);
 digitalWrite(13, HIGH); //turn on LED to indicate program has started
 delay(2000);
 digitalWrite(13,LOW);
}

void loop(){

 if(Serial.available()){  // if there is serial data to read...
   bufferArray[index] = Serial.read(); // load the current byte into the current array index location
   index++;       // advance index by one
 }
 else{           // if there is no serial data...
   index = 0;     // reset the index and wait for the next packet
 }
 if((index > 11)){  // if index is at matrix
   index = 0;     // reset the index to index location "0"
 }
 if(bufferArray[0]==49) // && bufferArray[1] == 24)
 {  
   Serial.println(bufferArray[0]);
   Serial.println(bufferArray[1]);
   Serial.println(bufferArray[2]);
   Serial.println(bufferArray[3]);
   Serial.println(bufferArray[4]);
   Serial.println(bufferArray[5]);
   Serial.println(bufferArray[6]);
   Serial.println(bufferArray[7]);
   Serial.println(bufferArray[8]);
   Serial.println(bufferArray[9]);
   Serial.println(bufferArray[10]);
   //Serial.println(bufferArray[11]);

   digitalWrite(13, HIGH); //turn on LED to indicate program has started
   delay(1000);
   digitalWrite(13,LOW);
   bufferArray[0]=-1;
 }
}

and this is what i get in processing:


Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
[0] "/dev/tty.Wuarg-Dial-UpNetworking-1"
[1] "/dev/cu.Wuarg-Dial-UpNetworking-1"
[2] "/dev/tty.Bluetooth-PDA-Sync"
[3] "/dev/cu.Bluetooth-PDA-Sync"
[4] "/dev/tty.Bluetooth-Modem"
[5] "/dev/cu.Bluetooth-Modem"
[6] "/dev/tty.ARDUINOBT-BluetoothSeri-1"
[7] "/dev/cu.ARDUINOBT-BluetoothSeri-1"
sending
[0] 49
[1] 24
[2] 1
[3] 49
[4] 60
[5] 126
[6] 49
[7] 55
[8] 36
[9] 49
[10] 49
inBuffer [0] 49
[1] 13
[2] 10
[3] 0
[4] 13
[5] 10
[6] 0
[7] 13
[8] 10
[9] 0
[10] 13
inBuffer [0] 10
[1] 0
[2] 13
[3] 10
[4] 0
[5] 13
[6] 10
[7] 0
[8] 13
[9] 10
[10] 0
inBuffer [0] 13
[1] 10
[2] 0
[3] 13
[4] 10
[5] 0
[6] 13
[7] 10
[8] 0
[9] 13
[10] 10

can anybody give me a tip why processing is printing the in buffer 6 times?
and why only the first byte arrives in good condition? the other bytes look like some bits were mixed up?

i know there are lots of these posts like mine, but i just don't understand the problem...
i'm new to processing but not to arduino and serial comunication

thanks for your answers

p.s. how can i get windows with scrollbar for code?
Page Index Toggle Pages: 1