sterretje thank you for the code. I tried it and I am having the same issue with your code and the code below. When going from a reset state the first commands response is missed, on the second data call, the last commands response is missing but on the third and greater data calls, all of the data is there except that the last commands response gets pushed in front of all others...see code and example output below.
#include <SoftwareSerial.h>
SoftwareSerial rs232(12, 14);
const byte numChars = 64;
char receivedChars[numChars];
char endStr[numChars];
boolean newData = false;
int rcvCount = 0;
char *cmds[] = {"AT+CPIN?", "AT+CSQ", "AT+COPS?", "AT+CGMI", "AT+CGMM", "AT+CGSN"};
long previousMillis = 0;
long interval = 100;
void setup() {
Serial.begin(9600);
rs232.begin(9600);
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
void gatherData() {
int x = 0;
while (x < (sizeof(cmds) / sizeof(cmds[0]))) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
Serial.write(cmds[x]);
Serial.write('\r');
x++;
}
}
}
void recvWithStartEndMarkers() {
char t;
while (rs232.available() > 0) {
t = rs232.read();
if (t == 'A') {
gatherData();
rcvCount = 0;
}
}
static boolean recvInProgress = false;
static byte ndx = 0;
char endMarker = '\r';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
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;
rcvCount++;
}
}
}
void showNewData() {
if (newData == true) {
// copy data to reply buffer
strcat(endStr, receivedChars);
receivedChars[0] = (char)0; //clear the char array
if (((sizeof(cmds) / sizeof(cmds[0])) - 1) != rcvCount) {
strcat(endStr, ",");
} else {
rs232.print("http://127.0.0.1:8080/get.php?somevar=");
rs232.println(endStr);
rcvCount = 0;
endStr[0] = (char)0; //clear the char array
}
newData = false;
}
}
I have changed the responses to A,B,C,D,E,F,G to simplify things.
Responses (code ran multiple times to show error):
The commands and their respective responses:
AT+CPIN? = A, B
AT+CSQ = C
AT+COPS? = D
AT+CGMI = E
AT+CGMM = F
AT+CGSN = G
What I am getting:
http://127.0.0.1:8080/get.php?somevar=C,D,E,F,G
http://127.0.0.1:8080/get.php?somevar=A,B,C,D,E,F
http://127.0.0.1:8080/get.php?somevar=G,A,B,C,D,E,F
http://127.0.0.1:8080/get.php?somevar=G,A,B,C,D,E,F
http://127.0.0.1:8080/get.php?somevar=G,A,B,C,D,E,F
This has been tested on an Arduino MEGA, UNO and an ESP8266, all have the same result.