I have a serial sensor who is sending data like e.g. is running time/uptime since new battery.
This value comes to the serial terminal as: C90000E8E533
To get the actual running time we should remove the "identifier" who explains what kind of status message this is and remove the checksum. In this case C9 is the identifier part and 33 is the checksum.
The "core" running time" is 0000E8E5 which in DEC gives us 59621. So our device is running for 59621 quarterseconds, so a 4.14 hours of running time.
My function gives this, so incorrect data. 100% sure my own fault.
call operating_time
data HEX:
35
data DEC:
53
data:
5
Running Time:
C90000E8E533
I think it is because of the way that I want to construct the value from the data_array.
data = String(data_array[2,3,4,5,6,7,8,9], HEX);
Anyone a good pointer or some tips to steer me to the right functions to do this?
Thanks.
#include <SoftwareSerial.h>
#define TX D6
#define RX D7
SoftwareSerial mySerial(RX, TX); // RX, TX
bool data_chk = false;
//byte data_array[20];
char data_array[20];
char optime[8];
byte stx[] = {0x02}; //stop byte
byte etx[] = {0x03}; //start byte
byte ack[] = {0x06}; //ack byte
byte cmd1[] = {0x30, 0x34, 0x36, 0x34}; //0464
byte cmd2[] = {0x30, 0x38, 0x36, 0x38}; //0868
byte cmd3[] = {0x30, 0x39, 0x36, 0x39}; //0969
byte cmd4[] = {0x30, 0x42, 0x37, 0x32}; //0B72
byte cmd5[] = {0x30, 0x43, 0x37, 0x33}; //0C73
byte cmd6[] = {0x30, 0x44, 0x37, 0x34}; //0D74
byte cmd7[] = {0x30, 0x45, 0x37, 0x35}; //0E75
byte cmd8[] = {0x30, 0x46, 0x37, 0x36}; //0F76
byte rcvd = 0;
byte count = 0;
String data;
String data2;
String data3;
String data4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
mySerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (mySerial.available())
{
while (mySerial.available() > 0)
{
byte rcvd = mySerial.read();
//rcvd |= mySerial.read();
//Serial.println(rcvd);
if (rcvd == stx[0] && (data_chk == false))
{
data_chk = true;
count = 0;
}
else if ((rcvd == etx[0]) && (count > 10))
{
data_chk = false;
mySerial.write(ack[0]);
if (data_array[0] == 'C' && data_array[1] == '9') // for operating time
{
Serial.println("call operating_time");
operating_time();
}
Serial.println(""); // print newline
for (int j = 0 ; j < count; j++)
{
Serial.print(data_array[j]); // print data from array, this is working and the "right answer"
}
count = 0;
// break;
}
else if (data_chk == true)
{
// Serial.write(char(rcvd));
data_array[count] = rcvd;
// Serial.write(data_array[count]);
count++;
}
}
}
if (Serial.available())
{
while (Serial.available() > 0)
{
//mySerial.write((char)Serial.read());
char data = Serial.read();
if (data == 'A')
{
mySerial.write(stx[0]);
mySerial.write(cmd1[0]);
mySerial.write(cmd1[1]);
mySerial.write(cmd1[2]);
mySerial.write(cmd1[3]);
mySerial.write(etx[0]);
}
else if (data == 'B')
{
mySerial.write(stx[0]);
mySerial.write(cmd2[0]);
mySerial.write(cmd2[1]);
mySerial.write(cmd2[2]);
mySerial.write(cmd2[3]);
mySerial.write(etx[0]);
}
else if (data == 'C')
{
mySerial.write(stx[0]);
mySerial.write(cmd3[0]);
mySerial.write(cmd3[1]);
mySerial.write(cmd3[2]);
mySerial.write(cmd3[3]);
mySerial.write(etx[0]);
}
}
}
}
void operating_time()
{
// String data = String(data_array[2], HEX);
// Serial.println(data);
// data = data + String(data_array[3], HEX) + String(data_array[4], HEX) + String(data_array[5], HEX);
// Serial.println(data);
// data = data + String(data_array[6], HEX) + String(data_array[7], HEX) + String(data_array[8], HEX) + String(data_array[9], HEX);
// Serial.println(data);
data = String(data_array[2,3,4,5,6,7,8,9], HEX);
Serial.println("data HEX: ");
Serial.println(data);
data2 = String(data_array[2,3,4,5,6,7,8,9], DEC);
Serial.println("data DEC: ");
Serial.println(data2);
data3 = String(data_array[2,3,4,5,6,7,8,9]);
Serial.println("data: ");
Serial.println(data3);
// invalid conversion from 'char' to 'const char*' [-fpermissive]
// data4 = strtoul(data_array[2,3,4,5,6,7,8,9], 0, 16);
// Serial.println(data4);
Serial.print("Running Time: "); // in quarter seconds
// to do: divide decimal number / 4 / 60 / 60 to get running time in hours
// for (int i=2; i<10; i++) {
// optime[i-2] = data_array[i]; // assign the members one at a time
// }
//
// for (int k = 0 ; k < 8; k++)
// {
// // mySerial.print(data_array[k]);
// Serial.print(optime[k]);
// }
// Serial.println("");
// Serial.println(optime);
}
Serial_COM_sensor.ino (4.06 KB)