Hey everyone, I'm a long time lurker but a first time poster.
I'm working on a project using arduino mega's where I will have a module sending data to the main user interface module via serial. I have the serial string divided in to six sections with a long integer in each section. The digits of the long integer will change depending on what data needs to be sent to the user interface module.
The problem that I'm having is when the "user interface" mega receives the data and performs the strncmp function it will only pass the strncmp 4-5 times after the mega first powers up and after that it fails causing it to not process the data. I'm able to view the serial data being sent to the "user interface" mega via pluggy and can see that there shouldn't be an issue with the data coming in.
The below serial read code is modified from a code that I previously used for process data from a gps receiver that I found online. The code worked for processing the gps data so I'm not sure what I'm doing wrong that is causing an issue with this code.
I have also uploaded screen shots of the serial monitor showing that the strncmp fails and of pluggy showing the continuous data stream coming in to the "user interface" mega.
The only thing that I can say has changed from the strncmp code from when I used it on a teensy to read gps data is the characters listed in the strncmp if statement and the code inside the if statement on what to do with the indexed data.
Any help and advice with what I'm doing wrong would be greatly appreciated.
This is the code that I'm using to generate the serial data.
int d1 = 0;
int d2 = 0;
int d3 = 0;
int d4 = 0;
int d5 = 0;
int d6 = 0;
unsigned long time;
unsigned long podtime = 0;
unsigned long dout = 0;
int dnumber = 1;
void setup() {
Serial2.begin(9600);
}
void loop() {
Datacalc();
Dataout();
}
void Datacalc(){
dout = dnumber;
if(d1 == 0){
unsigned long j1calc = dout * 10;
dout = j1calc;
}
else{
unsigned long j1calc = dout * 10 + 1;
dout = j1calc;
}
if(d2 == 0){
unsigned long j2calc = dout * 10;
dout = j2calc;
}
else{
unsigned long j2calc = dout * 10 + 1;
dout = j2calc;
}
if(d3 == 0){
unsigned long j3calc = dout * 10;
dout = j3calc;
}
else{
unsigned long j3calc = dout * 10 + 1;
dout = j3calc;
}
if(d4 == 0){
unsigned long j4calc = dout * 10;
dout = j4calc;
}
else{
unsigned long j4calc = dout * 10 + 1;
dout = j4calc;
}
if(d5 == 0){
unsigned long j5calc = dout * 10;
dout = j5calc;
}
else{
unsigned long j5calc = dout * 10 + 1;
dout = j5calc;
}
if(d6 == 0){
unsigned long j6calc = dout * 10;
dout = j6calc;
}
else{
unsigned long j6calc = dout * 10 + 1;
dout = j6calc;
}
}
void Dataout(){
time = millis();
unsigned long odtime = time;
if((odtime - podtime) >= 1000){
Serial2.print("$TDS,");
Serial2.print(dout);
Serial2.print(",");
long dout2 = dout + 1000000;
Serial2.print(dout2);
Serial2.print(",");
long dout3 = dout2 + 1000000;
Serial2.print(dout3);
Serial2.print(",");
long dout4 = dout3 + 1000000;
Serial2.print(dout4);
Serial2.print(",");
long dout5 = dout4 + 1000000;
Serial2.print(dout5);
Serial2.print(",");
long dout6 = dout5 + 1000000;
Serial2.println(dout6);
podtime = odtime;
}
}
This is the code that I'm using to receive the data
boolean dataLock = false;
boolean newdata = false;
int charCount = 0;
char inputBuf[255];
byte tIndex[16];
long d1 = 0;
long d2 = 0;
long d3 = 0;
long d4 = 0;
long d5 = 0;
long d6 = 0;
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
}
void loop() {
Dataread();
Printserial3();
}
void Dataread(){
while(Serial3.available()) {
char ch = Serial3.read();
if(ch != '\n') {
inputBuf[charCount] = ch;
charCount++;
inputBuf[charCount] = 0;
}
if(ch == '\n') {
Serial.println("Serial3 Available");
dataLock = true;
if(strncmp(inputBuf,"$TDS",4) == 0){
int commaCount = 0;
Serial.println("String Compare Good");
for(int i = 0; i < 254; i++){
if(inputBuf[i] == ',') {
tIndex[commaCount] = i + 1;
inputBuf[i] = 0;
commaCount++;
}
else if(inputBuf[i] == '*'){
inputBuf[i] = 0;
break;
}
}
d1 = atol(&inputBuf[tIndex[0]]);
d2 = atol(&inputBuf[tIndex[1]]);
d3 = atol(&inputBuf[tIndex[2]]);
d4 = atol(&inputBuf[tIndex[3]]);
d5 = atol(&inputBuf[tIndex[4]]);
d6 = atol(&inputBuf[tIndex[5]]);
newdata = true;
}
}
}
}
void Printserial3(){
if(newdata == true){
Serial.print("$TDS,");
Serial.print(d1);
Serial.print(",");
Serial.print(d2);
Serial.print(",");
Serial.print(d3);
Serial.print(",");
Serial.print(d4);
Serial.print(",");
Serial.print(d5);
Serial.print(",");
Serial.println(d6);
newdata = false;
}
}

