From: Roman Bazalevsky Date: Tue, 31 Dec 2019 14:38:14 +0000 (+0300) Subject: Произвольные настройки порта в конфигурационном файле. X-Git-Url: https://git.rvb.name/weathermon.git/commitdiff_plain/5a09d3affc63feb942e2453ccfc4143726292501 Произвольные настройки порта в конфигурационном файле. --- diff --git a/bin/weathermon-iio b/bin/weathermon-iio index d288edf..dcbca64 100755 --- a/bin/weathermon-iio +++ b/bin/weathermon-iio @@ -116,6 +116,20 @@ function get_mhz(record) end +function search_rs232_const(rs232,prefix,value) + + for k,v in pairs(rs232) do + + if k == prefix..value:upper() then + return v + end + + end + + return nil + +end + function init_serial_device(device,subsystem,parameters) rs232 = require("luars232") @@ -123,24 +137,33 @@ function init_serial_device(device,subsystem,parameters) pcall(function () local e, port = rs232.open(device["port"]) - - if subsystem == "mhz" then + + local baud = device["baud"]; if baud == nil then baud = 9600; end + local bits = device["bits"]; if bits == nil then bits = 8; end + local stop_bits = device["stop_bits"]; if stop_bits == nil then stop_bits = 1; end + local parity = device["parity"]; if parity == nil then parity = "NONE"; end + local flowctl = device["flowctl"]; if flowctl == nil then flowctl = "OFF"; end - assert(port:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR) - assert(port:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR) - assert(port:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR) - assert(port:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR) - assert(port:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR) + assert(port:set_baud_rate(search_rs232_const(rs232,"RS232_BAUD_",baud)) == rs232.RS232_ERR_NOERROR) + assert(port:set_data_bits(search_rs232_const(rs232,"RS232_DATA_",bits)) == rs232.RS232_ERR_NOERROR) + assert(port:set_parity(search_rs232_const(rs232,"RS232_PARITY_",parity)) == rs232.RS232_ERR_NOERROR) + assert(port:set_stop_bits(search_rs232_const(rs232,"RS232_STOP_",stop_bits)) == rs232.RS232_ERR_NOERROR) + assert(port:set_flow_control(search_rs232_const(rs232,"RS232_FLOW_",flowctl)) == rs232.RS232_ERR_NOERROR) - getparameter = {} + getparameter = {} - getparameter["rs232"] = port + getparameter["rs232"] = port + + if subsystem == "mhz" then + getparameter["function"] = get_mhz getparameter["name"] = "CO2_PPM" getparameter["sensor"] = "MHZ19" parameters[#parameters+1] = getparameter end + + parameters[#parameters+1] = getparameter end)