Serial Send and Receive

Hi

I have questions, when we send or receive serial is all in binary so it doesn't matter what we write inside of parentheses (HEX,BIN, DEC)

Example
RS485.write(0x01,0xFF);
VS
RS485.write(1,255);
VS (MIX)
RS485.write(0x01,255);

and if does matter How do you do below example.

When we receive a data how do you convert it to Hex to send it back.

  if (values[1] == 5 && values[2] == 16 && values[3] == 191 && values[4] == 6  && first_time == 0 ) {

    digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit
    for (int i = 0; i < sizeof(msgOut); i++) {
      RS485Serial.write(msgOut[i]); // Send the byte back
    }
    Serial.println("-------------------SEND---------------------------");

    digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit

  }

  if (values[1] == 8 && values[2] == 254 && values[3] == 191 && values[4] == 2  && first_time == 0) {

    first_time = 1;
    Serial.println("-------------------GOT IT---------------------------");
    device_address = values[5];
    byte Address_Assignment[] = {0x7E, 0x05,device_address,0xBF,0x03,0x47,0x7E};

    digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit
    for (int i = 0; i < sizeof(Address_Assignment); i++) {
      RS485Serial.write(Address_Assignment[i]); // Send the byte back
    }
    Serial.println("-------------------SEND---------------------------");
    Serial.println(device_address);

    digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit

  }
  if (values[1] == 8 && values[2] == 17 && values[3] == 191 && values[4] == 4  && first_time == 1) {
      Serial.println("-------------------Controller Device Present Query---------------------------");
    
  }

I just Googled "decimal to hex in C" and got 24 million hits. I'll bet one of those will be what you need.

The questions is this does it matter what we send since is alway serial sending in bin

Shahreza:
I have questions, when we send or receive serial is all in binary so it doesn't matter what we write inside of parentheses (HEX,BIN, DEC)

.....

When we receive a data how do you convert it to Hex to send it back.

These two sentences seem to me to be contradictory? If you know it is all binary (and it is) I don't understand why you want to convert it.

You should realize the HEX, BIN and DEC only define how the data appears to a human. And all of them are representations of numbers using ascii characters.

You can, of course, send a number as text - the difference between .print() (which sends ascii characters) and .write() (which sends binary). Of course the ascii characters are themselves binary. For example

byte testNum = 65;
Serial.write(testNum); // sends one byte
Serial.print('\n'); // new line
Serial.print(testNum); // sends two bytes, one with the value 54 and one with the value 53.
Serial.print('\n');

If you send the data as ascii characters the receiving program will have to convert it back into a single byte value if it needs to do maths with it.

...R

Shahreza:
When we receive a data how do you convert it to Hex to send it back.

Hmmm...I thought this was also a question.

Shahreza:
When we receive a data how do you convert it to Hex to send it back.

You don't.

HEX, DEC and BIN ar just text representations of numbers. The data is just a byte and if you want to send it back you just send the received byte back.