Create Byte from GUI input Hex code

Hello,

I am trying to have an Arduino code run on inputs from a GUI.

Part of my code involves sending serial communication to an external device that communicates with hexadecimal bytes

For example:

byte FESA[4] = {0x00, 0x54, 0x02, 0x7F};
Serial1.write(FEAUoff,4);

I want to be able to have the code adjust to whatever hexadecimal value comes in from the GUI. The 4th value of the byte is what needs to change.

The GUI sends a string of numerous characters that dictate the code, and I have substrings to pull them apart. Most of the string gets convert to integers which is straight forward, but this aspect needs to be able to replace the above "7F"

Amp1=data.substring(8,10);

So if Amp1 reads "2D", how can I update the byte FESA to become

byte FESA[4] = {0x00, 0x54, 0x02, 0x2D};

Thank you!

0x7F in the array of bytes is already a byte

Are you confusing a string of characters representing one or more HEX values with the HEX values themselves which will each be in a single byte ?

To confuse things further you give an example of using substring() which only works with Strings (capital S)

Please post small but complete sketch that illustrates what you are trying to do

Thank you for the reply,

I want the 0x7F to be a "variable" byte, i.e its value is dependent on what comes in from the GUI.

I have a large String (captial S) come in from the GUI that is constructed by a variety of user inputs, and I break it into numerous substrings. Below is the first bit of code, where it reads the String from the GUI and breaks it apart and converts the data into its appropriate use.


  if(Serial.available()){

    //READ STRING FROM GUI
    data = Serial.readString();
    d1 = data.charAt(0);
    r=data.substring(1,4);
    ot=data.substring(4,5);
    dc=data.substring(5,6);
    Channel=data.substring(6,8);
    Amp1=data.substring(8,10);
    Amp2=data.substring(10,12);
    Amp3=data.substring(12,14);
    C1=Channel.charAt(1);
    
    //CONVERT STRING INTO INTEGERS
    reps = r.toInt();
    ontime=ot.toInt();
    dutycycle=dc.toInt();
    offtime=ontime*dutycycle;
 
//Write Amplitude
    //byte FESA_1[4] = {0x00, 0x41, 0x02, 0xAmp1};
    //byte FESA_2[4] = {0x00, 0x41, 0x02, 0xAmp2};
    //byte FESA_3[4] = {0x00, 0x41, 0x02, 0xAmp3};
      Serial1.write(FESA_1,4);
      Serial2.write(FESA_2,4);
      Serial3.write(FESA_3,4);

This last part is what is wrong (clearly). I would like a way to take the Amp Strings, which are two digit Hex values, and covert to the proper input for a byte with a leading 0, so that as I change the value of Amp on the GUI, it will update on the code and subsequently the serial communication.

Once you have split the String into substrings you can convert them into ints using the toInt() function and you can store the result in a byte

i don't see a "String" function to convert a hex string to an int.

consider the code below which produces

171
 0x00, 0x54, 0x22, 0x2d
void setup () {
    Serial.begin (9600);

    int    w, x, y, z;

    String q = "ab";
    sscanf (q.c_str(), "%x", &x);
    Serial.println (x);

    const char *t = "0054222d";
    sscanf (t, "%02x%02x%02x%02x", &w, &x, &y, &z);

    char s [80];
    sprintf (s, " 0x%02x, 0x%02x, 0x%02x, 0x%02x", w, x, y, z);
    Serial.println (s);
}

void loop () {
}

Thanks!

I actually found a work around as Visual Studios does have a Hex to Dec function, so i convert the byte from Hex to Dec before it gets sent to the Arduino, and the byte[4] accepts a dec variable as an input, so I don't need to worry about the 0x with Hex. So i got it to work

Thanks for all the replies!

aren't you expecting a 2 digit hex, not a potentially 3 digit decimal #?

I can just change the String.substring(a,b) to pull out the appropriate 3 digit from the String, and then use to.Int() and byte() to change type. I have if statements in the GUI to always make sure it's 3 digits

Thanks!

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