Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpSyntax › Reading a string from serial device

Page Index Toggle Pages: 1
Reading a string from serial device (Read 10734 times)
02/15/09 at 17:33:28

patrick_r   Offline
YaBB Newbies
switched on …
Frankfurt am Main

Posts: 10
*
 
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
} [/quote]

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
« Last Edit: 02/16/09 at 01:33:25 by patrick_r »  
IP Logged
 
Reply #1 - 02/16/09 at 15:55:00

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
could you post a typical received string?
 
IP Logged
 
Reply #2 - 03/08/09 at 17:17:10

patrick_r   Offline
YaBB Newbies
switched on …
Frankfurt am Main

Posts: 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  Embarrassed

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.
 
IP Logged
 
Reply #3 - 03/08/09 at 17:50:36

patrick_r   Offline
YaBB Newbies
switched on …
Frankfurt am Main

Posts: 10
*
 
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.
 
IP Logged
 
Reply #4 - 03/08/09 at 18:05:07

patrick_r   Offline
YaBB Newbies
switched on …
Frankfurt am Main

Posts: 10
*
 
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  Huh
 
IP Logged
 
Page Index Toggle Pages: 1