-- -- Example to read from Solar Logger. -- -- This uses luars232 to send to the com port. -- -- The serial port is buffered. -- Send the command and wait for PIC to return information. -- -- Use a simple timeout counter to establish when the PIC has dried up. -- rs232 = require( "luars232" ) -- Linux -- port_name = "/dev/ttyS0" -- Windows port_name = "COM1" port_name = "COM4" local out = io.stderr lastPort = nil -- -- On my Fujitsu I can find COM1 to COM9. does not find others. -- for cnt = 0,255 do port_name = "COM"..cnt -- print( port_name ) local e, p = rs232.open(port_name) if e == rs232.RS232_ERR_NOERROR then -- handle error out:write(string.format("Found serial port '%s', error: '%s'\n", port_name, rs232.error_tostring(e))) assert(p:close() == rs232.RS232_ERR_NOERROR) lastPort = port_name end -- assert(p:close() == rs232.RS232_ERR_NOERROR) end if lastPort then port_name = lastPort -- assume PIC plugged into last port found port_name = lastPort else print( "no port found ... ") end port_name = "COM4" -- open port local e, p = rs232.open(port_name) if e ~= rs232.RS232_ERR_NOERROR then -- handle error out:write(string.format("can't open serial port '%s', error: '%s'\n", port_name, rs232.error_tostring(e))) return end -- set port settings assert(p:set_baud_rate(rs232.RS232_BAUD_4800) == rs232.RS232_ERR_NOERROR) assert(p:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR) assert(p:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR) assert(p:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR) assert(p:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR) out:write(string.format("OK, port open with values '%s'\n", tostring(p))) -- print(string.format("OK, port open with values '%s'\n", tostring(p))) --- --- listen. We need to wait for end of transmission --- --- finished = 1 silenceCount = 0 -- read with timeout local read_len = 10000 -- read one byte -- local timeout = 100 -- in miliseconds local timeout = 100 -- in miliseconds --- --- wait for transmission and flush input buffer --- wait for end of transmission by reading less untill we get silence. --- silenceCount = 10 while silenceCount ~= 0 do --- --- listen. We need to wait for end of transmission --- --- -- use the read time out to allow the PIC to send response. -- first read read loads local err, data_read, size = p:read(read_len, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- print( "===", err, size ) print( err,size,data_read ) -- now read less read_len = 100 -- read one byte if size > 1 then silenceCount = 2 end silenceCount = silenceCount -1 dataBuff = "" end -- read with timeout local read_len = 10000 -- read one byte -- local timeout = 100 -- in miliseconds local timeout = 100 -- in miliseconds dataBuff = "" --- --- Stop logging. --- --- -- write command -- Write S; to stop logger printing out measurements. print("Sending commands...") err, len_written = p:write("S;\n", timeout) -- use the read time out to allow the PIC to send response. local err, data_read, size = p:read(read_len, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- print( "===", err, size ) -- print( data_read ) dataBuff = data_read err, len_written = p:write("g07;\n", timeout) -- use the read time out to allow the PIC to send response. local err, data_read, size = p:read(read_len, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- print( "===", err, size ) -- print( data_read ) dataBuff = dataBuff .. data_read -- really need to check if this is valid before moving on. print( dataBuff ) print("Sending command: d; to dump results ...") --- --- request dump. --- --- -- write command err, len_written = p:write("d;\n", timeout) -- set buffer and time out local read_len = 10000 -- read one byte -- local timeout = 100 -- in miliseconds local timeout = 200 -- in miliseconds finished = 1 silenceCount = 0 while silenceCount < 3 do err, data_read, size = p:read(read_len, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- print( "===", err, size ) print( data_read ) -- print( "+" ) if size > 1 then dataBuff = dataBuff .. data_read else silenceCount = silenceCount + 1 end end -- write without timeout --err, len_written = p:write("test") assert(e == rs232.RS232_ERR_NOERROR) -- write with timeout 100 msec --err, len_written = p:write("test\n", timeout) --assert(e == rs232.RS232_ERR_NOERROR) -- close print( "=== dump ===" ) -- look for lines stripping Control characters ofp = io.open("log.txt","a+") if ofp then ofp:write("====SL001=====================================\n") ofp:write( os.date() .. "\n" ) ofp:write("==============================================\n") ofp:write( dataBuff .. "\n" ) ofp:write("====END=======================================\n\n") ofp:close() end for line in string.gmatch( dataBuff, "\n([%C]*)%c*\n*" ) do -- print( "line: "..line .. " ===" ) v0,v1,v2,v3 = line:match( "^(%x+),,%x+,%x+,%x+,%x+,%x+,(%x+),(%x+),(%x+)" ) if( v3) then print( tonumber(v0,16),tonumber(v1,16),tonumber(v2,16),tonumber(v3,16) ) end end print( "=== end ===" ) --- --- reset the results pointer. --- --- err, len_written = p:write("p0702;\n", timeout) -- use the read time out to allow the PIC to send response. local err, data_read, size = p:read(read_len, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- print( "===", err, size ) -- print( data_read ) dataBuff = dataBuff .. data_read -- really need to check if this is valid before moving on. print( dataBuff ) print("Sending command: s; to dump results ...") --- --- restart the measurements. --- --- -- err, len_written = p:write("s;\n", timeout) -- use the read time out to allow the PIC to send response. local err, data_read, size = p:read(read_len, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- print( "===", err, size ) -- print( data_read ) dataBuff = dataBuff .. data_read -- really need to check if this is valid before moving on. print( dataBuff ) assert(p:close() == rs232.RS232_ERR_NOERROR) print("Finished...")