Skip to main content


rs485 library implements serial communication for the ENP-RS485 module.


-- @param baud_rate number Rate at which information is transferred
-- @param data_bits number Data bits, range 5-8
-- @param parity string Parity, one symbol: "O" - odd "E" - even, "N" - disabled
-- @param stop_bits number Number of stop bits, values: 1 or 2
-- @param buffer_size number (optional) Size of buffer for data receiving, limit is 4096 bytes
-- @return number
function rs485.init(baud_rate, data_bits, parity, stop_bits, buffer_size)

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).


result = rs485.init(115200, 8, "N", 1)
if result ~= 0 then
enapter.log("RS-485 failed: "..result.." "..rs485.err_to_str(result), "error", true)


-- @param data string Data to send
-- @param do_flush boolean Flush incoming data buffer, default is `true`
-- @return number
function rs485.send(data, do_flush)

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.


result = rs485.send("L 10 2") -- send the data to RS-485 network
if result ~= 0 then
-- sending data failed
enapter.log("RS485 send failed: "..result.." "..rs485.err_to_str(result), "error", true)

rs485.send("A)223", false) -- do not flush receive buffer


-- @param timeout number Time to wait for a response in milliseconds
-- @param max_data_size number Maximum size of a data chunk
-- @return string|nil, number
function rs485.receive(timeout, max_data_size)

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.


data, result = rs485.receive(1000) -- receive with 1 second timeout
if not data then
-- receiving data failed
enapter.log("RS485 receive failed: "..result.." "..rs485.err_to_str(result), "error", true)
rs485.receive(1000, 2048) -- receive the data chunk of up to 2048 bytes


-- @param error number
-- @return string
function rs485.err_to_str(error)

Returns string representation of rs485 function return code.

Hardware diversity is welcome. Integrate any device into a unified energy network.
© 2024 Enapter
Developer toolkit