Pages: [1]   Go Down
Author Topic: How to Send Hexadecimal value to Serial Devices  (Read 2647 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

https://docs.google.com/document/d/1HxC-Ky4KqSM6SwILC0fRDFnGGZbuPF5TaRtJrfwwFFA/edit#
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

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

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50366
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.

Code:
// 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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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

Code:
#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  ");
          }
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pretty Simple:

Code:
int hexIn = Serial.read();
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: