How to Send Hexadecimal value to Serial Devices

hi. hello. i have a question here. how i'm gonna send hexadecimal value to serial devices. i'm done a little bit study. the hex value can be send by using serial write like example below.

//Serial.write(0x55); <----right (HEX)

but how about AA BB 06 00 00 00 01 01 03 03? this is hex command to control the RFID reader. i just want to get the basic idea how to enable & disable the RFID first. if i manage to enable & disable the reader, i can do lots more things with this reader. the reader that i buy is Mifare CR038. Here is the datasheet of the reader. with all, regards.

Serial.write(0xAA);
Serial.write(0xBB);
Serial.write(0x06);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x01);
Serial.write(0x01);
Serial.write(0x03);
Serial.write(0x03);

is there any other way that so that i can write it simultaneously?

mr_hacker90:
is there any other way that so that i can write it simultaneously?

There are three overloaded implementations of Serial.write:

Serial.write(val)
Serial.write(str)
Serial.write(buf, len)

The first one sends a single byte, as johnwasser showed.

The second one sends an array of bytes, stopping when it reaches a null value in the array. (This would usually be used to send null-terminated strings.)
The third one sends an array of bytes with the number of bytes explicitly specified. This one seems to be what you want.

// uncompiled, untested
byte message[] = {0xAA, 0xBB, 0x06, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03 };

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

is there any other way that so that i can write it simultaneously?

That depends on what you mean by "simultaneously". By my definition (at the same time), the answer is no. The Arduino can not do two things at once.

That depends on what you mean by "simultaneously". By my definition (at the same time), the answer is no. The Arduino can not do two things at once.

based on the rfid user manual that i read, in order to turn off the led of the reader, i need to send this command code via serial communication.

AA BB 06 00 00 00 07 01 0 05

Packet Format (Hexadecimal): AA BB 06 00 00 00 07 01 LED XOR

AA BB = Header, 2 bytes.

06 00 = Packet length, 2 bytes, lower byte first. This indicate there are 6 bytes of data from Node ID to XOR.

00 00 = Node ID, Serial number of CR038, 2 bytes, lower byte first. 00 00 mean broadcast, it works for any ID.
07 01 = Function/Command Code Set LED status, 2 bytes, lower byte first.

XOR = result of exclusive OR operation from Node ID to LED.

i guess it not simultaneously. but in such, according to sequence in a packet.

PeterH:
There are three overloaded implementations of Serial.write:

Serial.write(val)
Serial.write(str)
Serial.write(buf, len)

The first one sends a single byte, as johnwasser showed.

The second one sends an array of bytes, stopping when it reaches a null value in the array. (This would usually be used to send null-terminated strings.)
The third one sends an array of bytes with the number of bytes explicitly specified. This one seems to be what you want.

// uncompiled, untested

byte message[] = {0xAA, 0xBB, 0x06, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03 };

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

i understand. based on the your code, it uses Serial.write(buf, len)

buf: an array to send as a series of bytes

len: the length of the buffer

understand now. thank you very much. i will try compile it.

no errors. i juz compile it. perhaps this monday i can try burn it into the uno board. tq PeterH & PaulS. finally, i got some new knowledge with your helps. really appreciate it.

by the way, how i i'm gonna read the reply from the reader. i know it is related to the serial.read
in my code. i want to read the reply from the reader when i initialize & turn the led off. suppose to be when i turn off the led, the reader would reply Command

(Hexadecimal): AA BB 06 00 00 00 01 01 03 03

Reply from CR038 (Hexadecimal): AA BB 06 00 BF FF 01 01 00 40
BF FF = Node ID or Serial number of CR038, 2 bytes, lower byte 1st.
01 01 = Function/Command Code from host, 2 bytes, lower byte 1st.
00 = Status, 1 byte. 0 mean success, other than 0 mean fail.
40 = Result of XOR operation from Node ID to Status. Try it and you will get 0x40

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);

byte initPort[] = {0xAA, 0xBB, 0x06, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03};
byte offLED[] = {0xAA, 0xBB, 0x06, 0x00, 0x00, 0x00, 0x07, 0x01, 0x00, 0x05};

void setup() {
  // initialize serial:
  Serial.begin(19200);
  
  // set up the LCD's number of columns and rows: 
  lcd.begin(8, 2);
  // Print a message to the LCD.
  lcd.print("Reader");
  
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("Port Init");
  
  // print the string when a newline arrives:
  Serial.write(initPort, sizeof(initPort));
  delay(1000);
  
  // set the cursor to column 0, line 1  
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("LED OFF ");
  delay(1000);
  
  // print the string when a newline arrives:
  Serial.write(offLED, sizeof(offLED));
}

void loop() {
}

i started with an easy example. serial.available & serial.read from arduino tutorial. i managed to use the serial.read. but only for one figure number. not in string. i stuck here. how i'm gonna read the serial reply from the reader. the reply is AA BB 06 00 BF FF 01 01 00 40

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);

char incomingByte = 0;   // for incoming serial data

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  
  // set up the LCD's number of columns and rows: 
  lcd.begin(8, 2);
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
  
      // say what you got:
      Serial.print("I received: ");
      Serial.println(incomingByte, 0);
      
      if (incomingByte=='1'){
          lcd.setCursor(0, 1);
          // print the number of seconds since reset:
          lcd.print("LED OFF ");
          }
        
      else if(incomingByte=='2'){
          lcd.setCursor(0, 1);
          // print the number of seconds since reset:
          lcd.print("LED ON  ");
          }
  }
}

Did you find out how to read hex over the serial when the machine responds?

napkinsterror:
Did you find out how to read hex over the serial when the machine responds?

Pretty Simple:

int hexIn = Serial.read();

Okay thanks, just didn't know if I had to do a read(something,HEX) thanks for the help.

PeterH:

// uncompiled, untested

byte message[] = {0xAA, 0xBB, 0x06, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03 };

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

Did it work?

Greetz Chris

PeterH:

// uncompiled, untested

byte message[] = {0xAA, 0xBB, 0x06, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03 };

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

Thanks a lot. It is truly helpful.

I am having an issue sending from the Arduino Mini to a Windows computer. I am sending:

Serial1.begin(9600);
Serial1.write(0xef);
Serial1.write(0xbe);

My windows computer consistently receives it as 0x08 then 0x90. I used both C# and a hyper term clone to capture the two bytes. Could it be an encoding issue? How do I tell Arduino code that I am using 8 bits, no parity, 1 stop bit on the Arduino?

I am using 9600 baud, 8 bit not parity, 1 stop bit on the PC.

Thanks
Kathy

napkinsterror:
Okay thanks, just didn't know if I had to do a read(something,HEX) thanks for the help.

Hexadecimal is just an easy way for you to write bytes when you type in your program. Or it is an easy way to read bytes and you can ask the program to display them that way on display devices. But beyond that it is just a number and when the processor deals with it will always be binary.

It might help one or two in this thread if some terms are made clear.

Hexadecimal is simply base 16 where decimal is base 10. Both are used in source and output to make things easier to read and write for humans.

byte myNumber = 15;

same as

byte myNumber = 0xF;

The value stored in myNumber is BINARY. The OP wants to send BINARY VALUES through serial perhaps not realizing that Serial Text is BINARY VALUES that are interpreted as ASCII Text.

Kathy, Arduino serial is by default 1 start bit, 8 data bits, 1 stop bit, no parity.

I would look at the wiring first.

Delta-G beat me to the hex!

mr_hacker90:
by the way, how i i'm gonna read the reply from the reader. i know it is related to the serial.read
in my code. i want to read the reply from the reader when i initialize & turn the led off. suppose to be when i turn off the led, the reader would reply Command

(Hexadecimal): AA BB 06 00 00 00 01 01 03 03

Reply from CR038 (Hexadecimal): AA BB 06 00 BF FF 01 01 00 40
BF FF = Node ID or Serial number of CR038, 2 bytes, lower byte 1st.
01 01 = Function/Command Code from host, 2 bytes, lower byte 1st.
00 = Status, 1 byte. 0 mean success, other than 0 mean fail.
40 = Result of XOR operation from Node ID to Status. Try it and you will get 0x40

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);

byte initPort[] = {0xAA, 0xBB, 0x06, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03};
byte offLED[] = {0xAA, 0xBB, 0x06, 0x00, 0x00, 0x00, 0x07, 0x01, 0x00, 0x05};

void setup() {
  // initialize serial:
  Serial.begin(19200);
 
  // set up the LCD's number of columns and rows:
  lcd.begin(8, 2);
  // Print a message to the LCD.
  lcd.print("Reader");
 
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("Port Init");
 
  // print the string when a newline arrives:
  Serial.write(initPort, sizeof(initPort));
  delay(1000);
 
  // set the cursor to column 0, line 1 
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("LED OFF ");
  delay(1000);
 
  // print the string when a newline arrives:
  Serial.write(offLED, sizeof(offLED));
}

void loop() {
}




i started with an easy example. serial.available & serial.read from arduino tutorial. i managed to use the serial.read. but only for one figure number. not in string. i stuck here. how i'm gonna read the serial reply from the reader. the reply is AA BB 06 00 BF FF 01 01 00 40



#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);

char incomingByte = 0;  // for incoming serial data

void setup() {
  Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
 
  // set up the LCD's number of columns and rows:
  lcd.begin(8, 2);
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
 
      // say what you got:
      Serial.print("I received: ");
      Serial.println(incomingByte, 0);
     
      if (incomingByte=='1'){
          lcd.setCursor(0, 1);
          // print the number of seconds since reset:
          lcd.print("LED OFF ");
          }
       
      else if(incomingByte=='2'){
          lcd.setCursor(0, 1);
          // print the number of seconds since reset:
          lcd.print("LED ON  ");
          }
  }
}

If you want to write good responsive communication code then you had best learn to write without using delay() or other blocking code. Those make execution jerky and can easily cause timing and timeout errors, at best you end up with a klunky sketch.

I show three addresses in my signature space below. The first teaches why and how to not use blocking code (delay, readUntil,...) to do multiple things at the same time like user I/O while something is running, a stop button or ability to change parameters during the run. The second link shows how to do serial I/O without blocking including a simple state machine example. Learn state machines!