How to send Hex command to serial device

Hello,
I communicating Arduino uno with radio device.
For the communication with radio, I need to send HEX commands to radio module.
can any one tell me how to transmit the Hex command to radio module?

sumisirmewar:
Hello,
I communicating Arduino uno with radio device.
For the communication with radio, I need to send HEX commands to radio module.
can any one tell me how to transmit the Hex command to radio module?

assuming you are taking about UART serial, try using

Serial.write(hex_value); //'hex_value' in the form 0xab , 'ab' ranging from 00 to FF

Hi Sherzaad,

i didnt understand, the hex values are passing to rf module or not.
the module respond, when i send the command.
and the module respond responce is in the Hex.
so can u tell me about this?

this is my code

int transfer[]={0x2B, 0x0B, 0x04, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("");
}

void loop() {
// put your main code here, to run repeatedly:
for (uint8_t i = 0; i < sizeof(transfer); i++)
{
Serial.write(transfer, sizeof(transfer));
}
delay(500);
}

sumisirmewar:
Hi Sherzaad,

i didnt understand, the hex values are passing to rf module or not.
the module respond, when i send the command.
and the module respond responce is in the Hex.
so can u tell me about this?

this is my code

int transfer[]={0x2B, 0x0B, 0x04, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("<Arduino is ready>");
}

void loop() {
  // put your main code here, to run repeatedly:
  for (uint8_t i = 0; i < sizeof(transfer); i++) 
  {
    Serial.write(transfer, sizeof(transfer));
  }
  delay(500);
}

I doubt this will work, but I really don't know because I don't know what the radio device is expecting. The way you have the array declared you will be sending an array of integers in little endian order. If that is what the radio is expecting , then fine. But I doubt it. Here is what it will actually be sending:

0x2B, 0x00, 0x0B, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00

In addition, because it is an integer array with 12 elements the length is 24. You also have a for loop that iterates the length of the array (24 times). Therefore you are transmitting 24 bytes, 24 times for a total of 576 bytes. I doubt that is what you want either.

By the way, in your example you are not transferring "hex". hex is just the notation we for a binary number. For example, 0x0B (hex) is the same as 11 decimal is the same as 00001011 binary.

Maybe you could post an example of what the radio device is expecting to receive?

ok sir
I will share u one more code...

void setup() {
Serial.begin(115200);
Serial.println("");
}

void loop() {
byte message[] = {0x2B, 0x05, 0x02, 0x00, 0x00, 0x01 };

char buf[4];
for (byte i = 0; i < sizeof(message); i++) {
Serial.write(buf, sizeof(buf));
Serial.write(0x20);

}
delay(500);
Serial.println("");
}

this is my code..
in that, the hex values i want to pass is the reset command of my rf module...

and when i pass the command on serial port
this shows on serial terminal...

11:20:17.466 ->
11:20:17.466 -> ?> ?> ?> ?> ?> ?>
11:20:17.951 -> ?> ?> ?> ?> ?> ?>
11:20:18.476 -> ?> ?> ?> ?> ?> ?>
11:20:18.963 -> ?> ?> ?> ?> ?> ?>
11:20:19.445 -> ?> ?> ?> ?> ?> ?>
11:20:19.965 -> ?> ?> ?> ?> ?> ?>
11:20:20.485 -> ?> ?> ?> ?> ?> ?>
11:20:20.953 -> ?> ?> ?> ?> ?> ?>
11:20:21.477 -> ?> ?> ?> ?> ?> ?>
11:20:21.977 -> ?> ?> ?> ?> ?> ?>
11:20:22.457 -> ?> ?> ?> ?> ?> ?>
11:20:22.963 -> ?> ?> ?> ?> ?> ?>

Please read How to use this forum - please read - Website and Forum - Arduino Forum, specifically point #7 about posting code.

You have a device and the PC connected to the serial port of the Arduino; that will usually not be a success. Please provide details of the device and please tell us which Arduino you're using. Please provide a diagram how everything is connected.

From post #4

void loop() {   

byte message[] = {0x2B, 0x05, 0x02, 0x00, 0x00, 0x01 };

char buf[4];
    for (byte i = 0; i < sizeof(message); i++) {
        Serial.write(buf, sizeof(buf));
        Serial.write(0x20);
     
    }
    delay(500);
    Serial.println("");
}

buf is an unitialised variable so your sending rubbish to the device; and you do that 6 times. Why? Don't you want to send the message instead of buf? Something like

    for (byte i = 0; i < sizeof(message); i++) {
        Serial.write(message[i]);

or

        Serial.write(message, sizeof(message));

Note that the serial monitor can't display no-printable characters (bytes with values below 0x20 or above 0x7F)

How I read the Hex Values on serial port when my rf module respond

sumisirmewar:
How I read the Hex Values on serial port when my rf module respond

Start by answering the earlier questions (quoted below)'

You have a device and the PC connected to the serial port of the Arduino; that will usually not be a success. Please provide details of the device and please tell us which Arduino you're using. Please provide a diagram how everything is connected

PS
I now see that you're using an Uno. Look at SoftwareSerial or the better alternative NeoSWSerial.