19.09.2016, 15:04
Hi Buuuudzik,
Here is a script i use for other devices, here is a function to read a line without knowing the lenght, also another HEX function you might want to use:
Hope this helps you..
BR,
Erwin
Here is a script i use for other devices, here is a function to read a line without knowing the lenght, also another HEX function you might want to use:
Code:
---------------------------------------------------------------------
-- *********************** START CONNECTION ********************** --
---------------------------------------------------------------------
-- Load Modules
require('serial')
-- Open connection
port = serial.open('/dev/RS485', { baudrate = 4800, parity = 'odd', duplex = 'half', databits = 8, stopbits = 1 })
---------------------------------------------------------------------
-- *********************** START FUNCTIONS *********************** --
---------------------------------------------------------------------
-- Function to read until carier return is received or timeout
function readline(port, timeout)
local char, buf
buf = {}
while true do
char = port:read(1, timeout or 0.2)
-- error (timeout) or newline, stop
if char == nil or char == '\n' then
break
-- ignore cr char
elseif char ~= '\r' then
table.insert(buf, char)
end
end
return table.concat(buf)
end
-- Function to write to port and return a reply
function writetoport(port, timeout, command)
-- Write data to port as HEX
port:write(command)
-- Call function to read data
line = readline(port, timeout)
if #line > 0 then
return line
else
return "No reply received"
end
end
-- Function to decode hex string value to readable string format
function str2hex(str)
raw_len = string.len(str)
i = 1
while i <= raw_len do
current_hexvalue = '0x' .. string.format("%02x", string.byte(str, i))
if value then
value = value .. ', ' .. current_hexvalue
else
value = current_hexvalue
end
i = i + 1
end
return value
end
---------------------------------------------------------------------
-- ************************ END FUNCTIONS ************************ --
---------------------------------------------------------------------
---------------------------------------------------------------------
-- *********************** START WRITE/READ ********************** --
---------------------------------------------------------------------
-- Declare command to send to rotel
command = string.char(0xfe, 0x03, 0xa5, 0x10, 0x0a, 0xc2) -- power toggle
-- Send command to port and wait for reply or timeout
reply = writetoport(port, 1, command)-- Timeout is set to 1 second (default = 0.2)
---------------------------------------------------------------------
-- ************************ END WRITE/READ *********************** --
---------------------------------------------------------------------
---------------------------------------------------------------------
-- ************************ START LOGGING ************************ --
---------------------------------------------------------------------
-- Process the reply
if reply == "No reply received" then
log(reply)
else
--Decode the reply if hex
decoded = str2hex(reply)
log(decoded)
end
---------------------------------------------------------------------
-- ************************* END LOGGING ************************* --
---------------------------------------------------------------------
-- Close port
port:close()
---------------------------------------------------------------------
-- ************************ END CONNECTION *********************** --
---------------------------------------------------------------------
Hope this helps you..
BR,
Erwin