Hi guys!
I'm trying to make a program, where an incoming Hexadecimal value gets converted into a normal string.
I was trying to use a function for this, since I think I want the conversion to run only once. If I were to put it into the void loop(), then it would spam the serial monitor.
Here is the code:
String asciiString;
String hexString;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(115200);
delay(1000);
}
void hextostr() {
Serial.println(hexString);
for (uint8_t i = 0; i < hexString.length(); i += 2) {
String hexChar = hexString.substring(i, i+2);
char c = (char) strtol(hexChar.c_str(), NULL, 16);
asciiString += c;
}
Serial.println(asciiString);
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()) {
String data = Serial.readString();
Serial2.println(data);
}
while (Serial2.available()) {
String data = Serial2.readString();
Serial.println(data);
String hexString = data;
hextostr();
}
}
Right now, I get the received hexadecimal perfectly, and I can print it out, but for some reason, the global variable "hexString" doesn't carry it's string over to my function, and I get nothing for the translated string.
(I should add, that the translation process works perfectly fine in a separate program, I can translate any hexadecimal string to a normal string. Here is the code for that one:
void setup() {
Serial.begin(9600);
String hexString = "7465737a7465737a7465737a7465737a743132333132333b3b3b2e2e2d2d"; // your hexadecimal string
String asciiString = "";
for (uint8_t i = 0; i < hexString.length(); i += 2) {
String hexChar = hexString.substring(i, i+2);
char c = (char) strtol(hexChar.c_str(), NULL, 16);
asciiString += c;
}
Serial.println(asciiString);
}
void loop() {}
managed to solve the issue in the meantime, the problem was that the LoRa module I was using inserted a "radio rx " string before the actual data, so I had to get rid of that using string.Remove, and now its working.
However, I get this weird phantom character at the end of every translated sentence.
The fixed code:
String asciiString = "";
String hexString = "";
char c = "";
String data;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(115200);
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
asciiString = "";
hexString = "";
char c = "";
while (Serial.available()) {
String data = Serial.readString();
Serial2.println(data);
}
while (Serial2.available()) {
String data = Serial2.readString();
Serial.println(data);
hexString = data;
hexString.remove(0, 9);
hextostr();
}
}
void hextostr() {
Serial.println(hexString);
for (uint8_t i = 0; i < hexString.length(); i += 2) {
String hexChar = hexString.substring(i, i+2);
char c = (char) strtol(hexChar.c_str(), NULL, 16);
asciiString += c;
}
Serial.println("Incoming Message:");
Serial.println(asciiString);
}
The answer it gives to 737a6961:
"szia☐"
as if when you have a wrong baud rate selected
I have no idea where that box icon comes from.