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
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.
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.
#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};
}
}
{
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.
AWOL:
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.