Hello all,
This is a carry over from another thread i started but the conversation is starting to deviate.
I have an arduino uno communicating serially with an RF modem. At this point I'm basically issuing it some AT commands and looking at the response. The responses come on multiple lines with terminators.
Code:
#include <SoftwareSerial.h>
SoftwareSerial monSerial(10, 11); //Rx, Tx
void setup() {
Serial.begin(38400);
Serial.setTimeout(500);
monSerial.begin(38400);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
readSer();
checkConn();
Serial.print("AT+CNUM?\r\n");
delay(100);
readSer();
delay(5000);
}
void readSer() {
const int buffSize = 50; //Set buffer size
char sData[buffSize]; //Array for storing serial data
char sChar; //Character read from serial
int len =0; //length of what's in buffer
while(Serial.available() > 0) //only read if data
{
sChar = Serial.read(); //Read it
if ((sChar == '\r') || (sChar == '\n')) {
//EOL
if (len > 0) {
sParse(sData);
}
len = 0;
}
else {
if (len < buffSize) {
sData[len++] = sChar; //append recieved char to array
sData[len] = '\0'; // append null terminator to end
}
}
}
}
void sParse(char *msg) {
const char * rsp = "+CNUM: 0,";
char * p;
int ID;
p = strstr(msg, rsp);
if (p) {
char *ISSI;
ISSI = strrchr(p, ',');
ISSI++;
ID = strtoul(ISSI, NULL, 16);
monSerial.println(ID);
}
rsp = "+CTSDSR:";
p = strstr(msg,rsp);
if (p) {
monSerial.println("Status Received");
readSer() feeds the incoming data into a char array and once it his a or it will send it to sParse(). Eventually sParse will be evaluating the data on multiple criteria.
As it is now I'm just looping through making sure the modem is connected. Occasionally the modem will receive a message from another modem which is why i call readSer() at the beggining of the loop. in sParse(), I'm looking for a line that has +CTSDSR
The actual message from the modem looks like this
+CTSDSR:13,0,1010,0,1052,16<cr><lf>
814D<cr><lf>
My problem is that the data of interest (814D) happens on the next line after what's caught by my if statement. But because my readSer() function is stripping out the cr/lf and passing the data one line at a time i can't get it. The message can change
I tried calling readSer() from inside the if statement to get the next line, it compiled but nothing happened so i can only assume that's not proper... Since I know from the message that what I want is on the next line so could do Serial.read, but then i have to make another buffer to read the data which makes the readSer() function redundant...
I'm sure this is childs play for some but at this point my brain is melting trying to figure out the best way to approach this.