Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Syntax >> Reading a string from serial device
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1234719208

Message started by patrick_r on 02/15/09 at 17:33:28

Title: Reading a string from serial device
Post by patrick_r on 02/15/09 at 17:33:28
Hello,

I am trying to read out the serial value from a Maxsonar-EZ1 ultrasonic device. This device sends an ASCII capital “R”, followed by three ASCII character digits representing the range in inches up to a maximum of 255, followed by a carriage return (ASCII 13). Unfortunately I'm not very savvy when it comes to traditional text programming (me using vvvv mainly). So far I'm using this sketch:

Quote:
String distance = "";

void setup() {
 Serial1.begin(9600); // MaxSonar-EZ1 connected to Serial1 Pin
 Serial.begin(9600);  // Serial set to 9600 as specified by MaxSonar. NB: There is no benefit in setting a higher rate as the EZ1 only talks @ 9600
}

void loop() {
 distance = Serial1.read();
 Serial.print("Distance: ");
 Serial.println(distance);            // print distance
}


It's not reading the Rxxx[RET] string right like this, when I declare distance as 'int' or 'byte' I get results but they're not usable. What I just want from it would be to print serially 'Rxxx'.

I'm a noob, please help or point me to what I miss here.
Thanks, Patrick

Title: Re: Reading a string from serial device
Post by barragan on 02/16/09 at 15:55:00
could you post a typical received string?

Title: Re: Reading a string from serial device
Post by patrick_r on 03/08/09 at 17:17:10
Uh, sorry for my late reply, it seem my spam filter has eaten the notification. This is the output:

Code:
?Distance:
Distance:
Distance:
Distance:
Distance:
Distance:
Distance:
Distance:
Distance:
Distance:
Distance:
Distance:
Distance:
Distance:
Distance:


Not much happening this way, the output itself is no longer than this for a minute. Sifting the examples for something adaptable, I failed making  sense of the nomenclature needed for this task  :-[

Using this sketch
Code:
String buffer = String("");
int ready = 0;
int val;

void setup() {
 Serial1.begin(9600);               // MaxSonar-EZ1 connected to Serial1 Pin
 Serial.begin(9600);                // Serial set to 9600 as specified by MaxSonar.
}

void loop() {
 val = Serial1.read();
 Serial.println(val, BYTE);
 val = 0;
 delay(50);
}


the output looks like this

Code:
ü
+
?
V
V
V
?
V
V
?
?
V
V
V
V
?
V
?
V
?
V
V
V
?
V
V
V
V
V
V
V
V
V
?
V
V
V
V
V
V
V
V
?
V
V
V
V
V
V
V
V
?
?
V
V
V
V
V
V
V
V
V
V
V
?
V
V
V
V
V
?
V
?
V
V
V
?
V
V
V
?
?
V
V
V
V
V
?
V
V
?
?
V
V
V
V
V
V
?
V
?
V
V
?
V
V
V
?
V
V
V
V
V
V
V
V
?
V
V
V
V
V
?
V
V
V
V
V
V
V
V
V
V
V
V
?
V
V
V
?
V
V
V
?
V
?
V
V
V
V
?
V
?
V
V
V
V
V
V
V
V
V
?
V
V
V
V
V
V
V
V
V
?
V
V
V
V
V
V
V
V
V
V
?
V
?
V
?
V
V
V
V
V
V
V
V
?
V
V
V
?
V
V
V
V
V
V
V
V
V
V
V
V
?
?
V
V
V
V
V
?
V
V
V
V
V
?
?
?
V
?
V
V
?
V
V
V
V
V
V
?
?
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
?
V
V
V
V
V
?
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
?
V
V
V
V
V
V
V
V
V
V
V
V
V
V
?
V
V
V
?
V
V
V
V
V
?
?
V
V
?
V
V
V
V
V
V
V
V
?
V
V
V
?
?
V
V
V
V
V
V
V
V
V
V
V
?
V
V
V
V
V
V
V
V
?
V
?
V
V
V
?
?
V
V
?
?
V
V
?
V
V
V
V
V
V
V
V
V
?
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
V
?
V
V
?
V
V
?

The values do change when the detected range changes but, I'd prefer them to be within 0 and 255 (like they'll get send by the EZ-1).

The ultimate goal would be to read three of these Sensors and send their values separated like CSV or so.

Title: Re: Reading a string from serial device
Post by patrick_r on 03/08/09 at 17:50:36
I have slightly altered my last sketch, cleaning some leftovers from previous attempts.


Quote:
int val;

void setup() {
 Serial1.begin(9600);               // MaxSonar-EZ1 connected to Serial1 Pin
 Serial.begin(9600);                // Serial set to 9600 as specified by MaxSonar.
}

void loop() {
 val = Serial1.read();
 Serial.println(val);
 val = 0;
 delay(500);
}


This is the serial output when moving an object from very close to far and back again


Code:
´?jRü103
54
54
54
54
54
54
54
54
43
54
54
54
54
54
54
86
54
54
54
54
54
43
-42
-106
86
0
-77
-77
-77
-77
-77
-77
-77
86
86
118
54
86
0
89
89
89
89
89
-39
89
89
-39
-78
-78
-78
-78
-10
-106
86
22
-42
-20
108
108
-84
99
99
86
-20
101
-78
99
-20
-78
99
-20
102
86
-84
44
-84
0
-42
-78
-39
-39
89
86
-77
-77
22
86
-10
54
54
54
54
54
54
54


Unfortunately it's expected behaviour from ultrasonic rangefinders to report erratic values (spikes) from time to time. By the way, I didn't find a description of Serial.read in the API description, so I'm unsure what the expected output is.

Title: Re: Reading a string from serial device
Post by patrick_r on 03/08/09 at 18:05:07

patrick_r wrote on 02/15/09 at 17:33:28:
This device sends an ASCII capital “R”, followed by three ASCII character digits representing the range in inches up to a maximum of 255, followed by a carriage return (ASCII 13)

If I would know what type (string, value, …) Serial.read outputs, I could wrap my head around this better I guess  :-?

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.