485-TTL with pin0, pin1

Hi all. I use modul 485-TTL (http://yourduino.com/sunshop2/index.php?l=product_detail&p=323) to connect arduino nano with my computer. The Example use SoftwareSerial and i test it ok, but i want to use hardware serial (pin0 and pin1) pin0-DI. pin1-RO. pin12-DE and RE. Here is my code

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(12, OUTPUT); 
  digitalWrite(12,LOW);
}

void loop() {
    // Init Transceiver

  // put your main code here, to run repeatedly:
  while(Serial.available())
  {
      int incom=Serial.read();
      digitalWrite(12,HIGH);
      Serial.println(incom);
      delay(50); //Delay for what?
      digitalWrite(12,LOW);
    }
  }

My question is: delay after Serial.println() for what? If i don't use delay, no thing return. Please help me. Sr my english isn't good.

thanhthuongt2: Hi all. I use modul 485-TTL (http://yourduino.com/sunshop2/index.php?l=product_detail&p=323) to connect arduino nano with my computer. The Example use SoftwareSerial and i test it ok, but i want to use hardware serial (pin0 and pin1) pin0-DI. pin1-RO. pin12-DE and RE. Here is my code

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(12, OUTPUT); 
  digitalWrite(12,LOW);
}

void loop() {    // Init Transceiver

 // put your main code here, to run repeatedly:  while(Serial.available())  {      int incom=Serial.read();      digitalWrite(12,HIGH);      Serial.println(incom);      delay(50); //Delay for what?      digitalWrite(12,LOW);    }  }




My question is: delay after Serial.println() for what? If i don't use delay, no thing return.
Please help me.
Sr my english isn't good.

The Serial.println() just adds data to a buffer. It takes a while for the data to be sent one bit at a time out the TX pin. Instead of delay(), a better choice would be to use Serial.flush(). Serial.flush(); waits for everything in the Serial output Buffer to actually be sent out the TX pin.

Without waiting for everything to be sent out the TX pin, the digitalWrite(12,LOW) immediately shuts off the RS485 driver. So, only a couple of bits of the first bytes in the Serial Output buffer are actually sent.

Better code for your TX event would be:

// instead of this code 
/*
  while(Serial.available())
  {
      int incom=Serial.read();
      digitalWrite(12,HIGH);
      Serial.println(incom);
      delay(50); //Delay for what?
      digitalWrite(12,LOW);
    }
*/

// If I understand you code, you are echoing everything received?

static unsigned long timeout; // a persistent variable that will retain it's value
static uint8_t bufcount; // howmany characters were in Serials Input buffer last time.

if(bufcount==Serial.available()){ // no new data has shown up
  if((millis()-timeout>500) // 1/2 a second has elapsed since last byte was received
    && (Serial.available)){ // and there is data in the input buffer

   digitalWrite(12,HIGH);// Switch RS485 driver to Send mode
   while(Serial.available()){ // there is data in the input buffer to be echoed back.
     char ch=Serial.read();
     Serial.print(ch);
     }
   Serial.flush(); // wait for all data to actually be sent out the TX pin
   digitalWrite(12,LOW); // back into Receive mode
   bufcount=0; // reset buffer count
   timeout = millis(); // reset timeout counter
   }
  else { // 1/2 second has not elapsed since last byte was received, wait! or
   // there is not data in the input buffer
   }
else { // new data has been received
  bufcount =Serial.available();
  timeout = millis(); // restart timeout
  }

Chuck.