Go Down

Topic: How to Send Hexadecimal value to Serial Devices (Read 2 times) previous topic - next topic

mr_hacker90

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.

mr_hacker90



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: [Select]

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

mr_hacker90

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.

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

Code: [Select]

#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: [Select]

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

napkinsterror

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

Go Up