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
#1
Good morning,
I have a problem in this resident script.
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 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)     crc_calc = crc16(data)   --log(crc_calc)   return (crc_calc) end for i = 1, 6, 1 do  --log(i)          crc = crc_calc(i,1) --log(crc) end
The first time (i = 1) it works, then it broken and says this error
Code:
Power_one 10.09.2025 13:10:53 Resident script:164: attempt to call global 'crc_calc' (a string value) stack traceback:
I can't understand what is it the problem.

Can someone help me? Please

B.R. Cristian
Reply
#2
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 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)   local crc_result = crc16(data)  -- use different name   --log(crc_result)   return (crc_result) end for i = 1, 6, 1 do --log(i)        crc = crc_calc(i,1) --log(crc) end
hello, try this
Reply
#3
(10.09.2025, 11:27)hocine Wrote:
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 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)   local crc_result = crc16(data)  -- use different name   --log(crc_result)   return (crc_result) end for i = 1, 6, 1 do --log(i)        crc = crc_calc(i,1) --log(crc) end
hello, try this
Hi,
thanks but it is the same error. Always first step and then error.
BR Cristian
Reply
#4
Where did you get this function from? crc16(data)
------------------------------
Ctrl+F5
Reply
#5
(10.09.2025, 11:48)Daniel Wrote: Where did you get this function from? crc16(data)

sorry i forget to attached a part...... here for you 
Code:
--funzione calcolo CRC16 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)   crc_calc = (',0x'..result_low..',0x'..result_high)   return crc_calc end
Reply
#6
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
------------------------------
Ctrl+F5
Reply
#7
(10.09.2025, 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


Forum Jump: