Sending hexa decimal values through serial port of Arduino

Hi all,

I am using MFRC522 RFID reader for my project. I am receiving the card number using arduino nano and convert it as wiegand signal. But I want to communicate with the door controller using RS485 protocol. For this purpose
I want to receive and send hexadecimal values through serial port of Arduino .The protocol contains hexa decimal values with start and end sentinel.

Can someone help me with examples how to send hexa decimal values.

Thanks,

Do you want to send the ASCII representation of the HEX values such as "A3" or the binary value such as 0xA3 ?

Serial.println("A3");
//or
Serial.println(0xA3);
//or even
Serial.println(163);  //which is the same as 0xA3

There are not "hexa decimal values" at all. Hex format is a representation to human only. Hex, decimal and binary numbers are perfectly the same for controller.
Therefore, you don't need to convert your values to hex to sending it by RS485 link

please provide a datasheet wherein the protocol is described, so we can see what is actually required.

Additionally post a sequence of what you believe that you want to send to the "door controller" including the start and end sentinel.

Basically

Serial.write(0xA3); will send one byte whose value is 163 in decimal over the Serial line whereas Serial.print(0xA3); will send the ASCII representation of 0xA3 in decimal over the serial line so you’ll get 3 bytes the first one with the ascii code for ‘1’, then the ascii code for ‘6’ and then the ascii code for ‘3’

Print can also send the ascii representation in hexadecimal. Use Serial.print(0xA3, HEX); and it will send the ASCII representation of 0xA3 in hexadecimal over the serial line: you’ll get 2 bytes the first one with the ascii code for ‘A’, then the ascii code for ‘3’

None of these send the leading text 0x so if you want it you need to add it yourself

The reader will receive hex values like 07,42,31,46,31,30,37,0D ,where 07 is start sentinel and end sentinel is 0D.

please post the protocol.

try this to understand the output of Serial Data:

uint8_t message[]{0x07,0x42,0x31,0x46,0x31,0x30,0x37,0x0D};

void debug(){
  for (auto &i : message){
    Serial.write(i);       // what the device might expect

    // and now just for demo to see something in the serial monitor
    Serial.print(" "); 
    Serial.print(i);       // just for you
    Serial.print(" ");
    Serial.print(i, HEX);  // just for you
    Serial.println();
  }
}

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

}

void loop() {
  // put your main code here, to run repeatedly:

}

will bring

 7 7
B 66 42
1 49 31
F 70 46
1 49 31
0 48 30
7 55 37
 13 D

where the first byte 0x07 and the last 0x0D are not printable characters - hence you don't see them on the built in Serial Monitor.

Thanks. What should I do for receiving hexadecimal values through serial port of arduino

Are you didn't understand the answers? There are no "hexadecimal values"... You should make it clear whether you want the values as bytes or as a text string. This is the main thing, and not that they are hex or not

I need it as byte array.

Please show the code how do you obtained your data from RFID reader

Also please describe your project. If you need to receive the RFID from RFC522 and send it via RS485 - what all this has to do with Serial?

@renicr
A Serial.read() will read the value into a variable.

Please check the Serial Input Basics - updated how to read reliable from Serial.

please provide a datasheet wherein the protocol is described, so we can see what is actually required. (3rd time).

Data Format:

Serial hardware port set up – E,8,1, 19200 (Even parity, 8 bit, 1 stop bit, 19,200 baud rate)

Data Packet:

The general protocol format is as follows:-

SS RDR ADDRESS COMMAND DATA CC1 CC2 ES

SS- start sentinel =07
RDR - reader type = can vary 40 to 45
Address- Reader address can vary from 30 to 38
Command-
Data-
CC1 and CC2- checksums
ES- end sentinel = 0D

These are all hexadecimal numbers.

I have gone through "Serial Input Basics- updated" by Robin 2, It is really a great tutorial. But in the example 6 sending data as binary values ,it shows start and end sentinel as '<' and '>'. But if I want to send binary values as start and end sentinel how can I modify this 6th example?

I am developing a mifare RFID reader with Wiegand and RS485 output. Using MFRC522 reader module and Arduino Nano for processing the received data. For RS485 part I am using MAX485 chip for converting the data from Arduino (TXD and RXD pin) . Reader communicate with door access controller by sending binary data.
So I believe I have to serial write and read for achieving this.

you are mixing up things. '<' and '>' are used in example 3. in Example 6 the markers are already defined as number.

void recvBytesWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    byte startMarker = 0x3C; // --> change it  to 0x07
    byte endMarker = 0x3E;  // --> change it to 0x0D
    byte rb;

in the end it doesn't matter if you assign a variable with the ASCII representation in ' ', in dec or in hex - it is all the same for the controller.

void recvBytesWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    byte startMarker = 0x3C; // --> change it  to 0x07
    byte endMarker = 0x3E;  // --> change it to 0x0D
    byte rb;

How to test this in serial monitor after changing the startMarker and endMarker?

With example 6 I can test it like below as 0x3C is "<" and 0x3E is ">"

image

consider

void setup()
{
  Serial.begin(115200);
}
void loop()
{
  if (Serial.available() > 0)
  {
    static unsigned long readValue = 0;
    char readChar = Serial.read();
    switch (readChar)
    {
      case '<':
        Serial.println("start");
        readValue = 0;
        break;
      case '0' ... '9':
        readValue = readValue * 0x100 + readChar;
        break;
      case '>':
        Serial.println("end");
        Serial.print("0x"), Serial.println(readValue, HEX);
        break;
    }
  }
}
//------------------------------------------------------------

Have a nice day and enjoy coding in C++.

You need another terminal software which can send not only ASCII letters.
See coolterm or hterm or similar.

Or adopt the sketch in #7 and use another Arduino as sender.