ENP-RS485
rs485
library implements serial communication for the ENP-RS485 module.
rs485.init
#
Initializes hardware for serial communication. Must be called before any other communication function, and before using libraries like modbus
.
Returns 0
if initialization is performed successfully, otherwise returns error code (use rs485.err_to_str
to convert it to string representation).
#
Examplers485.send
#
Sends data over RS-485 interface. data
should be a string, it will be handled as a byte array.
Returns 0
if data was sent successfully, otherwise returns error code (use rs485.err_to_str
to convert it to string representation).
Calling rs485.send
will clear any data which is already buffered in a receive buffer. This is done for the most common request-response scenario when we want to receive the response to the request we just sent regardless of what was there in the receive buffer before that. You can prevent this behavior by setting do_flush
to false
.
#
Examplers485.receive
#
Receive data chunk from the RS-485 network. Most of the time it will hold the entire data packet - the sequence of data frames separated by several frames of silence. If the data you received does not hold the entire data packet (as defined by your communication protocol), read the next chunks until you have the full packet.
Returns data as string or nil
in case of error. Error code is returned as a second return value (use rs485.err_to_str
to convert it to string representation).
The maximum size of the resulting data is 1024 bytes by default. You can increase it by setting max_data_size
.
#
Examplers485.err_to_str
#
Returns string representation of rs485
function return code.