Go Down

Topic: wrong serial data (Read 827 times) previous topic - next topic

tech7

Aug 02, 2019, 08:08 pm Last Edit: Aug 02, 2019, 08:09 pm by tech7
hello everyone.
i am communicating with a system serially and i am able to communicate with it except one issue.
i have two functions and i want to call them together so that i can get two values one after other but i am unable to do so. The data values comes correct when i call either of the function but when i try calling them together the data values throws some random values and never comes right. what could be the issue? the code is given below

Code: [Select]



#include<SoftwareSerial.h>

#define nodata 0

float data1[11] = { 0.0 };
float data2[11] = { 0.0 };

SoftwareSerial obj (2,3);             


void setup()
{
 obj.begin(9600);
 Serial.begin(9600);
}

 
void config1()
{
 
 obj.write(0x01);
 obj.write(0x0A);
 obj.write(0x23);
 obj.write(0x43);
 obj.write(0x45);
 obj.write(0x19);
 obj.write(0x20);

  if(obj.available()>0)
  {
    for(int i=0;i<10;i++)
    {
    data1[i]= obj.read();
    }
    float value1 = (data1[6]*256+data1[5]);
    Serial.println(value1);
    value1 = 0; 
  }
 
  else
  {
    return nodata;
  }
 
  for(int i=0;i<10;i++)
  {
   data1[i]= {0};
  }
 
}

void config2()
{
 
 obj.write(0x01);
 obj.write(0x0A);
 obj.write(0x24);
 obj.write(0x44);
 obj.write(0x46);
 obj.write(0x19);
 obj.write(0x20);

  if(obj.available()>0)
  {
   
    for(int i=0;i<10;i++)
    {
    data2[i]= obj.read();
    }
    float value2 = (data2[6]*256+data2[5]);
    Serial.println(value2);
    value2 = 0; 
     
  }
  else
  {
    return nodata;
  }
 
   for(int i=0;i<10;i++)
    {
    data2[i]= {0};
    }
  }


void loop()
{
 config1();
 delay(100);
 config2();
   
}




as you can see i am calling config1 and config2 together, in this case the data never comes right..but when i call either config1 or config2, it gives data perfectly.



AWOL

Code: [Select]
if(obj.available()>0)
  {
    for(int i=0;i<10;i++)
    {
    data1[i]= obj.read();
  The usual response to this kind of construct goes "check to see if there is at least one character to read, then go ahead and read all ten of them".

cattledog

Code: [Select]
float data1[11] = { 0.0 };
float data2[11] = { 0.0 };


If you are reading the serial data into these arrays, they should be typed as byte not float.

tech7

Code: [Select]
float data1[11] = { 0.0 };
float data2[11] = { 0.0 };


If you are reading the serial data into these arrays, they should be typed as byte not float.
okay. i declared them as byte..but still the same problem occurs..can't read both functions together

AWOL

Have you checked to see that there is data to read, before reading it?

GolamMostafa

Do you have any device connected with your host Arduino via software serial port -- obj(2, 3)? If so, upload the following sketch (a slightly version of your one) and check that there appears something on the Serial Monitor.
Code: [Select]
#include<SoftwareSerial.h>

#define nodata 0

byte data1[11] = { 0 };
byte data2[11] = { 0 };
SoftwareSerial obj (2, 3);

void setup()
{
  obj.begin(9600);
  Serial.begin(9600);
}


void loop()
{
  config1();
  delay(100);
  config2();

}


void config1()
{
  obj.write(0x01);
  obj.write(0x0A);
  obj.write(0x23);
  obj.write(0x43);
  obj.write(0x45);
  obj.write(0x19);
  obj.write(0x20);

  if (obj.available() > 0)
  {
    byte m = obj.available();
    for (int i = 0; i < m; i++)
    {
      data1[i] = obj.read();
    }
    int value1 = (data1[6] * 256 + data1[5]);
    Serial.println(value1);
    value1 = 0;
  }

  else
  {
    return nodata;
  }

  for (int i = 0; i < 10; i++)
  {
    data1[i] = {0};
  }

}

void config2()
{

  obj.write(0x01);
  obj.write(0x0A);
  obj.write(0x24);
  obj.write(0x44);
  obj.write(0x46);
  obj.write(0x19);
  obj.write(0x20);

  if (obj.available() > 0)
  {
    byte n = obj.available();
    for (int i = 0; i < n; i++)
    {
      data2[i] = obj.read();
    }
    int value2 = (data2[6] * 256 + data2[5]);
    Serial.println(value2);
    value2 = 0;

  }
  else
  {
    return nodata;
  }

  for (int i = 0; i < 10; i++)
  {
    data2[i] = {0};
  }
}

AWOL

Code: [Select]
  if (obj.available() > 0)
  {
    byte m = obj.available();
    for (int i = 0; i < m; i++)
    {
      data1[i] = obj.read()
Oops

GolamMostafa

#7
Aug 03, 2019, 06:11 pm Last Edit: Aug 03, 2019, 06:32 pm by GolamMostafa
Code: [Select]
 if (obj.available() > 0)
  {
    byte m = obj.available();
    for (int i = 0; i < m; i++)
    {
      data1[i] = obj.read()

Oops
My logic behind it is:

Probably a number of data bytes (say, m) have already been accumulated in the FIFO Buffer of the host Arduino. Execution of 'if(obj.available()>0)' will lead to the execution of the sub-clauses where I have known 'how many data bytes are present in the buffer' and then I have loaded them into the array.

Would appreciate to know where I have gone wrong.  

AWOL

It's a great way of continually overwriting data1[0]

Quote
Probably a number of data bytes (say, m) have already been accumulated
I'd prefer "Maybe".

GolamMostafa

#9
Aug 04, 2019, 07:23 am Last Edit: Aug 05, 2019, 04:17 am by GolamMostafa
It's a great way of continually overwriting data1[0]
To understand your above quoted proposition, I have conducted the following experiment. I have not drawn any conclusion. Your valuable comments would be highly appreciated.

1.  This is the UART setup between MEGA and UNO.


2.  MEGA sends the message "Arduino...!" to UNO. The message asynchronously reaches to UNO and gets saved into the FIFO Buffer of UNO.

3. UNO keeps polling K1 and when it is found closed (of course user has to press it), the UNO reads the message from the buffer and shows onto its Serial Monitor. It is an off-time signal acquisition and presentation.


4.  Operation
After uploading the sketches, I have pressed down the RESET switches of both Arduinos; after that, I have brought in the SM2 of UNO; after that, I have released the RESET button of UNO and then (after a while) I have released the RESET button of MEGA. (MEGA has sent the message "Arduino...!" to UNO and then has entered into loop() function to blink L continuously.) And then, I have gently applied LOW at DPin-4 (closing K1) of UNO to see the message Arduino...! on SM2. The message is there on SM2.

5. Sketches
Sender's Sketch
Code: [Select]
void setup()
{
  Serial.begin(115200);
  Serial1.begin(9600);
  pinMode(13, OUTPUT);
  Serial1.print("Arduino...!");
}

void loop()
{
   digitalWrite(13, !digitalRead(13));
   delay(1000);
}


Receiver's Sketch
Code: [Select]
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX=DPin-2, STX=DPin-3
char myChar[20] = "";

void setup()
{
  Serial.begin(115200);
  SUART.begin(9600);
  pinMode(4, INPUT_PULLUP);
  while (digitalRead(4) != LOW)
    ;
  byte n = SUART.available();
  Serial.println(n);                    //shows: 11
  if (n != 0)
  {
    for (int i = 0; i < n; i++)
    {
      myChar[i] = SUART.read();
    }
    Serial.println(myChar);   //shows: Arduino...!
  }
}

void loop()
{

}

AWOL

Quote
Your valuable comments would be highly appreciated.
You appear to have constructed an experiment to investigate something quite unlike the original question.

I don't know what you are trying to achieve.

GolamMostafa

#11
Aug 04, 2019, 02:24 pm Last Edit: Aug 04, 2019, 03:53 pm by GolamMostafa
You appear to have constructed an experiment to investigate something quite unlike the original question.

I don't know what you are trying to achieve.
My experiment aims at validating your following proposition; but, it does not get validated. The data[0] is not being continually overwritten.

Quote
It's a great way of continually overwriting data1[0]

AWOL


Go Up