Dear Robin,
yeah, I just changed 'k' to 'msgNr' for clarity, thanks for the suggestion. please see below my latest message, with the sender request data from the receive one message after another message.
In my original code, in fact, the sender starts sending the second message without being asked... in my code I specified the number of requested messages ... and I realized that there was a conflict in that way after trial and error.
the reason I sent "+++" is to ensure the next message is correctly sent, currently whenever I send "+++", I see on my emulator I actually send "v++", so if I directly send the actual message, the receive can't understand a thing becuase it's a wrong message....
so do you have any clue why I couldn't get the first message correct?
thank you very much
#include <Arduino.h> // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
Uart Serial2 (&sercom1, 11, 10, SERCOM_RX_PAD_0, UART_TX_PAD_2);
const int numChars = 5000;
char receivedChars[numChars];
boolean newData = false;
boolean printDone = false;
short msgNr = 0;
void SERCOM1_Handler()
{
Serial2.IrqHandler();
}
void setup() {
// initialize serial communication at 115200 bits per second: Serial1 is pin 0 and 1;
Serial1.begin(9600);
Serial2.begin(9600);
Serial1.println("<Arduino is ready>");
// Assign pins 10 & 11 SERCOM functionality
pinPeripheral(10, PIO_SERCOM);
pinPeripheral(11, PIO_SERCOM);
}
void loop() {
//send next command only if previous requested data has been completely captured and printed out, indicated by printDone == false
//this loop request the last 'k' record of data from ATM-915, and print out them one by one
if( (msgNr ==0) || (printDone == true) && (k<10) ) {
printDone = false;
Serial2.println("a");
Serial2.print("rdlfind 2 -");
Serial2.print(msgNr +1,HEX);
Serial2.print(" -");
Serial2.println(msgNr +1,HEX);
msgNr++;
}
recvWithStartEndMarkers();
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static int ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
// if (Serial2.available() > 0) {
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;
}
}
}
void showNewData() {
if (newData == true) {
Serial1.print("This just in ... ");
Serial1.println(receivedChars);
printDone = true;
newData = false;
}
}