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 › Problems reading Serial in Processing
Page Index Toggle Pages: 1
Problems reading Serial in Processing (Read 3554 times)
Problems reading Serial in Processing
Apr 4th, 2009, 9:59am
 
Hi,

I realise this is probably a very basic question but was hoping someone could help as I have been struggling with it for sometime. I have a SRF05 sensor (http://www.robot-electronics.co.uk/htm/srf05tech.htm) and have used the example code (http://www.robot-electronics.co.uk/files/SRF05.pde) in Arduino. This works great and returns expected values in the Arduino program when using the serial monitor.

I am now trying to get those same values in Processing. I am no longer running Arduino as the program is uploaded to the board. I have tried using the SerialRead example from the library but cannot seem to return any similar values. I would be grateful if someone could help or point me in the right direction here.

Help much appreciated.
Re: Problems reading Serial in Processing
Reply #1 - Apr 5th, 2009, 9:43am
 
Hi,

did you try to put an delay before reading the serial data? I e.g. got one of the early diecimilas where i need to wait 8seconds before being able to request data...
Re: Problems reading Serial in Processing
Reply #2 - Apr 6th, 2009, 6:40am
 
Thanks for the reply, but Im not sure that was the issue (I've got one of the new Arduinos).

I've managed to get the values into Processing with the following code -

Code:
import processing.serial.*;

Serial myPort;

void setup() {
 size(400, 300);
 background(0);
 smooth();
 String portName = "/dev/tty.usbserial-A7006SKu";
 myPort = new Serial(this, portName, 9600);
}

void draw() {
 delay(100);
 String inBuffer = myPort.readString();
 if (inBuffer != null) {
  println(inBuffer);
 }
}


Does anyone know of a way to extract the int values from the string?

Cheers.
Re: Problems reading Serial in Processing
Reply #3 - Apr 6th, 2009, 7:56pm
 
I have the Arduino mark each serial output with a letter character. This is the Arduino code:
Code:
void loop(){
// read the analog input on pin 0:
analogValue = analogRead(0);
if (abs(analogValue-lastValue) > analogDelta) {
Serial.print(analogValue);
Serial.print("A");
lastValue = analogValue;
digitalWrite(ledPin, HIGH);
}
if ( analogValue == 0 & lastValue != 0) {
Serial.print(analogValue);
Serial.print("A");
lastValue = analogValue;
digitalWrite(ledPin, HIGH);
}
if (analogValue == 1023 & lastValue != 1023){
Serial.print(analogValue);
Serial.print("A");
lastValue = analogValue;
digitalWrite(ledPin, HIGH);
}
delay(10);
digitalWrite(ledPin, LOW);
}

In this scheme each analog output data is followed by character A. Here the interest is data change, not data over time. So this code writes to serial only when there is a data change. Not knowing how to handle 0 and 1023 is taken care of by the two crude exception if statements.

The following seems to work on the Processing side where MARKER is 65 for the letter A:
Code:

void setupSerial(){
port = new Serial(this, Serial.list()[2], 14400);
port.bufferUntil(MARKER);
}
void serialEvent(Serial port) {
dataStr = (port.readStringUntil(MARKER));
if (dataStr.length() >=2){
dataStr = dataStr.substring(0, dataStr.length()-1); // strip off the last char
potV = float(dataStr)*litersPerDataVal;
if ((dataStr != null) & (curTest < qtyTests)) { // ie stop after last test
arypotV[curTest] = potV;
if(arypotV[curTest] > arypotMax[curTest]){
arypotMax[curTest] = arypotV[curTest];
}
}
}
}


The serialEvent seems to occur autonomously whenever there is data keeping the data current enough for the rest of the application. I think the port.bufferUntil(MARKER) keeps the data from piling up. That used to happen before using bufferUntil.
Re: Problems reading Serial in Processing
Reply #4 - Apr 11th, 2009, 4:37am
 
Thanks for the reply nutmeg, unfortunately I was unable to implement your example into my own code and get it working.

Here's what I've got so far -

Code:
/*
Arduino example for SRF05
Using a single pin for both trigger and echo.
*/

int duration; // Stores duratiuon of pulse in
int distance; // Stores distance
int srfPin = 2; // Pin for SRF05

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

void loop(){
pinMode(srfPin, OUTPUT);
digitalWrite(srfPin, LOW); // Make sure pin is low before sending a short high to trigger ranging
delayMicroseconds(2);
digitalWrite(srfPin, HIGH); // Send a short 10 microsecond high burst on pin to start ranging
delayMicroseconds(10);
digitalWrite(srfPin, LOW); // Send pin low again before waiting for pulse back in
pinMode(srfPin, INPUT);
duration = pulseIn(srfPin, HIGH); // Reads echo pulse in from SRF05 in micro seconds
distance = duration/58; // Dividing this by 58 gives us a distance in cm
Serial.println(distance, DEC);
delay(10); // Wait before looping to do it again
}


Then in Processing -

Code:
import processing.serial.*;

Serial myPort;

void setup() {
size(400, 300);
background(0);
smooth();
String portName = "/dev/tty.usbserial-A7006SKu";
myPort = new Serial(this, portName, 9600);
}

void draw() {
delay(100);
String inBuffer = myPort.readString();
if (inBuffer != null) {
println(inBuffer);
}
}


The string in Buffer is the correct values and prints fine but I'm unable to convert it into a usable Integer. I've tried Integer.parseInt().

Any ideas would be much appreciated as I'm very keen to get this working. Thanks.
Re: Problems reading Serial in Processing
Reply #5 - Apr 13th, 2009, 4:42am
 
nutmeg I was hoping you could maybe explain some of your code to me or point me in the direction of a tutorial maybe? I understand the concept of putting in a marker and managed this in Arduino but I am getting lost in the Processing side.

Code:
#define echoPin 2		 // the SRF04's echo pin
#define initPin 3 // the SRF04's init pin
int distance;
unsigned long pulseTime = 0; // variable for reading the pulse

void setup() {
// make the init pin an output:
pinMode(initPin, OUTPUT);
// make the echo pin an input:
pinMode(echoPin, INPUT);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {
// send the sensor a 10microsecond pulse:
digitalWrite(initPin, HIGH);
delayMicroseconds(10);
digitalWrite(initPin, LOW);

// wait for the pulse to return. The pulse
// goes from low to HIGH to low, so we specify
// that we want a HIGH-going pulse below:

pulseTime = pulseIn(echoPin, HIGH);
distance = pulseTime/58;

// print out that number
Serial.print(distance);
Serial.print("A");
delay(100);
}


Now in Processing when I print my buffer I get numbers followed by 'A'. If I begin to add the following code I don't get any data when printing the buffer. Here is where I get lost.

Code:
import processing.serial.*;

Serial myPort;
int MARKER = 65;
String inBuffer;

void setup() {
size(400, 300);
background(0);
smooth();
String portName = "/dev/tty.usbserial-A7006SKu";
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil(MARKER);
}

void seriaEvent(Serial myPort) {
delay(100);
inBuffer = myPort.readStringUntil(MARKER);
if (inBuffer != null) {
println(inBuffer);
}
}
Re: Problems reading Serial in Processing
Reply #6 - Aug 4th, 2009, 4:52pm
 
Hey guys,

I had similar problems as well so I wrote all this up at:

luckylarry.co.uk/2009/08/arduino-processing-getting-values-from-srf05-ultrasound
-sensor-serial-port/

This seems to be working fine for me and you can of course alter my code for your SRF05 setup - you use mode 2 by the looks of it. sorry would make that an active link but this is my first post Smiley

I found most if not all problems were on the Processing side, just because of the values expected once you place limitations and checks for this it works fine (at least for me!)
Page Index Toggle Pages: 1