Go Down

Topic: Sending and receiving an array of integers over softwareserial  (Read 3558 times) previous topic - next topic

sen_diptangshu

Let me explain.  values to be sent =1,5,225.
                        values I send in array a[]={rev(1),rev(5),rev(225)}={1,5,522}
                        receiver receives a[] as string . It parses the string and extracts numbers. But the numbers are 1,5,224. For any number consisting of 3 digits or more , the same thing happens,value gets reduced by 1.I have enclosed the output on the serial monitor of the receiver . Plz see it for reference.

Robin2

Let me explain.  values to be sent =1,5,225.
                       
I should have been more specific. I want to see an example of what is received when you send that.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sen_diptangshu


Robin2

Image from Reply #17 so we don't have to download it. See this  Image Guide



...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

As you can see your image is unreadable. Please don't post pictures of text. Just copy and paste the text.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sen_diptangshu


Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

sen_diptangshu

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

//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;
}




Code: [Select]

//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

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Code: [Select]
         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.
The art of getting good answers lies in asking good questions.

CrToRo

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

Robin2

Could someone please share the programming for receiving the data contained in the array?
It is in the link in Reply #3

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up