I am trying to read digits (number from 0 to 255) from Serial port, convert them to HEX String and send them using SoftwareSerial as HEX.
For example: when I send '60' trough the serial port, the SoftwareSerial will send '3C'
currently I am using the following code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 4); // RX, TX
String inputString = "";
boolean stringComplete = false;
int HexInt = 0;
String HexString = "00";
void setup() {
Serial.begin(115200);
mySerial.begin(38400);
inputString.reserve(200);
delay(500);
Serial.println("");
mySerial.println("");
}
void loop() {
serialEvent();
if(stringComplete) {
HexInt = inputString.toInt();
HexString = String(HexInt,HEX);
if (HexInt<16) HexString = "0" + HexString ;
HexString.toUpperCase();
mySerial.println(HexString);
Serial.println(inputString); //debug
stringComplete = false;
inputString = "";
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == '\n') continue;
if (inChar == '\r') {
stringComplete = true;
if (inputString == "") inputString = "NULL";
inputString.toUpperCase();
continue;
}
inputString += inChar;
}
}
My question is: Is there a more elegant / effective way to perform the same outcome?