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.

Error in script
#7
(Yesterday, 13:32)Daniel Wrote: Try this
Code:
function calcolo32bit (data)
  if data[3] then
    --log('sono passato', data)
        local byte_1 = data[6]
        local byte_2 = data[5]
        local byte_3 = data[4]
        local byte_4 = data[3]
      byte_received = ((byte_4 * 16777216)+(byte_3 * 65536)+(byte_2 * 256)+byte_1)
        --log(byte_received)
        s = bit.rshift(byte_received, 31)
        --log(s)
        exp1 = bit.rshift(byte_received, 23)
        --log(exp1)
        matissa = bit.band(byte_received , 8388607)
        --log(matissa)
        matissa2 = (matissa * 0.0000001) + 1
        --log(matissa2)
        risultato = (((-1)^s) * (2^(exp1-127))* matissa2)
      return(risultato)
  else
    return(0)
  end
end

function crc16(data)
-- log('valore passato = ',data)
  local crc = 0xFFFF
for i = 1, #data do
    crc = bit.bxor(crc, data:byte(i))
    for j = 1, 8 do
      local k = bit.band(crc, 1)
      crc = bit.rshift(crc, 1)
      if k ~= 0 then
        crc = bit.bxor(crc, 0x8408)
        end
      end
    end
    --return bit.bxor(crc, 0xFFFF)
  result = string.format("%04X",bit.bxor(crc, 0xFFFF))
  result_low = string.sub(result, 3 , 4)
  result_high = string.sub(result, 1 , 2)
  local crc16 = (',0x'..result_low..',0x'..result_high)
  return crc16
end

function crc_calc(inv,cmd_set)
 
  inv_num = {0x02,0x03,0x04,0x05,0x06,0x07}

  --log('inv_num',inv_num[inv])

  cmd = {{0x32,0x20,0x20,0x20,0x20,0x20,0x20},
      {0x3B,0x01,0x00,0x20,0x20,0x20,0x20},
      {0x3B,0x02,0x00,0x20,0x20,0x20,0x20},
      {0x3B,0x03,0x00,0x20,0x20,0x20,0x20},
      {0x3B,0x04,0x00,0x20,0x20,0x20,0x20},
      {0x3B,0x08,0x00,0x20,0x20,0x20,0x20},
      {0x3B,0x09,0x00,0x20,0x20,0x20,0x20},
      {0x3B,0x1E,0x00,0x20,0x20,0x20,0x20},
      {0x3B,0x31,0x00,0x20,0x20,0x20,0x20},
      {0x4E,0x00,0x20,0x20,0x20,0x20,0x20},
      {0x4E,0x01,0x20,0x20,0x20,0x20,0x20},
      {0x4E,0x03,0x20,0x20,0x20,0x20,0x20},
      {0x4E,0x04,0x20,0x20,0x20,0x20,0x20},
      {0x4E,0x05,0x20,0x20,0x20,0x20,0x20}}
 
data = string.char(inv_num[inv],cmd[cmd_set][1],cmd[cmd_set][2],cmd[cmd_set][3],cmd[cmd_set][4],cmd[cmd_set][5],cmd[cmd_set][6],cmd[cmd_set][7])
--log('valore grezzo = ',data)
   crc1 = crc16(data)
--log(crc_calc)
  return (crc1)
end





for i = 1, 6, 1 do
  local crc = crc_calc(i ,1)
   log(crc)
end
thank you so much in this way it works.
So error was in crc16 function.
BR Cristian
Reply


Messages In This Thread
Error in script - by CristianAgata - Yesterday, 11:13
RE: Error in script - by hocine - Yesterday, 11:27
RE: Error in script - by CristianAgata - Yesterday, 11:34
RE: Error in script - by Daniel - Yesterday, 11:48
RE: Error in script - by CristianAgata - Yesterday, 12:00
RE: Error in script - by Daniel - Yesterday, 13:32
RE: Error in script - by CristianAgata - Yesterday, 15:22

Forum Jump: