Say I have an ASCII string "12345678910" and I want to pull out two of the characters and turn them into the decimal number they actually represent and do some compares on them to a decimal number, what is be best approach to this?
For instance say I want to pull out the ASCII 7 and 8 above and combine them and convert them to decimal 78 and then compare that number to a range of 00-40 = shorted, 41-69 = good, 70-99 = open.
Any ideas?
Would having the string stored in a char array make this easier because I have both versions of the string available?
You can use the built in string functions to parse captured character strings. The below is for servos, but you don’t need a servo to try it to see how the string from the serial monitor is captured, split, and turned into numbers.
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
Serial.println("servo-test-21"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(10);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see results
Serial.println(servo2);
int n1; //declare as number
int n2;
char carray1[6]; //magic needed to convert string to a number
servo1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
servo2.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
}
}
Well the data I am parsing turns out it is in HEX instead of just ASCII characters.
so a sentence of my data looks like this: =00000E1F017E250100 it always starts with an = and ends with a CR &LF
I am after the the hex number 25 (hex) in this example which happens to be temperature in °C of 37 (dec)
I am already moving this sentence into a char array so how do I get only those characters and put them in a variable that can be printed as a decimal number?
I could never get the example above to work. It would not return the correct value with certain HEX combinations... 0A for example would not return 10, it would return something like 16???? I was not smart enough to figure out why this does not work so I gave up.
Anyway I figured out how to use the strtol function and it works great so far...
//string is a char array that I load my ASCII sentences in from the serial port. This works fine, the sentence data looks like this: =00000E1F017E250100
char TempData[3]; //Create array for temperature data
TempData[0] = string[13]; //Load the first ASCII representation of the first HEX number into the array
TempData[1] = string[14]; //Load the second ASCII representation of the second HEX number into the array
TempData[2] = '\0'; //Load a Null character into the array
int Base=16; //Set the base for the strol function to HEX
long int Temp; //Define the Temp Variable
Temp = strtol(TempData, NULL, Base); //Run the strol function on the TempData array and return the variable to Temp
mySerial.print(Temp, DEC); //Print TEMP
Well, I think hex is just a dual ascii character representation of a number. You could try something like below to capture the desired ascii characters and then convert them into a number. You can paste (ctrl-v) your string into the serial monitoe text box and experiment.
// zoomkat 10-29-10 simple delimited '=' string parce
// from serial port input (via serial monitor)
// and print result out serial port
// CR/LF could also be a delimiter
// for IDE 0019 and later
String readString, testhex;
void setup() {
Serial.begin(9600);
Serial.println("serial-delimit-21"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like =00000E1F017E250100
while (Serial.available()) {
delay(1); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
if (c == '=') {
break; //breaks out of capture loop at ,
}
readString += c; //makes the string readString
}
if (readString.length() >0) {
Serial.println(readString); //prints string to serial port out
// do other stuff with string here
testhex = readString.substring(12, 14); //get the first four characters
Serial.println(testhex);
readString=""; //clears variable for new input
}
}