Author Topic: know when UART serial data has been sent  (Read 1470 times)

0 Members and 1 Guest are viewing this topic.

Offline tomshirvoTopic starter

  • Regular Contributor
  • *
  • Posts: 53
know when UART serial data has been sent
« on: February 24, 2015, 02:05:31 am »
Hi All,

I'm using a max481E RS485 chip with a few Arduinos connected. This chip needs two pins changed to allow sending or receiving. The problem is that I have made a function that will set the pin then send the data and then change the pin back. Only one character is sent and the pin is set back to LOW. I think this is because the serial.write is shifting data into a buffer and then the hardware for the serial takes over and sends it.

Is there a way to know when the data is sent or wait until the data is sent and then change the pin state.

Code: [Select]
void commWrite(char * data,int dataSize){
  digitalWrite(commsTX,HIGH);
  for(int i = 0; i < dataSize;i++){
    Serial.write(data[i]);
  }
  digitalWrite(commsTX,LOW);
}

Thanks
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: know when UART serial data has been sent
« Reply #1 on: February 24, 2015, 02:32:10 am »
In theory, on AVR you can check the TXC0 bit in the status register, and this is done by the Serial.flush() arduino function.
(In practice, there are some complications and I'm not sure whether they ever got it working perfectly.  But that's the starting point.)
 

Offline tomshirvoTopic starter

  • Regular Contributor
  • *
  • Posts: 53
Re: know when UART serial data has been sent
« Reply #2 on: February 24, 2015, 03:11:13 am »
Thanks for that, I just added Serial.flush() as that waits until the data has been sent before returning.

Code: [Select]
void commWrite(char * data,int dataSize){
  digitalWrite(commsTX,HIGH);
  for(int i = 0; i < dataSize;i++){
    Serial.write(data[i]);
  }
  Serial.flush();
  digitalWrite(commsTX,LOW);
}
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf