ENP-RS232
rs232
library implements serial communication for the ENP-RS232 module.
rs232.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 rs232.err_to_str
to convert it to string representation).
#
Examplers232.send
#
Sends data over RS-232 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 rs232.err_to_str
to convert it to string representation).
Calling rs232.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
.
#
Examplers232.receive
#
Receive data chunk from the RS-232 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 rs232.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
.
#
Examplers232.err_to_str
#
Returns string representation of rs232
function return code.