Hex value through serial verification

Good Afternoon. I amm sending the following through serial3 to another device (display)
The value right now is let say 1023
The receiving end only gets 102.
Any idea how to verify if i am actually sendit all of bytes? (don`t have scope unfortunetly) or is it the reveiving end problem.

Thanks

void setup() {
  Serial.begin(115200);
  Serial3.begin(115200);
}


void loop() {

  int sensorValue = analogRead(A0);
  String sensorValueString = String(sensorValue);;
  byte sensorValueBytes[sensorValueString.length()];
  sensorValueString.getBytes(sensorValueBytes, sensorValueString.length());
  Serial.println(sensorValueString);



  byte hexValues[] = { 0xAA, 0x98, 0x02, 0x94, 0x00, 0xFA, 0x1a, 0x21, 0x08, 0x9e, 0x42, 0x00, 0x00, sensorValueBytes[0], sensorValueBytes[1], sensorValueBytes[2],sensorValueBytes[3], 0xCC, 0x33, 0xC3, 0x3C };  //start marker(AA), command(98), position (0294-00FA),font_id(1a), font transparent background(21), font size(08),font colour(9e42),bgk colour( 0000),Value, end markers (CC,33,C3,3C)
  int numBytes = sizeof(hexValues) / sizeof(hexValues[0]);

  // Write the hexadecimal values to serial
  for (int i = 0; i < numBytes; i++) {
    Serial3.write(hexValues[i]);
  }

  delay(100);
}

This code will send all the bytes. The problem is at the other end.

  for (int i = 0; i < numBytes; i++) {
    Serial3.write(hexValues[i]);
  }

Hint:

All of this String nonsense

  int sensorValue = analogRead(A0);
  String sensorValueString = String(sensorValue);;
  byte sensorValueBytes[sensorValueString.length()];
  sensorValueString.getBytes(sensorValueBytes, sensorValueString.length());
  Serial.println(sensorValueString);

can be replaced by this

  Serial.println(analogRead(A0));
1 Like

have you considered readBytesUntil()

So, in reply to the string.
The Serial.println() have nothing to do with the above 4 lines.
The:

int sensorValue = analogRead(A0);
  String sensorValueString = String(sensorValue);;
  byte sensorValueBytes[sensorValueString.length()];
  sensorValueString.getBytes(sensorValueBytes, sensorValueString.length());
  Serial.println(sensorValueString);

Is a conversion for the sensorValueBytes[]:

byte hexValues[] = { 0xAA, 0x98, 0x02, 0x94, 0x00, 0xFA, 0x1a, 0x21, 0x08, 0x9e, 0x42, 0x00, 0x00, sensorValueBytes[0], sensorValueBytes[1], sensorValueBytes[2],sensorValueBytes[3], 0xCC, 0x33, 0xC3, 0x3C };

Unless there is any more sophisticated method

Unfortunetly the receiver isn`t arduino based therefore i have no option other than write hex to it.

Except that those lines do generate the very String being printed.

Well, true that🤠

Why not just send this:
byte hexValues[] = { 0xAA, 0x98, 0x02, 0x94, 0x00, 0xFA, 0x1a, 0x21, 0x08, 0x9e, 0x42, 0x00, 0x00, 0x31,0x30,0x32,0x33, 0xCC, 0x33, 0xC3, 0x3C };
and see if it's received correctly? Note, I've hardcoded your four digit number, so you definitely should receive it. If not, we can investigate further with other things to try, like verifying it receives 3-digit values correctly.

It is actually a good shout.
So by sending:

byte hexValues[] = { 0xAA, 0x98, 0x02, 0x94, 0x00, 0xFA, 0x1a, 0x21, 0x08, 0x9e, 0x42, 0x00, 0x00, 0x31, 0x30,0x32,0x33, 0xCC, 0x33, 0xC3, 0x3C };  //start marker(AA), command(98), position (0294-00FA),font_id(1a), font transparent background(21), font size(08),font colour(9e42),bgk colour( 0000),Value, end markers (CC,33,C3,3C)
  int numBytes = sizeof(hexValues) / sizeof(hexValues[0]);

I get 1023 value received

...perfectly senseless frase...

Hex is only representation to human. Controller do not send "hex values", the other side do not received it... They both work with bytes.

Okay, so now, keep that line handy; generate the line you sent previously, and compare them. You choose how.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.