Version 1.0.12, +05:30 03:16:49 PM 28-01-2024, Sunday, Monday
Creates a new RS485Class
object. If you are using a hardware serial port, you can simply send its name as a parameter. If you are using software serial, you must include the SoftwareSerial library first and create a new object of that type. Then send the name of the object as a parameter.
Currently, software serial is supported only on AVR (Arduino Uno, Nano, etc.) and ESP8266 boards. The macro SOFTWARE_SERIAL_REQUIRED
is used to check if a software serial port to be used. You can update the board support by adding the board name to the macro in the CSE_RS485.h
file.
RS485Class RS485 (auto serial, int dePin, int rePin, int txPin);
serial
: Name of the serial port to use. Can be hardware serial (HardwareSerial
) or software serial (SoftwareSerial
).dePin
: Drive enable pin.rePin
: Receive enable pin. Optional. Default:-1
.txPin
: Serial transmit pin (used to send break signals). Optional. Default:-1
.
Initializes the RS485 object. The baudrate can be left empty to make it 0
. This will prevent the serial port from being initialized by the RS485 library. You have to then manually initialize the serial port before calling any RS485 library functions. If the baudrate is non-zero, the user can also specify the UART configuration, for example SERIAL_8N1
. Custom UART configurations are not supported by all platforms, especially when using Software Serial ports. Currently, custom UART frame configurations are ignored when using software serial ports on the AVR platform. It is also possible to set pre and post delays in milliseconds for each communication.
There are 5 overloads of this function:
RS485.begin()
RS485.begin (baudrate)
RS485.begin (unsigned long baudrate, uint16_t config)
RS485.begin (unsigned long baudrate, int predelay, int postdelay)
RS485.begin (unsigned long baudrate, uint16_t config, int predelay, int postdelay)
baudrate
: Communication speed in bits per second (baud). Optional. Default:0
. If0
, the serial port will not be initialized.config
: Serial port configuration. Example,SERIAL_8N1
.predelay
: Delay in milliseconds before sending data. Optional. Default:0
.postdelay
: Delay in milliseconds after sending data. Optional. Default:0
.
None.
Disables RS485 communication. This will close the serial port and reset the DE and RE pins to INPUT
mode. The serial port will be closed regardless of whether it was initialized by the RS485 library or not.
RS485.end()
None.
None.
Returns the number of bytes (characters) available for reading from the RS485 port. This is data that already arrived and is stored in the serial receive buffer.
RS485.available()
None.
int
: The number of bytes available to read.
Returns the next byte (character) of the incoming serial data without removing it from the internal serial buffer. That is, successive calls to peek()
will return the same character, as will the next call to read()
. The original Arduino serial peek()
function returns an int
type. So this library also does the same.
RS485.peek()
None.
int
: The first byte of incoming serial data available or-1
if no data is available.
Returns a single byte from the serial read buffer. Return -1
if no data is available. Each read operation will remove the byte from the buffer.
RS485.read()
None.
int
: The first byte of incoming serial data available or-1
if no data is available.
Writes binary data to the serial port. This data is sent as a byte or series of bytes.
RS485.write (uint8_t b)
b
: unsigned char.
size_t
: The number of bytes written.
Waits for the transmission of outgoing serial data to complete.
RS485.flush()
None.
None.
Starts RS-485 transmission. This will assert the DE pin and wait for a pre-delay if it is set. The DE pin has priority over the RE pin. That means, if both DE and RE are actively asserted, the transceiver will be in transmission mode.
You must call this function before any write operations.
RS485.beginTransmission()
None.
None.
Ends the RS-485 transmission. This deasserts the DE pin and the RE pin is not modified (since DE has priority over RE). If the post-delay is non-zero, the function wait for the specified duration before deasserting the DE pin.
RS485.endTransmission()
None.
None.
Puts the transceiver in receive mode by asserting the RE pin. If the DE pin is currently being asserted, this has no effect, because DE has precedence over RE. Call the endTransmission()
function to deassert the DE pin to free the bus.
RS485.receive()
None.
None.
Puts the transceiver in a no receive mode by deasserting the RE pin. If the DE pin is also deasserted, then the transceiver is in a high impedance state.
RS485.noReceive()
None.
None.
Sends a serial break signal for the specified duration in milliseconds. This is done by closing the serial port, holding the TX pin of the serial port LOW
for a specified duration in milliseconds, and then reinitializing the serial port. If the TX pins is not set, this function has no effect. The serial port will be reinitialized only if the baudrate is greater than 0
. If the baudrate is 0
, then you have to manually initialize the serial port in your main code, just after calling this function.
RS485.sendBreak (unsigned int duration)
duration
: Duration of the break signal in milliseconds.
None.
Same as sendBreak()
but the duration is specified in microseconds.
RS485.sendBreak (unsigned int duration)
duration
: Duration of the break signal in microseconds.
None.
Modify the pins used to communicate with the RS-485 transceiver.
RS485.setPins (int dePin, int rePin, int txPin)
dePin
: Drive output enable pin.rePin
: Receiver output enable pin.txPin
: Transmission pin (used to send break signals).
None.
Sets the pre and post delays in milliseconds.
RS485.setDelays (int predelay, int postdelay)
predelay
: The delay before sending data.postdelay
: The delay after sending data.
None.
Asserts (set to HIGH
) the DE pin if the pin is defined. The function returns true
if the assertion was successful. If the DE pin is not defined, the function returns false
.
RS485.assertDE()
None.
bool
:true
if the DE pin was asserted,false
otherwise.
Deasserts (set to LOW
) the DE pin if the pin is defined. The function returns true
if the deassertion was successful. If the DE pin is not defined, the function returns false
.
RS485.deassertDE()
None.
bool
:true
if the DE pin was deasserted,false
otherwise.
Asserts (set to LOW
) the RE pin if the pin is defined. The function returns true
if the assertion was successful. If the RE pin is not defined, the function returns false
.
RS485.assertRE()
None.
bool
:true
if the RE pin was asserted,false
otherwise.
Deasserts (set to HIGH
) the RE pin if the pin is defined. The function returns true
if the deassertion was successful. If the RE pin is not defined, the function returns false
.
RS485.deassertRE()
None.
bool
:true
if the RE pin was deasserted,false
otherwise.