Hardware Serial Confusion - using LinkSprite RS485 hardware.

I am communicating with an industrial device via RS485 shield from LinkSprite. However, I don’t understand why it’s working.

I write the request for data to the device, the device waits 10mS after the stop bit, then responds. I have a 100mS delay between the end of writing and the beginning of reading. And yet, there is data to read long after the device has transmitted it. There is something I don’t understand here - any thoughts?

//*******As of July 25, 2014, this will read the Totalizer Count and display it
// on the LCD. 
  
#include <LiquidCrystal.h>
int data =0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  
 Serial.begin (9600,SERIAL_7E1); //Industrial device has start bit, 7 data bits, Even parity, check sum, and one stop bit.
 inputString.reserve(200);
  
  
  lcd.begin(16,2);
  delay (100);
  lcd.setCursor (0,1);
  lcd.print ("Display Working."); //Display this on the second line
  delay (200);
  
  }

void loop() {
  
 
    lcd.setCursor (0,0);
    delay (2000);
    Serial.print (">01QTC49."); //Send request for data to device. The device is set to send response 
                                //after 10mS. The Tx led comes on and off, and after 10mS the Rx led comes on and off.
    delay (100);                //The Rx led has been off for most of this delay.
   
  
 while (Serial.available()){ 
  
 data = Serial.read();
 
 lcd.write (data);            //After the "delay 100" is over, the string of data is displayed on the LCD.
                              // The data must have been read and buffered during the delay for this to happen, or I don't 
                              //understand how the hardware serial works - but probably both.

}
  }

ih55t:
And yet, there is data to read long after the device has transmitted it.

I don't understand what you mean. Can you give an example of the data the device sends?

What do you mean by "long after"? (How many milliseconds?)

Can you post a link to the datasheet for the device?

...R

ih55t:
I write the request for data to the device, the device waits 10mS after the stop bit, then responds. I have a 100mS delay between the end of writing and the beginning of reading. And yet, there is data to read long after the device has transmitted it. There is something I don't understand here - any thoughts?

The comment in your code is correct, data received is buffered until you read it. The buffer size is probably 64 bytes.

The industrial device is a flow meter conditioner, and one of its functions is recording total volume. When I send a request for the total flow volume, I do get back the correct numbers. All is well in that regard. The question is with the Atmel 328P silicon. Specifically the hardware serial, pins 0 and 1. As bobcousins posted, data received at the hardware serial port must be buffered, since I'm not reading it until 90mS after it was sent from the industrial device.

I was concerned that I didn't fully understand what the code was doing, but it seems that the hardware serial functions separate from the code, in a sense. If it's not in the Tx mode, is should be waiting in the Rx mode, and seems to buffer incoming data even if the code hasn't come around to "read".

It makes sense after the fact. It would be difficult to know exactly when data was going to be coming in, so the hardware serial just reports back what it's already received when the code is ready for it.

Thanks for the info - greatly appreciated.

ih55t:
seems to buffer incoming data even if the code hasn't come around to "read".

I think the standard buffer size is 64 bytes but you could edit the Hardware Serial code to increase that if necessary.

...R