Hi,
I’ve been trying to send data to the Arduino Mega board using Serial.available() and Serial.read(). However the data isn’t getting detected through a standard terminal program. However, when the data is sent out through the serial monitor tool of the IDE the data is getting received and displayed on the LCD.
I’ve also tested Serial.print to check the Rx, onto the terminal program. This works both on my terminal program as well as the serial monitor.serial ports i.e.: when Serial.available() is changed to Serial2.available() or Serial3.available() the data is getting received on the board so I’m sure nothing is wrong on my terminal program.
I wished to check if Serial.read() is disabled on the Arduino Mega board for terminal programs other than the Serial monitor in the Arduino IDE.
Code is as below.
#include <LiquidCrystal.h>
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
int receivedChars1;
int receivedChars2,i,y;
int ledPin = 50;
int resetPin = 48;
boolean newData = false;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
// digitalWrite(resetPin, HIGH);
lcd.begin(16,2);
pinMode(ledPin, OUTPUT);
// pinMode(resetPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
Serial.setTimeout(50);
Serial2.begin(9600);
Serial3.begin(9600);
//Serial.println("<Arduino is ready>");
}
void loop() {
recvWithStartEndMarkers();
receivedChars1= atoi(receivedChars);
//delay(100);
recvWithStartEndMarkers();
receivedChars2= atoi(receivedChars);
i=receivedChars1-receivedChars2;
y=receivedChars2-receivedChars1;
if(i>10)
{
lcd.setCursor(0,0);
lcd.print("Please Swipe ");
lcd.setCursor(0,1);
lcd.print("Card");
// digitalWrite(ledPin, HIGH);
delay(3000);
softReset();
//delay(3000);
}
[b]while (Serial.available())
{
char received = Serial.read();
[/b] // Serial.write(received);
if(received == '@')
{
lcd.setCursor(0,0);
lcd.print("Please collect ");
lcd.setCursor(0,1);
lcd.print("Goods");
digitalWrite(ledPin, HIGH);
delay(10000);
softReset();
}
else
{
lcd.setCursor(0,0);
lcd.print("Insufficient ");
lcd.setCursor(0,1);
lcd.print("Balance");
// digitalWrite(ledPin, HIGH);
delay(5000);
softReset();
}
}
}
void recvWithStartEndMarkers() {
delay(101);
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '.';
char endMarker = 'S';
char rc;
while (Serial2.available() > 0 && newData == false) {
rc = Serial2.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
newData = false;
while(Serial2.available()>0)
{
Serial2.read();
}
}
void softReset(){
asm volatile (" jmp 0");
}