Pages: [1]   Go Down
Author Topic: Reading xbee data in Processing  (Read 673 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I'm trying to send data from an xbee series 1 connected to an arduino Fio to the serial port using an xbee explorer usb. I want to use the data in processing. As a test I just I try sending "hello" with this simple example:
Code:
void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.write(45); // send a byte with the value 45
   int bytesSent = Serial.write("hello"); //send the string “hello” and return the length of the string.
}
In the X-CTU monitor everything works fine, I can read the word 'hello'. In processing however I just get the following numbers:
104
108
111
repeated over and over.
I use this code in processing:
Code:
import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
void setup()
{
  size(200, 200);
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
    println(val);
  }
 }

Does anyone know what's happening here? Any help would be greatly appreciated. Best, d
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34143
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In processing you are simply printing the value of the bytes you receive. You are not printing the ASCII code that this corresponds to. You need to do this to read the message hello.

Quote
In processing however I just get the following numbers:
104
108
111
repeated over and over.
I bet there are other numbers as well, in particular those that correspond to the letters in hello.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi thanks very much for your fast response. Where should I look to convert the bytes to ascII? TIA, danielle
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've got it! From the serial library in Processing I need to use this:
Code:
// Example by Tom Igoe

import processing.serial.*;

int lf = 10;    // Linefeed in ASCII
String myString = null;
Serial myPort;  // The serial port

void setup() {
  // List all the available serial ports
  println(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.clear();
  // Throw out the first reading, in case we started reading
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);
  myString = null;
}

void draw() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(lf);
    if (myString != null) {
      println(myString);
    }
  }
}
More info here: http://www.processing.org/reference/libraries/serial/Serial_readStringUntil_.html

Logged

Pages: [1]   Go Up
Jump to: