Sending and receiving an array of integers over softwareserial

Output on serial monitor:

0
0
0

0
0
0
<1,5,522,>
1
5
224

sen_diptangshu:
Output on serial monitor:

We are now along way from your Reply #12 which first mentioned this problem. Please refresh my mind about what that output is showing compared to what it should be showing. You said something about reversing numbers.

And I am rather surprised to see <1,5,522,> because the code in my recvWithStartEndMarkers() function strips out the start- and end-markers

...R

The recv function I used is not an exact replica of your recvwithstartendmarkers().It is slightly different.Plz go through the code.

//sending
#include <SoftwareSerial.h>
#include <math.h>

SoftwareSerial mySerial(5,6); //rx pin,tx pin

void setup() {
  // put your setup code here, to run once:
mySerial.begin(28800);
}
int otp=1;
int a[]={rev(1),rev(5),rev(225)};

void loop() {
  // put your main code here, to run repeatedly:
if(otp==1){
  senddata();
  otp=0;
}
}

void senddata(){
   int i;
   mySerial.print("<");
   for(i=0;i<sizeof(a)/sizeof(a[0]);i++){
      mySerial.print(a[i]);
      mySerial.print(",");
    
   }
   mySerial.print(">");
}
int rev(int n){
  
  int i,reverse=0;
  while(n>0){
    int rem=n%10;
    reverse=reverse*10+rem;
    n=n/10;
  }
  return reverse;
}
//receiving
#include <SoftwareSerial.h>
#include <math.h>

SoftwareSerial mySerial(5,6);  //rx pin,tx pin

char recvchars[32];
boolean newdata=false;
int dataarray[3];

void setup() {
  // put your setup code here, to run once:
mySerial.begin(28800);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
if(newdata==false){
  recvdata();

/*Serial.print(recvchars);
Serial.print("\n");
delay(100);

readintonum(recvchars,dataarray);
int i;
for(i=0;i<3;i++){
  Serial.println(dataarray[i]);*/
}


} 


void recvdata(){
  static boolean recvinprogress=false;
  static byte ndx=0;
  char startmarker='<';
  char comma=',';
  char endmarker='>';
  char c;

  while(mySerial.available()>0 && newdata==false){
    c=mySerial.read();

    if(c==startmarker)recvinprogress=true;

    else if(c==endmarker){
      recvchars[ndx]=c;
      recvinprogress=false;
      ndx=0;
      newdata=true;
    }
    if(recvinprogress==true){
      recvchars[ndx]=c;
      ndx++;  
    }
    
    
  }

  Serial.print(recvchars);
Serial.print("\n");
delay(100);

readintonum(recvchars,dataarray);
int i;
for(i=0;i<3;i++){
  Serial.println(dataarray[i]);
}
delay(100);
}

void readintonum(char* recvchars,int* dataarray){
  int i,count=0,num=0,arraycount=0;
  boolean started=false;
  for(i=0;i<32;i++){
     if(recvchars[i]=='<')started=true;
       
     if((started==true)&&(recvchars[i]==',')){
      count=0;
      dataarray[arraycount]=num;
      arraycount++;
      num=0;
     }
     if((started==true)&&(recvchars[i]>=48)&&(recvchars[i]<=57)){
         num=num+pow(10,count)*(recvchars[i]-48);
         count++;
     }
     if((started==true)&&(recvchars[i]=='>'))break;
  }
  return;
}

Expected output
<1,5,522,>
1
5
225

Actual Output
<1,5,522,>
1
5
224

sen_diptangshu:
The recv function I used is not an exact replica of your recvwithstartendmarkers().It is slightly different.Plz go through the code.

Not unless I get a good explanation for why my working and reliable version needed to be changed and a clear description of how you have changed it.

As far as I am concerned any manipulation of the received data should happen AFTER the data has been received - not during the receive process.

...R

         num=num+pow(10,count)*(recvchars[i]-48);

pow() takes two floats and returns a float. It is a piss-poor way to multiply 10 by itself.

What you are seeing is 99.999999 being truncated to 99, NOT 100.

Reversing the numbers before sending is just plain stupid. Collect the string, use strtok() to parse it, and atoi() to convert the tokens to ints.

Could someone please share the programming for receiving the data contained in the array?

CrToRo:
Could someone please share the programming for receiving the data contained in the array?

It is in the link in Reply #3

...R