Dear Community,
I am working currently on a project where I need to use RS485. So far, I have not found any official solution, utilizing the HW USART(s).
Since the Arduino Serial library is the natural place to add this functionality I have modified it to add this functionality on my own. It eliminates the need to manually set the TX enable and wait until transmission is complete to clear it. The RS485 functionality can be simply invoked by adding a few control parameters to Serial.begin. Sending data in the usual manner is all that is required:
Serial.print("whatever..."); // DIR is automatically asserted.
The puffered sending is not affected in any way as the routine accomodates the data shifting out until the last character was sent to prevent cutting off the ongoing transmission.
Here is an example:
#define RS485_BAUD 9600
#define RS485_ENABLED 1
#define RS485_DISABLED 0
#define RS485_DIR_PIN 13 // standard Arduino pin numbering used here
#define RS485_POLARITY_ACTIVE_HIGH 0
#define RS485_POLARITY_ACTIVE_LOW 1
Serial.begin(RS485_BAUD, SERIAL_8N1, RS485_ENABLED, RS485_DIR_PIN, RS485_POLARITY );
where
RS485_ENABLED oder RS485_DISABLED, 1=ENABLED oder 0 (0=DISABLE)
RS485_POLARITY is only important for me due to hardware design reasons as ot allows to set the default level of the DIR pin.
I have tested it so far only on a ATMEGA328 but to plan to test it on chips with more then one USART. Once, testing has been satisfactory I would like to request if this modified library could become standard for the Arduino IDE package. Currently, I need to replace the two files HardwareSerial.cpp and the header file. This is clearly not ideal for the community.
Using Serial.begin in the normal short way, ignores the RS485 addition.
My question to you is, would you consider it beneficial to the wider community or should I just use it privately on my own? In the spirit of sharing I feel it is a worthwhile addition to the Arduino overall funtionality.
Anyways, I would appreciate to hear from you if you feel it deserves any interest and should be pursued further.
Best Regards,
Gerhard