0

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?

user28282
  • 55
  • 1
  • 5

1 Answers1

0

You have the number. It's made up of two nibbles, each representing 4 bits (1 hex character).

If you want to do a manual conversion take the first nibble (AND with 240) add it's value to "0" then do the same for the lower nibble (AND with 15).

OR

What I would do is use:

char HexString[3]; 
sprintf(HexString,"%02X",HexInt);

since you're not dealing with floats, you're good to go.

OR

edit: I recalled a printf function that we put together once before, I don't know if it would work with SoftwareSerial, so you might have to play around with it a bit, but as a 3rd option, you could try the following. I don't have access to an Arduino or compiler at the moment, otherwise I'd have a play with this right now.

Before your setup:

// Function that printf and related will use to print
int serial_putchar(char c, FILE* f) {
    if (c == '\n') serial_putchar('\r', f);
    return Serial.write(c) == 1? 0 : 1;
}

FILE serial_stdout;

In your setup, add:

// Set up stdout
fdev_setup_stream(&serial_stdout, serial_putchar, NULL, _FDEV_SETUP_WRITE);
stdout = &serial_stdout;

in your loop, change your assignment to:

HexString = printf("%02X",HexInt);

from: https://arduino.stackexchange.com/a/480/304

Madivad
  • 1,332
  • 8
  • 26
  • It should be `char HexString[3];`, you need space for the null terminator at the end of the string. And depending on the source of the data input it may make sense to either use snprintf or verify that the value is never outside the expected range. It pays to always be paranoid about buffer overflows. – Andrew Aug 01 '17 at 16:34
  • I usually have to think about these, but running a quick test in gcc confirms for a value of `0x05`, `HexString[0]='0'`, `HexString[1]='5'`,`HexString[2]=0`. – Madivad Aug 02 '17 at 00:41
  • Exactly, an array that is 3 long not the 2 long that you are allocating. – Andrew Aug 02 '17 at 09:06
  • yes. you're right... edited :) – Madivad Aug 02 '17 at 11:44
  • thank you very much for the quick respond.. I tried using the Second solution but I have a different issue now. I am actually trying to sent the HexString + another String like this `mySerial.println("GR+" + HexString);` and I get an error regarding char and string cannot be added together. – user28282 Aug 03 '17 at 06:18
  • did you solve that? If not you should ask a new question and post a link here so I can catch it. It depends, what does `HexString` now contain? guessing by the error it's a char since "GR+" is a string. In the mean time and since it's just the `.println()` you're having a problem with here, just use multiple `.print()` and a `.println()` to achieve the same thing – Madivad Aug 03 '17 at 14:49
  • @Madivad this is exactly how I solved it - I use `print("GR+")` followed by a `println(HexString)`. thank you! – user28282 Aug 08 '17 at 11:35