From 156d1ceeb4c98ca3c9a3ed1ae7efb89e41be67d2 Mon Sep 17 00:00:00 2001 From: ranchuan Date: Thu, 29 Jun 2023 18:12:56 +0800 Subject: [PATCH] =?UTF-8?q?lua=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/checker_save.py | 34 +++++++++++++++++++++++++++------- python/file/judge.js | 12 ++++++++++++ python/file/judge.lua | 36 +++++++++++++++++++++++++++++------- python/file/prints.lua | 18 +++++++++++++----- source/ReadMe.txt | 3 +++ 5 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 python/file/judge.js diff --git a/python/checker_save.py b/python/checker_save.py index bef91b9..d93f66f 100644 --- a/python/checker_save.py +++ b/python/checker_save.py @@ -1,6 +1,7 @@ import lupa from lupa import LuaRuntime +import execjs class save: @@ -38,20 +39,39 @@ class save: -if __name__ == "__main__": - sa=save() - print(sa.hex2bit(bytes([0xab,0xcd]))) + +def lua_test(): + # 初始化lua运行环境 lua = LuaRuntime(unpack_returned_tuples=True) + # 初始化全局变量 json save_json=lua.eval("function(a) json=a end") with open("file/json.lua",encoding="utf-8") as f: save_json(lua.execute(f.read())) + # 初始化全局变量 prints save_prints=lua.eval("function(a) prints=a end") with open("file/prints.lua",encoding="utf-8") as f: save_prints(lua.execute(f.read())) + # 初始化全局变量 cfg_name lua.execute("cfg_name=\"file/checker_ye_cfg.json\"\n") - lua.execute("check_data={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}\n") + # 初始化全局变量 check_data + lua.execute("check_data={0xB5,0x96,0xEC,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x01,0x00,0x00,0x00,0x00,0x21,0x00,0x12,0x01,0x00,0x00,0x00,0x00,0xB5,0x00,0xB1,0x00,0x6B,0x00,0x00,0x00,0xA5,0x02,0xA4,0x02,0x00,0x00,0xA4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}\n") + # 执行判定脚本 with open("file/judge.lua",encoding="utf-8") as f: - lua.execute(f.read()) - # lua_func = lua.eval('function(a, b) return a+b end') - # print(lua_func(1,2)) + a,b=lua.execute(f.read()) + print(a,b) + +def js_test(): + with open("file/judge.js",encoding="utf-8") as f: + context1 = execjs.compile(f.read()) + json=open("file/checker_ye_cfg.json",encoding="utf-8").read() + check_data=bytearray([0xB5,0x96,0xEC,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x01,0x00,0x00,0x00,0x00,0x21,0x00,0x12,0x01,0x00,0x00,0x00,0x00,0xB5,0x00,0xB1,0x00,0x6B,0x00,0x00,0x00,0xA5,0x02,0xA4,0x02,0x00,0x00,0xA4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]) + a=context1.call("main",1,check_data) + print(a) + + +if __name__ == "__main__": + sa=save() + print(sa.hex2bit(bytes([0xab,0xcd]))) + # lua_test() + js_test() diff --git a/python/file/judge.js b/python/file/judge.js new file mode 100644 index 0000000..4749ed8 --- /dev/null +++ b/python/file/judge.js @@ -0,0 +1,12 @@ + + + + +function main(j,check_data) +{ + //var obj =JSON.parse(json); + //print(j) + //print(check_data) +} + + diff --git a/python/file/judge.lua b/python/file/judge.lua index 3e6280d..cade3b8 100644 --- a/python/file/judge.lua +++ b/python/file/judge.lua @@ -30,7 +30,8 @@ PLAN_ID=15 -- 2023.5.19 -- 扫描uid,超出最大返回时间范围则显示芯片异常 -- 添加起爆充能失败的判断,此时返回芯片异常 - +-- 2023.6.29 +-- 使用算数运算代替位运算 @@ -106,13 +107,29 @@ function only(t,n) end + +-- 计算2的n次幂 +function exp2(n) + local ret=1 + for i=1,n,1 do + ret=ret*2 + end + return ret +end + + + + + -- 8个字节描述执行结果0成功,1失败 function find_ack(index,acks) if(index>len(acks)*8) or index<1 then print("index out of range.index=",index) return false end - if ((acks[(index-1)//8+1] & (1<<((index-1)%8)))~=0) then + local temp=acks[math.floor((index-1)/8)+1] + local i=(index-1)%8 + if ((math.floor((temp % exp2(i+1))/exp2(i)))~=0) then return false else return true @@ -126,7 +143,9 @@ function find_stat(index,stats) print("index out of range.index=",index) return false end - if ((stats[(index-1)//8+1] & (1<<((index-1)%8)))~=0) then + local temp=stats[math.floor((index-1)/8)+1] + local i=(index-1)%8 + if ((math.floor((temp % exp2(i+1))/exp2(i)))~=0) then return true else return false @@ -197,8 +216,9 @@ function find_return(index) if(temp==nil) then return ret end -- print("find_return,temp=") -- prints:print_a(temp) - for i=1,len//2,1 do - ret[i]=temp[i*2-1]|(temp[i*2]<<8) + local len_t=math.floor(len/2) + for i=1,len_t,1 do + ret[i]=temp[i*2-1]+(temp[i*2]*exp2(8)) end return ret end @@ -640,7 +660,7 @@ function check_env() local skip,l=calc_skip(len(json_table["TaskArray"]),json_table["TaskArray"]) -- print("requier len=",skip+l) if(skip+l+16>len(check_data)) then - print("check data too less.") + print("check data too less.",skip+l+16) return "数据长度与方案不符",210 end if(json_table["PlanID"]~=PLAN_ID) then @@ -693,4 +713,6 @@ end -- 先返回错误描述,再返回错误码 return main() - +-- print("sssss",exp2(-1)) +-- show_acks({1,2,3,4,5,6,7,8}) +-- prints:print_a({1,2,3,4,5,6,7,8,9,0xf0,0xe,0xd}) \ No newline at end of file diff --git a/python/file/prints.lua b/python/file/prints.lua index 7f6966e..78dfb39 100644 --- a/python/file/prints.lua +++ b/python/file/prints.lua @@ -1,6 +1,13 @@ +-- 2023.6.29 +-- 使用算数运算代替位运算 + + + + + module = {} function module:print_t ( t ) @@ -45,12 +52,13 @@ function byte_to_hex(byte,size) local str_ret="" if(size==2) then str_ret=string.char( - string.byte( str_table,((byte>>12)&0xf)+1), - string.byte( str_table,((byte>>8)&0xf)+1), - string.byte( str_table,((byte>>4)&0xf)+1), - string.byte(str_table,(byte&0xf)+1)) + -- 右移12位,//4096 + string.byte( str_table,(math.floor(byte/4096)%16)+1), + string.byte( str_table,(math.floor(byte/256)%16)+1), + string.byte( str_table,(math.floor(byte/16)%16)+1), + string.byte(str_table,(byte%16)+1)) else - str_ret=string.char(string.byte(str_table,(byte%256)//16+1),string.byte(str_table,(byte%256)%16+1)) + str_ret=string.char(string.byte(str_table,math.floor((byte%256)/16)+1),string.byte(str_table,(byte%256)%16+1)) end return str_ret end diff --git a/source/ReadMe.txt b/source/ReadMe.txt index fe1eace..191a7bc 100644 --- a/source/ReadMe.txt +++ b/source/ReadMe.txt @@ -127,3 +127,6 @@ 设备忙时按下按键不会发送请求检测命令 电机步数可以配置,默认22000步 解决不插网线会导致程序重启的bug +2023.6.28 + 要实现重复注相同码不报错的功能,重复检测不报错,要使用两套方案 + \ No newline at end of file