Hello,
I have a problem with the Serial.available () statement in an Argon. I attach the code.
void setup()
{
Serial.begin(9600);
delay(100);
Serial1.begin(9600);
delay(100);
Serial.setTimeout(500);
Serial1.setTimeout(500);
Serial.println();
Serial.println("...............................Ready...............................");
}
void loop()
{
Serial1.print("0123456789");
//Serial.println("HelloWorldSerial");
Serial1.flush();
//t = micros();
//while (Serial1.available()!=10)
// delay(0.5);
delay(4);
a = Serial1.peek();
Serial.println(a);
while (Serial1.available()!=0)
{
Serial.print(Serial1.available());
Serial.print(" ");
i = Serial1.read();
Serial.print(i);
Serial.print(" ");
}
delay(500);
Serial.println(j);
j = j + 1;
}
The expected output should look something like this:
10 48 9 49 8 50 7 51 6 52 5 53 4 54 3 55 2 56 1 57 1153
Ten is the number bytes, but sometimes the output is something like this:
2 48 1 49 8 50 7 51 6 52 5 53 4 54 3 55 2 56 1 57 1136
Although the data is in the buffer, the instruction Serial1.available (), understands that it only has two bytes and after several readings it goes to 8. The function seems to have an error when sizing the data in the buffer. Can someone help me with this?.
Note: In Photon or RedbearDuo this problem does not occur
I don’t think it’s not only Serial1.available()
that messes up but also `Serial1.flush() seems to not do what the docs state.
I’m currently running some more tests.
Thank you very much for the interest @ScruffR, I will be attentive to your tests. I had not mentioned that I have a jump between the tx and rx of Serial1. Happy day.
This is my test code which renders the expected (=correct) results on a Photon but not on the Argon
#define BUF_LIMIT 80
uint8_t buf[BUF_LIMIT];
int c;
#define txWAIT Serial1.flush()
//#define txWAIT delay(500)
void setup() {
Serial.begin();
Serial1.begin(115200);
memset(buf, 0, sizeof(buf));
for (char c = '0'; c < '0'+sizeof(buf)-2; c++)
buf[c-'0'] = c;
}
void loop() {
Serial.println();
while(Serial1.read() >= 0); // flush the RX buffer
Serial.printlnf("Should be 0 is %d", Serial1.available());
Serial1.write(buf, sizeof(buf)-1); // pre-fill RX buffer again
txWAIT;
Serial.printlnf("Content of RX buffer should be\r\n%s\r\nbut is", buf);
while((c = Serial1.read()) >= 0) // flush the RX buffer
Serial.write(c);
Serial.println();
Serial.printlnf("Trying to fill buffer with %d characters", sizeof(buf)-5);
Serial1.write(buf, sizeof(buf)-5); // pre-fill RX buffer
txWAIT;
Serial.printlnf("Should be %d is %d", min(sizeof(buf)-5, 63), Serial1.available());
for (int i = 0; i < 10; i++)
Serial.write(Serial1.read());
Serial.println();
Serial.printlnf("Should be %d is %d", min(sizeof(buf)-15, 63-10), Serial1.available());
Serial1.write(buf, 10); // re-fill RX buffer
txWAIT;
Serial.printlnf("Should be %d is %d", min(sizeof(buf)-5, 63), Serial1.available());
delay(1000);
}
It seems the RX buffer (which is supposed to be a circular or ring buffer) is not treated the same way as on Gen1&2 devices. Also the distance between head and tail of the buffer content is not calculated correctly - hence the erronous Serial1.available()
values.
It also seems you can fill the buffer beyond the expected 64 byte limit (on Gen1&2 it’s 64-1 byte to have a gap between head and tail).