Skip to main content

ENP-RS232

rs232 library implements serial communication for the ENP-RS232 module.

rs232.init#

-- @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 rs232.init(baud_rate, data_bits, parity, stop_bits, buffer_size)
end

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

Example#

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

rs232.send#

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

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.

Example#

result = rs232.send("L 10 2") -- send the data to RS-232 network
if result ~= 0 then
-- sending data failed
enapter.log("RS232 send failed: "..result.." "..rs232.err_to_str(result), "error", true)
end
rs232.send("A)223", false) -- do not flush receive buffer

rs232.receive#

-- @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 rs232.receive(timeout, max_data_size)
end

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.

Example#

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

rs232.err_to_str#

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

Returns string representation of rs232 function return code.

Hardware diversity is welcome. Integrate any device into a unified energy network.
© 2021 Enapter
Developer toolkit
DocumentationReference
Community
GithubTindie