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!