hex - Lua: Hexadecimal Word to Binary Conversion -


i'm attempting create lua program monitor periodic status pings of slave device. slave device sends status in 16-bit hexadecimal words, need convert binary string since each bit pertains property of device. can receive input string, , have table containing 16 keys each parameter. having difficult time understanding how convert hexadecimal word string of 16-bits can monitor it.

here basic function of i'm starting work on.

function slave_status(ip,port,name)     status = path:read(ip,port)     stable = {}     if status         stable.ready=bit32.rshift(status:byte(1), 0)         stable.paused=bit32.rshift(status:byte(1), 1)         stable.emergency=bit32.rshift(status:byte(1), 2)         stable.started=bit32.rshift(status:byte(1), 3)         stable.busy=bit32.rshift(status:byte(1), 4)         stable.reserved1=bit32.rshift(status:byte(1), 5)         stable.reserved2=bit32.rshift(status:byte(1), 6)         stable.reserved3=bit32.rshift(status:byte(1), 7)         stable.reserved4=bit32.rshift(status:byte(2), 0)         stable.delay1=bit32.rshift(status:byte(2), 1)         stable.delay2=bit32.rshift(status:byte(2), 2)         stable.armoff=bit32.rshift(status:byte(2), 3)         stable.shieldoff=bit32.rshift(status:byte(2), 4)         stable.diskerror=bit32.rshift(status:byte(2), 5)         stable.conoff=bit32.rshift(status:byte(2), 6)         stable.envoff=bit32.rshift(status:byte(2), 7)     end end 

i hope approach understandable? i'd receive hex strings, example 0x18c2 , turn 0001 1000 1100 0010, shifting right-most bit right , placing proper key. later in function monitor if bit had changed better or worse.

if run similar function in terminator in linux, , print out pairs following return:

49 24 12 6 3 1 0 0 56 28 14 7 3 1 0 0 

this not understanding how take each value , set bits

i'm pretty new not doubt there easier way this. if need explain further try.

i'd approach in different fashion 1 suggested paul.

first, create table storing properties of devices:

local tproperty = {     "ready",     "paused",     "emergency",     "started",     "busy",     "reserved1",     "reserved2",     "reserved3",     "reserved4",     "delay1",     "delay2",     "armoff",     "shieldoff",     "diskerror",     "conoff",     "envoff", } 

then, since device sends data 0xyyyy, can call tonumber directly (if not string). use function store each bit in table:

function bitconvert( sinput )     local treturn, inum = {}, tonumber( sinput ) -- optionally pass 16 second argument tonumber     while inum > 0         table.insert( treturn, 1, inum % 2 )         inum = math.floor( inum / 2 )     end     = #tproperty - #treturn, 1, -1         table.insert( treturn, 1, 0 )     end     return treturn end 

and then, map both tables together:

function map( tkeys, tvalues )     local treturn = {}     = 1, #tkeys         treturn[ tkeys[i] ] = tvalues[i]     end     return treturn end 

in end, have:

function slave_status( ip, port, name )     local status = path:read( ip, port )     local stable = map( tproperty, bitconvert(status) ) end 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -