Read Serial Input and Convert to Hex Byte, preserving 0x

Hey there guys,

I have been working on a project using the LoRa RA-02 (SX1278) module. I am attempting to set the SyncWord of the module, which takes in a byte anywhere from 0 to 0xFF. Default is 0x34. Anyways, I need it so when the Arduino first boots up, it asks for the SyncWord you'd like to use. For testing purposes I am just trying to use 0x34 to see if I can have it parsed and set. I have googled and been on like a hundred threads, and can't quite find what I am looking for, as it seems most people are working with byte arrays, or converting to ints, which is not what I am looking for.

Essentially I need to be able to take in 0x34 or 0xFF, that sort of hex, into the Serial Monitor, and have it saved to a byte variable in memory using the 0x format, so I can run LoRa.setSyncWord(0xFF) once the input is provided. Every example I tried converted this to an int, or an incorrect format which prevents the LoRa module from working.

Here is my current code:

Serial.println("LoRa init succeeded. Waiting for syncWord...");
  while(Serial.available() == 0){   //check if any data was received
      byte RXdata = Serial.read();
      
      if (RXdata != 255){
        Serial.println(RXdata);
        Serial.println("Sync Word Set. Launching...");
        LoRa.setSyncWord(RXdata); // ranges from 0-0xFF, default 0x34
        break;
      }
    }

Using this code, when I run my Arduino, and enter 0x34, or anything 0x, The code will never reach my

if (RXdata != 255){

clause. Nothing happens. It won't print the data I sent in, or set the sync word.
Could anybody possibly help with this? Any help is much appreciated.

  while(Serial.available() == 0){   //check if any data was received
      byte RXdata = Serial.read();

This means, "if there is no serial data available, read it".

I don't think you understand what the '0x' is for. That notation is designed to let the compiler know that you are specifying a hexadecimal constant. The setSyncWord method of LoRa accepts an integer as a parameter. That integer could be a constant or a variable. For example the following 4 code segments all yield the same result:

  LoRa.setSyncWord(0xF3);
  LoRa.setSyncWord(243);
  int syncWord = 0xF3;
  LoRa.setSyncWord(syncWord);
  int syncWord = 243;
  LoRa.setSyncWord(syncWord);

Therefore, what you need to do is convert your serial input to an integer. If you code it properly, you could choose to enter the sync word as a hexadecimal OR decimal number. The following tutorial should help you out:

Serial Input Basics

aarg:

  while(Serial.available() == 0){   //check if any data was received

byte RXdata = Serial.read();



This means, "if there is no serial data available, read it".

This is because I need it to hang the program until valid data is inputted. If I switched it around, the loop instantly ends because there may be no data for a few seconds. It breaks this loop, and takes in data once a valid integer has been provided.

ToddL1962:
I don't think you understand what the '0x' is for. That notation is designed to let the compiler know that you are specifying a hexadecimal constant. The setSyncWord method of LoRa accepts an integer as a parameter. That integer could be a constant or a variable. For example the following 4 code segments all yield the same result:

  LoRa.setSyncWord(0xF3);
  LoRa.setSyncWord(243);
  int syncWord = 0xF3;

LoRa.setSyncWord(syncWord);






int syncWord = 243;
  LoRa.setSyncWord(syncWord);




Therefore, what you need to do is convert your serial input to an integer. If you code it properly, you could choose to enter the sync word as a hexadecimal OR decimal number. The following tutorial should help you out:

[Serial Input Basics](https://forum.arduino.cc/index.php?topic=396450.0)

Thank you so much for this explanation. This helps me immensely. Thank you for the time you took to write this, and I can now continue with my project!

pablo67340:
This is because I need it to hang the program until valid data is inputted. If I switched it around, the loop instantly ends because there may be no data for a few seconds. It breaks this loop, and takes in data once a valid integer has been provided.

 while(Serial.available() == 0){   //check if any data was received
      byte RXdata = Serial.read();

Look at this closely, it basically says "while there is NO data available read a byte of data", which makes no sense. If you REALLY want to hang the program waiting on serial data then you could do this:

  while(Serial.available() == 0); // wait until we have data

  // Read data until there is no data left
  while(Serial.available() >0 ){
      byte RXdata = Serial.read();
      // TBD: process data
    }

I removed your processing code because it was wrong. You will have to collect all of the ASCII data and then convert it to an integer.

ToddL1962:

 while(Serial.available() == 0){   //check if any data was received

byte RXdata = Serial.read();




Look at this closely, it basically says "while there is NO data available read a byte of data", which makes no sense. If you REALLY want to hang the program waiting on serial data then you could do this:



while(Serial.available() == 0); // wait until we have data

// Read data until there is no data left
 while(Serial.available() >0 ){
     byte RXdata = Serial.read();
     // TBD: process data
   }




I removed your processing code because it was wrong. You will have to collect all of the ASCII data and then convert it to an integer.

Ahhh yes I see what you're saying. Redundant code = bad code. My bad on my part. Thanks again so much for this help. I appreciate it very much.

pablo67340:
Ahhh yes I see what you're saying. Redundant code = bad code. My bad on my part. Thanks again so much for this help. I appreciate it very much.

There wasn't any redundant code just code that didn't work. You will need to read the serial input until you have entered the complete number and then convert it. You could use the Serial.parseInt() method but it only allows for decimal input. You could start with that just to get everything else working and then improve the code by allowing hexadecimal input.

See my page on Arduino Software Solutions for various ways to read in data from Serial.
then at the bottom is code to convert text numbers. In the safe SafeString version you can just replace toInt() with hexToLong() to handle hex numbers.