This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Hex manipulation
#7
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:

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
Reply


Messages In This Thread
Hex manipulation - by buuuudzik - 18.09.2016, 14:35
RE: Hex manipulation - by Erwin van der Zwart - 18.09.2016, 21:31
RE: Hex manipulation - by buuuudzik - 19.09.2016, 12:12
RE: Hex manipulation - by Erwin van der Zwart - 19.09.2016, 12:42
RE: Hex manipulation - by admin - 19.09.2016, 12:44
RE: Hex manipulation - by buuuudzik - 19.09.2016, 13:36
RE: Hex manipulation - by Erwin van der Zwart - 19.09.2016, 15:04
RE: Hex manipulation - by admin - 19.09.2016, 15:07
RE: Hex manipulation - by buuuudzik - 19.09.2016, 16:15

Forum Jump: