Hello wherever you are in the world.
I am reading serial data via Serial2 from a car ECU into a string that I have got delimited by a "$" (ASCII 36).
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int buffer[100];
int count = 0;
void setup() {
// initialize serial:
Serial.begin(19200); // Used for outputting serial data to laptop for debugging
Serial2.begin(19200); // Connected to car computer
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
inputString = "36"; // clear the string:
stringComplete = false; //Reset the flag
count = 0; //Reset the array counter
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can delay response. Multiple bytes of data may be available.
*/
void serialEvent2() { //Checking serial port 2 on Mega 2560
while (Serial2.available()) {
// get the new byte:
byte inChar = Serial2.read();
// add it to the inputString:
inputString += inChar;
buffer[count] = inChar;
count++;
// if the incoming character is a $, set a flag so the main loop can do something about it:
if (inChar == '
This is what the code is outputting.
36482274181171210000242324330000235300000000000015833404000211812274128132497936
36482274181171210000242324330000235300000000000015833404000211812274128132497936
36482254181171210000242324330000235300000000000015833404000211812254128132498336
To break down the string, here is an explanation of the data.
36482274181171210000242324330000235300000000000015833404000211812274128132497936
36 48 227 4 181 1 7 121 etc etc.....
First byte = 36 (DEC, 24 HEX)
Second byte = 48 (DEC, 30 HEX)
Third byte = 227 (DEC, E3 HEX)
Forth byte = 4 (DEC, 4 HEX)
SO, byte 3 and 4 when reversed give me the battery voltage of the ECU that the serial data is coming out of.
4E3 HEX converted to decimal is 1251 divided by 100 give a voltage of 12.51 volts which is the correct value that I am after
My problem is that I have the data as a String, and no amount of strtoi() or strtoul() combinations that I try give me the expected results when I apply.
This was the code that I was using to get the bytes in the correct order, I realise that temp3 is a string and is probably not correct.
temp3 = "";
Serial.print("Battery voltage: ");
//Battery voltage code convert 4E3 from hex to decimal and then display.
temp3 += buffer[3];
temp3 += buffer[2];
Serial.println(temp3); //so you can see the captured string
The String temp3 would then equal 4E3, but I have no idea about what to do with this to get it to the float value of 12.51...
Any help would be very appreciated, as I have spent 4 days trying to nut this out, and all I have now is a massive headache.) {
stringComplete = true;
}
}
}
This is what the code is outputting.
36482274181171210000242324330000235300000000000015833404000211812274128132497936
36482274181171210000242324330000235300000000000015833404000211812274128132497936
36482254181171210000242324330000235300000000000015833404000211812254128132498336
To break down the string, here is an explanation of the data.
36482274181171210000242324330000235300000000000015833404000211812274128132497936
36 48 227 4 181 1 7 121 etc etc.....
First byte = 36 (DEC, 24 HEX)
Second byte = 48 (DEC, 30 HEX)
Third byte = 227 (DEC, E3 HEX)
Forth byte = 4 (DEC, 4 HEX)
SO, byte 3 and 4 when reversed give me the battery voltage of the ECU that the serial data is coming out of.
4E3 HEX converted to decimal is 1251 divided by 100 give a voltage of 12.51 volts which is the correct value that I am after
My problem is that I have the data as a String, and no amount of strtoi() or strtoul() combinations that I try give me the expected results when I apply.
This was the code that I was using to get the bytes in the correct order, I realise that temp3 is a string and is probably not correct.
§DISCOURSE_HOISTED_CODE_1§
The String temp3 would then equal 4E3, but I have no idea about what to do with this to get it to the float value of 12.51...
Any help would be very appreciated, as I have spent 4 days trying to nut this out, and all I have now is a massive headache.