Serial.read and .readBytes errors and bugs


i want output 80 chars lcd20x4 of whatever comes through serial. 
easy peacy right?
not so. 
1: using serial.read() in while loop result in infamous max of 63 chars,  ( using delays can bring in 70 odd chars) .

2: using serial.readbytes(buf,80); takes till 80 chars , seemingly treats buf ringbuffer...filling rest!  that's not true.
what under hood (i guess!!!) mark serial not available. call serial.isavailable() right away returns false though there bytes in buffer.

3: gives result odd, because loop run multiple times on buffer reading. ending last reading. below program gives surprising result... not wrong.
the second isavailable() in readserial() should/ought to/ true , put remaining chars buf2, doesen't.
 
code: [select]

#include <wire.h>
#include <liquidcrystal_i2c.h>
liquidcrystal_i2c lcd(0x27, 20, 4); // set lcd address 0x27 16 chars , 2 line display
static char buf[81] ;
int r = 0;
static char buf2[81];

void clearbuf(){
  memset(buf,0,sizeof(buf));
  memset(buf2,0,sizeof(buf2));
}

// test verify writing every position on screen
void fillscreen() {
  memset(buf,'a',sizeof(buf));
  memset(buf2,'b',sizeof(buf2));
  (int x = 0; x < 4; x++) {
    (int = 0; < 20; i++) {
      if (i == 0) {
        lcd.setcursor(0, x);
      }
   //   lcd.write((char)(i + 65 + x));
      lcd.write(buf[i+(x*20)]);
    }
  }
}

// trying in vain internal rx-buffer (in system .h file) : not here limitation is.
void writeserbufsize() {
  char sb[8];
  itoa(serial_tx_buffer_size, sb, 10);
  lcd.setcursor(0, 0);
  (int = 0; < 3; i++) {
    lcd.write(sb[i]);
  }
  lcd.setcursor(0, 0);
}

void setup()
{
  lcd.init();                      // initialize lcd
  lcd.backlight();
  fillscreen();
  writeserbufsize();
 
  serial.begin(9600);
}

int readserial()
{
  int n = 0;
  if (serial.available() > 0) {
    n = serial.readbytes(buf, 80);
    //   delay(10);  // don't you'll lose chars
  }

// hoping in vain able store surplus chars in buffer :-) never called?
  if (serial.available() > 0 ) {
    r = serial.readbytes(buf2, 80);
    //   delay(10);   // don't you'll lose chars
  }
 
  return n;
}

//output  stored chars in buf[]

void writelcd(int n) {
  (int l = 0; l < 4; l++) {
    (int x = 0; x < 20; x++) {
      if ((x % 20) == 0) lcd.setcursor(0, l);
      int p = x + (l * 20);
      if (p < n)lcd.write(buf[p]);  // write out buf , not buf2 !!!!
    }
  }
}

void loop()
{
  // when characters arrive on serial port...
  if (serial.available()) {
    clearbuf(); 
    // anothe try in vain: wait bit entire message arrive
    //  delay(10);   // don't you'll lose chars

    // clear screen
    lcd.clear();
    int n = readserial();
    writelcd(n);
    serial.print("antal char n= ");
    serial.println(n);
    serial.println(buf);
    if (r > 0) {   // write whatever surplus chars. never called.
      serial.print("antal char r= ");
      serial.println(r);
      serial.println(buf2);
    }
  }
}

 

double buffer - have second 81 byte char array, , stuff incoming bytes , display when you've got full message (either via length, or via start/end marker chars in serial stream).



Arduino Forum > Using Arduino > Programming Questions > Serial.read and .readBytes errors and bugs


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???