how to Write Hex into serial

hi I want to write some HEX into arduino serial to be able to send command to relay board. I have tried many ways with no success, I cant understand why... this is my code

int ledPin = 13;

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

void loop(){

   Serial.print('\xFE\xC6\x01');
   digitalWrite(ledPin, LOW);

   delay(1000);
   Serial.print('\xFE\x64\x01');
   digitalWrite(ledPin, HIGH);
}

why is this code cant send the command right? and if I want to send an ascii code instead of Hex , how can I do that?

best regards

Hi hamzehhirzallah

This may help:

http://arduino.cc/en/Serial/write

Regards

Ray

Serial.print('\xFE\xC6\x01');

Can we just be clear that that is legal, but meaningless?

What exactly do you want to do?
Do you want to write ASCII hex to serial, or hex (aka binary)?

What I want to do is to send Ascii command to relay board , I bought from this link http://www.controlanything.com/Relay/Device/R410PL_802154

I contacted the support on how to send command and their response was

on your Arduino simply write these bytes out the serial port connected to the module: 254 108 1 This will turn on relay 1 on the board.

also they said, these commands are in ascii. I could not find a way to send the ascii. so I tried to send the command in Hex, I searched on the net to find the example I posted earlier.

regards

So you're saying Serial.print ("254 108 1"); doesn't work?

AWOL: So you're saying Serial.print ("254 108 1"); doesn't work?

If it doesn't, and I don't expect it to:

Serial.write(254);
Serial.write(108);
Serial.write(1);

also they said, these commands are in ascii

I really wonder.

I have triyed all the options with no success

Serial.print ("254 108 1");
Serial.write(254);
Serial.write(108);
Serial.write(1);
Serial.print('\xFE\xC6\x01');
Serial.print('FE 6C 01');

I can see there is a LED light flash when there is Serial.print , but I still cant make the board response and I have sent the command 254 108 1 on X-ctu terminal, but with no result but when I sent "FE 6C 01" in the terminal it works perfectly

I still dont under stand why sending HEX is not working

but when I sent "FE 6C 01" in the terminal it works perfectly

Then why the f**k don't you Serial.print() that string and see what happens?

One problem is that you are mixing single quotes and double quotes. Try sending all your strings with double quotes.

to sending hex over serial:

Serial.write(0xFE);

sending hex array:

byte SendData[] = {0xFE,0xC6,0x01};
Serial.write(SendData,sizeof(SendData));

hamzehhirzallah: but when I sent "FE 6C 01" in the terminal it works perfectly I still dont under stand why sending HEX is not working

Then just send that same string with the Arduino. Sending Hex is apparently working, as it works when you send it with a terminal.

So

Serial.print ( "FE 6C 01" );

hamzehhirzallah: but when I sent "FE 6C 01" in the terminal it works perfectly

Are you sending the ASCII characters "F", "E", "6", "C", "0", "1" or their numerical equivalents via some escape code in your terminal?

Is there a possibility that the baud rate on the Arduino is wrong?

Have you tried connecting your Arduino to your terminal, receive the data from the terminal into the Arduino and look at exactly what's being sent out?

Also, the Print.cpp library (which handles all the .print and .println calls) is wacky in the way it handles signed vs. unsigned characters.

Try this:

unsigned char buffer [8];
sprintf (buffer, "\xFE\x6C\x01\r\n"); // FE6C01 plus carriage return / linefeed
Serial.print (buffer);