| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  | ---@diagnostic disable: lowercase-global, undefined-global | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 芯跳赋码仪错误判定脚本 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 方案配置文件: | 
					
						
							|  |  |  |  | -- cfg_name | 
					
						
							|  |  |  |  | -- 检测数据 | 
					
						
							|  |  |  |  | -- check_data | 
					
						
							|  |  |  |  | -- 当前路径 | 
					
						
							|  |  |  |  | -- get_path() | 
					
						
							|  |  |  |  | -- 打印表 | 
					
						
							|  |  |  |  | -- prints:print_t() | 
					
						
							|  |  |  |  | -- json解码 | 
					
						
							|  |  |  |  | -- json:decode() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 2023.7.5 | 
					
						
							|  |  |  |  | --     调用者可以设置 check_cfg_str 变量,此时脚本不会使用io函数而直接使用 | 
					
						
							|  |  |  |  | --     如果 check_cfg_str 为 nil 时自动读取 cfg_name 文件 | 
					
						
							|  |  |  |  | --     改为调用main函数来运行脚本 | 
					
						
							|  |  |  |  | -- 2023.7.14 | 
					
						
							|  |  |  |  | --     根据任务id找到下一个任务,只要判定相关任务顺序不变脚本就不受影响 | 
					
						
							|  |  |  |  | --     添加根据子错误找到对应的主错误功能 | 
					
						
							| 
									
										
										
										
											2023-07-20 18:00:29 +08:00
										 |  |  |  | -- 2023.7.20 | 
					
						
							|  |  |  |  | --     充能流程异常改为电容异常 | 
					
						
							| 
									
										
										
										
											2023-07-21 18:00:50 +08:00
										 |  |  |  | -- 2023.7.21 | 
					
						
							|  |  |  |  | --     修改检测项打印位置 | 
					
						
							| 
									
										
										
										
											2023-07-26 18:13:25 +08:00
										 |  |  |  | -- 2023.7.24 | 
					
						
							|  |  |  |  | --     读取芯片uid之后再次检测电流 | 
					
						
							|  |  |  |  | -- 2023.7.25 | 
					
						
							|  |  |  |  | --     上电电压超限时报检测器异常 | 
					
						
							| 
									
										
										
										
											2023-08-08 13:24:47 +08:00
										 |  |  |  | -- 2023.7.27 | 
					
						
							|  |  |  |  | --     改为位运算 | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 表长度 | 
					
						
							|  |  |  |  | function len(t) | 
					
						
							|  |  |  |  |     local res=0 | 
					
						
							|  |  |  |  |     if(t==nil)then return 0 end | 
					
						
							|  |  |  |  |     for k,v in pairs(t) do | 
					
						
							|  |  |  |  |         res=res+1 | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     return res | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 截取数组 | 
					
						
							|  |  |  |  | function mid(array,start,length) | 
					
						
							|  |  |  |  |     if(len(array)<(start+length-1)) then | 
					
						
							|  |  |  |  |         print("mid:out of range",start,length) | 
					
						
							|  |  |  |  |         return nil | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     return table.pack(table.unpack(array,start,start+length-1)) | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 把n插入t中,不允许重复 | 
					
						
							|  |  |  |  | function insert(t,n) | 
					
						
							|  |  |  |  |     for k,v in pairs(t) do | 
					
						
							|  |  |  |  |         if(v==n) then | 
					
						
							|  |  |  |  |             return | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     table.insert(t,n) | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 删除t中的值n | 
					
						
							|  |  |  |  | function del(t,n) | 
					
						
							|  |  |  |  |     for k,v in pairs(t) do | 
					
						
							|  |  |  |  |         if(v==n) then | 
					
						
							|  |  |  |  |             table.remove(t,k) | 
					
						
							|  |  |  |  |             break | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 删除t中除n之外的所有值 | 
					
						
							|  |  |  |  | function only(t,n) | 
					
						
							|  |  |  |  |     for k,v in pairs(t) do | 
					
						
							|  |  |  |  |         if(v~=n) then | 
					
						
							|  |  |  |  |             -- table.remove(t,k) | 
					
						
							|  |  |  |  |             t[k]=nil | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 计算2的n次幂 | 
					
						
							|  |  |  |  | function exp2(n) | 
					
						
							|  |  |  |  |     local ret=1 | 
					
						
							|  |  |  |  |     for i=1,n,1 do | 
					
						
							|  |  |  |  |         ret=ret*2 | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     return ret | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 计算参数错误占用的字节 | 
					
						
							|  |  |  |  | function clac_cfg_errbytenum() | 
					
						
							|  |  |  |  |     local num=0 | 
					
						
							|  |  |  |  |     local tasks=json_table["TaskArray"] | 
					
						
							|  |  |  |  |     for i=1,len(tasks),1 do | 
					
						
							|  |  |  |  |         num=num+tasks[i]["ReturnCount"] | 
					
						
							|  |  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-08-08 13:24:47 +08:00
										 |  |  |  |     -- return math.floor((num+7)/8) | 
					
						
							|  |  |  |  |     return (num+7)//8 | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  | 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 | 
					
						
							| 
									
										
										
										
											2023-08-08 13:24:47 +08:00
										 |  |  |  |     -- 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 | 
					
						
							|  |  |  |  |     if ((acks[(index-1)//8+1] & (1<<((index-1)%8)))~=0) then | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |         return false | 
					
						
							|  |  |  |  |     else | 
					
						
							|  |  |  |  |         return true | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 展示任务回复,0成功,1失败 | 
					
						
							|  |  |  |  | function show_acks(acks) | 
					
						
							|  |  |  |  |     -- prints:print_a(acks) | 
					
						
							|  |  |  |  |     print("return_ack:(0:ok;1:err)") | 
					
						
							|  |  |  |  |     local str="" | 
					
						
							|  |  |  |  |     for i=1,len(json_table["TaskArray"]),1 do | 
					
						
							|  |  |  |  |         local ack=find_ack(i,acks) | 
					
						
							|  |  |  |  |         if ack==true then | 
					
						
							|  |  |  |  |             str=str.."0, " | 
					
						
							|  |  |  |  |         else | 
					
						
							|  |  |  |  |             str=str.."1, " | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     print(str) | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 统计指定index之前应该跳过的数据长度和当前index的返回长度 | 
					
						
							|  |  |  |  | function calc_skip(index,tasks) | 
					
						
							|  |  |  |  |     local len=0 | 
					
						
							|  |  |  |  |     -- for i=1,table.getn(tasks),1 do | 
					
						
							|  |  |  |  |     local i=1 | 
					
						
							|  |  |  |  |     while i<index do | 
					
						
							|  |  |  |  |     -- for i=1,index,1 do | 
					
						
							|  |  |  |  |         -- print("calc_skip,index=",index,"i=",i) | 
					
						
							|  |  |  |  |         len=len+tasks[i]["ReturnCount"] | 
					
						
							|  |  |  |  |         i=i+1 | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     -- print("calc_skip,len=",len) | 
					
						
							|  |  |  |  |     return len*2,tasks[index]["ReturnCount"]*2 | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 找到指定index的任务返回数和值,把两个字节合并为一个值 | 
					
						
							|  |  |  |  | function find_return(index) | 
					
						
							|  |  |  |  |     local data=check_data | 
					
						
							|  |  |  |  |     local skip_len,len=calc_skip(index,json_table["TaskArray"]) | 
					
						
							|  |  |  |  |     local ret={} | 
					
						
							|  |  |  |  |     local temp=mid(data,skip_len+1+8+check_erryternum,len) | 
					
						
							|  |  |  |  |     if(temp==nil) then return ret end | 
					
						
							|  |  |  |  |     -- print("find_return,temp=") | 
					
						
							|  |  |  |  |     -- prints:print_a(temp) | 
					
						
							| 
									
										
										
										
											2023-08-08 13:24:47 +08:00
										 |  |  |  |     -- 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 | 
					
						
							|  |  |  |  |     for i=1,len//2,1 do | 
					
						
							|  |  |  |  |         ret[i]=temp[i*2-1]|(temp[i*2]<<8) | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |     end | 
					
						
							|  |  |  |  |     return ret | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- Checker_PowerPrapare,                        //0 电源准备   | 
					
						
							|  |  |  |  | -- XT_Test_PowerOn,                             //1 上电充能 | 
					
						
							|  |  |  |  | -- XT_Test_SetBusV,                             //2 设置总线电压 | 
					
						
							|  |  |  |  | -- XT_Test_BaseCur,                             //3 获取总线电流 | 
					
						
							|  |  |  |  | -- XT_Test_ScanUID,                             //4 扫描UID | 
					
						
							|  |  |  |  | -- XT_Test_WriteChipCfg,                        //5 写配置参数 | 
					
						
							|  |  |  |  | -- XT_Test_CheckChipCfg,                        //6 验证配置 | 
					
						
							|  |  |  |  | -- XT_Test_BindTestCode,                        //7 模拟注码 | 
					
						
							|  |  |  |  | -- XT_Test_ChgEnergy,                           //8 充能统计 | 
					
						
							|  |  |  |  | -- XT_Test_WriteRunField,                       //9 写现场值 网络id 延时 | 
					
						
							|  |  |  |  | -- XT_Test_CheckRunField,                       //10 比对现场值 | 
					
						
							|  |  |  |  | -- XT_Test_Resister,                            //11 桥丝通断检测 | 
					
						
							|  |  |  |  | -- XT_Test_Cap,                                 //12 电容容量统计 | 
					
						
							|  |  |  |  | -- Checker_WaitDelay,                           //13 延时等待 | 
					
						
							|  |  |  |  | -- XT_Test_WriteShell,                          //14 写管壳号/工厂信息 | 
					
						
							|  |  |  |  | -- XT_Test_WriteUID,                            //15 写UID | 
					
						
							|  |  |  |  | -- XT_Test_WritePWD,                            //16 写密码 | 
					
						
							|  |  |  |  | -- XT_Test_CheckBackFlag,                       //17 写入/检测备份区标志 | 
					
						
							|  |  |  |  | -- XT_Test_ReadBackFlag,                        //18 读取备份区数据 | 
					
						
							|  |  |  |  | -- XT_Test_Trim,                                //19 校准 | 
					
						
							|  |  |  |  | -- XT_Test_EnCommEndCur,                        //20 使能通讯末电流采集 | 
					
						
							|  |  |  |  | -- XT_Test_GetCommEndCur,                       //21 获取通讯末电流 | 
					
						
							|  |  |  |  | -- XT_Test_Discharge,                           //22 放电 | 
					
						
							|  |  |  |  | -- XT_Test_OneLineCheck,                        //23 在线检测 | 
					
						
							|  |  |  |  | -- XT_Test_CheckState,                          //24 状态检测 | 
					
						
							|  |  |  |  | -- XT_Test_Boom,                                //25 起爆 | 
					
						
							|  |  |  |  | -- XT_Test_Reset,                               //26 复位 | 
					
						
							|  |  |  |  | -- XT_Test_PowerOFF,                            //27 关总线	 | 
					
						
							|  |  |  |  | -- XT_Test_LockCmdC,                            //28 芯片锁存 | 
					
						
							|  |  |  |  | -- XT_Test_CodeBindEn,                          //29 使能赋码设备 | 
					
						
							|  |  |  |  | -- XT_Test_InputCheck,                          //30 在线检测 | 
					
						
							|  |  |  |  | -- XT_Test_CheckPWD,                            //31 密码验证 | 
					
						
							|  |  |  |  | -- XT_Test_LoadChipCfg,                         //32 加载芯片配置 | 
					
						
							|  |  |  |  | -- XT_Test_CapVoltage,                          //33 电容压差测试 | 
					
						
							|  |  |  |  | -- Checker_ResistorSample,                      //34 桥丝电阻测试 | 
					
						
							|  |  |  |  | -- XT_Test_UID_PWD_Bind,                        //35 检测过程中注码 | 
					
						
							|  |  |  |  | -- XT_Test_UID_PWD_Verify,                      //36 验证注码 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 根据任务id找到下一个任务 | 
					
						
							|  |  |  |  | function find_index(index,task_id) | 
					
						
							|  |  |  |  |     -- print("find_index:index=",index) | 
					
						
							|  |  |  |  |     local tasks=json_table["TaskArray"] | 
					
						
							|  |  |  |  |     -- prints:print_t(tasks) | 
					
						
							|  |  |  |  |     for i=index,len(tasks),1 do | 
					
						
							|  |  |  |  |         if(tasks[i]["TaskID"]==task_id) then | 
					
						
							|  |  |  |  |             return tasks[i] | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     print("find_index err:index=",index,"task_id=",task_id) | 
					
						
							|  |  |  |  |     return nil | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 检测器异常,1 | 
					
						
							|  |  |  |  | -- 主电容异常,2 | 
					
						
							|  |  |  |  | -- 接触异常,3 | 
					
						
							|  |  |  |  | -- 桥丝阻值异常,4 | 
					
						
							|  |  |  |  | -- 芯片异常,5 | 
					
						
							|  |  |  |  | -- 其他异常,6 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 判断上电错误,返回下一个检测任务,nil停止检测 | 
					
						
							|  |  |  |  | function Checker_PowerPrapare(task,err_code) | 
					
						
							|  |  |  |  |     if(find_ack(task["TaskIndex"]+1,mid(check_data,1,8))==false)then | 
					
						
							|  |  |  |  |         -- 上电错误只能是检测器异常,检测结束 | 
					
						
							| 
									
										
										
										
											2023-07-21 18:00:50 +08:00
										 |  |  |  |         print("PowerPrapare failed.index=",task["TaskIndex"]) | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |         only(err_code,1) | 
					
						
							|  |  |  |  |         return nil | 
					
						
							|  |  |  |  |     else | 
					
						
							|  |  |  |  |         -- 上电正常,排除检测器异常 | 
					
						
							|  |  |  |  |         -- del(err_code,1) | 
					
						
							|  |  |  |  |         -- 下一步是上电充能 | 
					
						
							|  |  |  |  |         return find_index(task["TaskIndex"]+2,1) | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 判断上电充能错误,返回下一个检测任务,nil停止检测 | 
					
						
							|  |  |  |  | function XT_Test_PowerOn(task,err_code) | 
					
						
							|  |  |  |  |     local ret_value={} | 
					
						
							|  |  |  |  |     if(find_ack(task["TaskIndex"]+1,mid(check_data,1,8))==false)then | 
					
						
							|  |  |  |  |         -- 上电充能异常 | 
					
						
							|  |  |  |  |         del(err_code,0) | 
					
						
							| 
									
										
										
										
											2023-07-21 18:00:50 +08:00
										 |  |  |  |         print("poweron failed.index=",task["TaskIndex"]) | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |         -- 下一步检测电流 | 
					
						
							|  |  |  |  |         return find_index(task["TaskIndex"]+2,3) | 
					
						
							|  |  |  |  |     else | 
					
						
							|  |  |  |  |         -- 充能流程正常,返回第一个值是电压,第二个值是充能时间 | 
					
						
							|  |  |  |  |         ret_value=find_return(task["TaskIndex"]+1) | 
					
						
							|  |  |  |  |         prints:print_a(ret_value,2) | 
					
						
							|  |  |  |  |         -- 电压无法上升,检测器异常 | 
					
						
							| 
									
										
										
										
											2023-07-26 18:13:25 +08:00
										 |  |  |  |         if(ret_value[1]<task["TestStandard"][1]["Min"]) or (ret_value[1]>task["TestStandard"][1]["Max"]) then | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |             print("voltage too low",ret_value[1]) | 
					
						
							|  |  |  |  |             only(err_code,1) | 
					
						
							|  |  |  |  |             return nil | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |         -- 充电时间过长,进一步检测电流,判断开路还是短路 | 
					
						
							|  |  |  |  |         if(ret_value[2]>39000) then | 
					
						
							|  |  |  |  |             del(err_code,0) | 
					
						
							|  |  |  |  |             return find_index(task["TaskIndex"]+2,3) | 
					
						
							|  |  |  |  |         elseif(ret_value[2]<1) then | 
					
						
							|  |  |  |  |         -- 新小板程序在开路时会返回0,保险起见还是看一下电流 | 
					
						
							|  |  |  |  |             del(err_code,0) | 
					
						
							|  |  |  |  |             return find_index(task["TaskIndex"]+2,3) | 
					
						
							|  |  |  |  |         end | 
					
						
							| 
									
										
										
										
											2023-07-21 18:00:50 +08:00
										 |  |  |  |         -- 排除检测板异常 | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |         del(err_code,1) | 
					
						
							| 
									
										
										
										
											2023-07-21 18:00:50 +08:00
										 |  |  |  |         -- 下一步检测电流 | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |         return find_index(task["TaskIndex"]+2,3) | 
					
						
							| 
									
										
										
										
											2023-07-21 18:00:50 +08:00
										 |  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 扫描uid,成功则芯片正常,返回下一个检测任务,nil停止检测 | 
					
						
							|  |  |  |  | function XT_Test_ScanUID(task,err_code) | 
					
						
							| 
									
										
										
										
											2023-07-21 18:00:50 +08:00
										 |  |  |  |     print("read chip UID") | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |     if(find_ack(task["TaskIndex"]+1,mid(check_data,1,8))==false)then | 
					
						
							|  |  |  |  |         -- 通信失败,返回芯片异常,检测结束 | 
					
						
							|  |  |  |  |         print("task failed.task_index=",task["TaskIndex"]) | 
					
						
							|  |  |  |  |         only(err_code,5) | 
					
						
							|  |  |  |  |         return nil | 
					
						
							|  |  |  |  |     else | 
					
						
							|  |  |  |  |         -- ret_value=find_return(task["TaskIndex"]+1) | 
					
						
							|  |  |  |  |         -- -- 最大反馈时间在范围之外,芯片异常 | 
					
						
							|  |  |  |  |         -- if(ret_value[2]<task["TestStandard"][2]["Min"]) or (ret_value[2]>task["TestStandard"][2]["Max"]) then | 
					
						
							|  |  |  |  |         --     only(err_code,5) | 
					
						
							|  |  |  |  |         --     return nil | 
					
						
							|  |  |  |  |         -- end | 
					
						
							| 
									
										
										
										
											2023-07-26 18:13:25 +08:00
										 |  |  |  |         -- 充能统计 | 
					
						
							|  |  |  |  | 		-- return find_index(task["TaskIndex"]+2,8) | 
					
						
							|  |  |  |  |         -- 再次检测电流 | 
					
						
							|  |  |  |  |         return find_index(task["TaskIndex"]+2,3) | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |     end | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 判断低压电流,返回下一个检测任务,nil停止检测 | 
					
						
							|  |  |  |  | function XT_Test_BaseCurLow(task,err_code) | 
					
						
							| 
									
										
										
										
											2023-07-21 18:00:50 +08:00
										 |  |  |  |     print("check current") | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |     if(find_ack(task["TaskIndex"]+1,mid(check_data,1,8))==false)then | 
					
						
							|  |  |  |  |         -- 电流检测错误只能是检测器异常,检测结束 | 
					
						
							|  |  |  |  |         print("task failed.task_index=",task["TaskIndex"]) | 
					
						
							|  |  |  |  |         only(err_code,1) | 
					
						
							|  |  |  |  |         return nil | 
					
						
							|  |  |  |  |     else | 
					
						
							|  |  |  |  |         -- 返回正向电流,反向电流 | 
					
						
							|  |  |  |  |         ret_value=find_return(task["TaskIndex"]+1) | 
					
						
							|  |  |  |  | 		print("current:",ret_value[1]) | 
					
						
							|  |  |  |  |         -- 电流为0,开路,返回接触异常 | 
					
						
							|  |  |  |  |         if(ret_value[1]<10) and (ret_value[2]<10)then | 
					
						
							|  |  |  |  |             only(err_code,3) | 
					
						
							|  |  |  |  |             return nil | 
					
						
							|  |  |  |  |         -- 电流过大,短路,返回芯片异常 | 
					
						
							|  |  |  |  |         elseif((ret_value[1]>task["TestStandard"][1]["Max"]) and (ret_value[1]<1500)) | 
					
						
							|  |  |  |  | 			or ((ret_value[2]>task["TestStandard"][2]["Max"]) and (ret_value[2]<1500)) | 
					
						
							|  |  |  |  | 		then | 
					
						
							|  |  |  |  |             only(err_code,5) | 
					
						
							|  |  |  |  |             return nil | 
					
						
							|  |  |  |  |         elseif((ret_value[1]>1500) and (ret_value[2]>1500))then | 
					
						
							|  |  |  |  |             only(err_code,3) | 
					
						
							|  |  |  |  |             return nil | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |         -- 排除接触异常 | 
					
						
							|  |  |  |  |         del(err_code,3) | 
					
						
							| 
									
										
										
										
											2023-07-26 18:13:25 +08:00
										 |  |  |  |         -- 下一步检测芯片,如果没有芯片则检测充能 | 
					
						
							|  |  |  |  |         local next=find_index(task["TaskIndex"]+2,4) | 
					
						
							|  |  |  |  |         if(next==nil) then | 
					
						
							|  |  |  |  |             next=find_index(task["TaskIndex"]+2,8) | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |         return next | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |     end | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 检测充能,失败则电容异常 | 
					
						
							|  |  |  |  | function JQ_Test_ChgEnergy(task,err_code) | 
					
						
							| 
									
										
										
										
											2023-07-21 18:00:50 +08:00
										 |  |  |  |     print("check charge") | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |     if(find_ack(task["TaskIndex"]+1,mid(check_data,1,8))==false)then | 
					
						
							|  |  |  |  |         -- 充能错误只能是检测器异常,检测结束 | 
					
						
							| 
									
										
										
										
											2023-07-20 18:00:29 +08:00
										 |  |  |  |         print("task capacitance Energy failed.task_index=",task["TaskIndex"]) | 
					
						
							|  |  |  |  |         only(err_code,2) | 
					
						
							| 
									
										
										
										
											2023-07-14 18:50:38 +08:00
										 |  |  |  |         return nil | 
					
						
							|  |  |  |  |     else | 
					
						
							|  |  |  |  |         ret_value=find_return(task["TaskIndex"]+1) | 
					
						
							|  |  |  |  |         if((ret_value[1]>task["TestStandard"][1]["Max"]) or (ret_value[1]<task["TestStandard"][1]["Min"])) then | 
					
						
							|  |  |  |  |             only(err_code,2) | 
					
						
							|  |  |  |  |             return nil | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |         if((ret_value[2]>task["TestStandard"][2]["Max"]) or (ret_value[2]<task["TestStandard"][2]["Min"])) then | 
					
						
							|  |  |  |  |             only(err_code,2) | 
					
						
							|  |  |  |  |             return nil | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |         if((ret_value[3]>task["TestStandard"][3]["Max"]) or (ret_value[3]<task["TestStandard"][3]["Min"])) then | 
					
						
							|  |  |  |  |             only(err_code,2) | 
					
						
							|  |  |  |  |             return nil | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |         print("judge end") | 
					
						
							|  |  |  |  |         only(err_code,0) | 
					
						
							|  |  |  |  |         return nil | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 定义占位函数,不应该调用 | 
					
						
							|  |  |  |  | function JQ_Test_Empty(task,err_code) | 
					
						
							|  |  |  |  |     print("err function call,task index=",task["TaskIndex"]) | 
					
						
							|  |  |  |  |     insert(err_code,6) | 
					
						
							|  |  |  |  |     return nil | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 这个数组根据方案检测顺序确定 | 
					
						
							|  |  |  |  | tasks_judge_fun={ | 
					
						
							|  |  |  |  |     Checker_PowerPrapare,--0 | 
					
						
							|  |  |  |  |     XT_Test_PowerOn,--1 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,--2 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,--3 | 
					
						
							|  |  |  |  |     XT_Test_BaseCurLow,--4 | 
					
						
							|  |  |  |  |     XT_Test_ScanUID,--5 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,--6 | 
					
						
							|  |  |  |  |     JQ_Test_ChgEnergy,--7 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,--8 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,--9 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,--10 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,--11 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,--12 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,--13 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 这个数组根据方案id确定 | 
					
						
							|  |  |  |  | tasks_judge_fun_id={ | 
					
						
							|  |  |  |  |     Checker_PowerPrapare,                       -- //0 电源准备   | 
					
						
							|  |  |  |  |     XT_Test_PowerOn,                            -- //1 上电充能 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,                            -- //2 设置总线电压 | 
					
						
							|  |  |  |  |     XT_Test_BaseCurLow,                         -- //3 获取总线电流 | 
					
						
							|  |  |  |  |     XT_Test_ScanUID,                            -- //4 扫描UID | 
					
						
							|  |  |  |  |     JQ_Test_Empty,                       -- //5 写配置参数 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,                       -- //6 验证配置 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,                       -- //7 模拟注码 | 
					
						
							|  |  |  |  |     JQ_Test_ChgEnergy,                          -- //8 充能统计 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,                      -- //9 写现场值 网络id 延时 | 
					
						
							|  |  |  |  |     JQ_Test_Empty,                      -- //10 比对现场值 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 取得表中第一个值 | 
					
						
							|  |  |  |  | function get_err_code(err_code) | 
					
						
							|  |  |  |  |     for k,v in pairs(err_code) do | 
					
						
							|  |  |  |  |         if(v~=nil) then | 
					
						
							|  |  |  |  |             return v | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     return nil | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 根据任务依次判断 | 
					
						
							|  |  |  |  | function judge() | 
					
						
							|  |  |  |  |     local fun_index=1 | 
					
						
							|  |  |  |  |     local fun_id=0 | 
					
						
							|  |  |  |  |     local err_code={0,1,2,3,4,5} | 
					
						
							|  |  |  |  |     -- 找到上电任务 | 
					
						
							|  |  |  |  |     local task=find_index(1,0) | 
					
						
							|  |  |  |  |     while true do | 
					
						
							|  |  |  |  |         if(task==nil)then | 
					
						
							|  |  |  |  |             if(len(err_code)>1)then | 
					
						
							|  |  |  |  |                 -- 如果找不到任务,则应该是方案错误,这里返回其他异常 | 
					
						
							|  |  |  |  |                 print("can not find task,fun_index=",fun_index) | 
					
						
							|  |  |  |  |                 insert(err_code,6) | 
					
						
							|  |  |  |  |             end | 
					
						
							|  |  |  |  |             break | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |         -- fun_index=task["TaskIndex"]+1 | 
					
						
							|  |  |  |  |         -- task=tasks_judge_fun[fun_index](task,err_code) | 
					
						
							|  |  |  |  |         -- 通过任务id来找到判定函数 | 
					
						
							|  |  |  |  |         fun_index=task["TaskIndex"]+1 | 
					
						
							|  |  |  |  |         fun_id=task["TaskID"]+1 | 
					
						
							|  |  |  |  |         task=tasks_judge_fun_id[fun_id](task,err_code) | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     return err_code | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 判断一个任务的参数错误 | 
					
						
							|  |  |  |  | function judge_task(task,data) | 
					
						
							|  |  |  |  |     local range=task["TestStandard"] | 
					
						
							|  |  |  |  |     local ret={} | 
					
						
							|  |  |  |  |     -- print("judge task") | 
					
						
							|  |  |  |  |     if range~=nil then | 
					
						
							|  |  |  |  |         -- print("judge task") | 
					
						
							|  |  |  |  |         for i=1,len(range),1 do | 
					
						
							|  |  |  |  |             -- prints:print_t(range[i]) | 
					
						
							|  |  |  |  |             -- print("data=",data[i]) | 
					
						
							|  |  |  |  |             if(data[i]<range[i]["Min"]) or (data[i]>range[i]["Max"]) then | 
					
						
							|  |  |  |  |                 -- print("task=",task["TaskBrief"],"out of range:") | 
					
						
							|  |  |  |  |                 -- print("err code=",task["ResultErrCode"][i]) | 
					
						
							|  |  |  |  |                 table.insert(ret,task["ResultErrCode"][i]) | 
					
						
							|  |  |  |  |             end | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     return ret | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 依次判断流程错误 | 
					
						
							|  |  |  |  | function judge_list() | 
					
						
							|  |  |  |  |     local acks=mid(check_data,1,8) | 
					
						
							|  |  |  |  |     for i=1,len(json_table["TaskArray"]),1 do | 
					
						
							|  |  |  |  |         local ack=find_ack(i,acks) | 
					
						
							|  |  |  |  |         local task_p=json_table["TaskArray"][i] | 
					
						
							|  |  |  |  |         local ret_value=find_return(i) | 
					
						
							|  |  |  |  |         -- print("taskindex:",task_p["TaskIndex"]) | 
					
						
							|  |  |  |  |         local err_code=judge_task(task_p,ret_value) | 
					
						
							|  |  |  |  |         -- print("err_code:") | 
					
						
							|  |  |  |  |         -- prints:print_a(err_code) | 
					
						
							|  |  |  |  |         if ack==false then | 
					
						
							|  |  |  |  |             table.insert(err_code,task_p["ExecuteErrCode"]) | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |         if len(err_code)>0 then | 
					
						
							|  |  |  |  |             -- print("task:",task_p["TaskBrief"],"err_code:") | 
					
						
							|  |  |  |  |             prints:print_a(err_code) | 
					
						
							|  |  |  |  |             return get_err_code(err_code) | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     return nil | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 根据子错误找到对应的主错误 | 
					
						
							|  |  |  |  | function find_majer_err(sub_err) | 
					
						
							|  |  |  |  |     for i=1,len(json_table["MajorErrInfo"]),1 do | 
					
						
							|  |  |  |  |         local sub_list=json_table["MajorErrInfo"][i]["SubErrCode"] | 
					
						
							|  |  |  |  |         for j=1,len(sub_list),1 do | 
					
						
							|  |  |  |  |             if sub_err==sub_list[j] then | 
					
						
							|  |  |  |  |                 return json_table["MajorErrInfo"][i]["Info"],json_table["MajorErrInfo"][i]["MajorErrCode"] | 
					
						
							|  |  |  |  |             end | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     return "具体错误待定义",sub_err | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 要校验小板的版本号,方案的版本号 | 
					
						
							|  |  |  |  | -- 脚本产生的异常从210开始,0xd2 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | function check_env() | 
					
						
							|  |  |  |  |     -- 校验输入数据的长度 | 
					
						
							|  |  |  |  |     local skip,l=calc_skip(len(json_table["TaskArray"]),json_table["TaskArray"]) | 
					
						
							|  |  |  |  |     -- print("requier len=",skip+l) | 
					
						
							|  |  |  |  |     local num_requre=skip+l+8+check_erryternum | 
					
						
							|  |  |  |  |     if(num_requre>len(check_data)) then | 
					
						
							|  |  |  |  |         print("check data too less.",num_requre,len(check_data)) | 
					
						
							|  |  |  |  |         return "数据长度与方案不符",210 | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     -- if(json_table["PlanID"]~=PLAN_ID) then | 
					
						
							|  |  |  |  |     --     print("plan id check err.") | 
					
						
							|  |  |  |  |     --     return "方案ID不符",211 | 
					
						
							|  |  |  |  |     -- end | 
					
						
							|  |  |  |  |     return "ok",0 | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | function main() | 
					
						
							|  |  |  |  |     -- 读取方案配置信息 | 
					
						
							|  |  |  |  |     -- print(cfg_name) | 
					
						
							|  |  |  |  |     if(check_cfg_str==nil)then | 
					
						
							|  |  |  |  |         check_cfg_file=io.open(cfg_name,"r") | 
					
						
							|  |  |  |  |         check_cfg_str=check_cfg_file:read("*a") | 
					
						
							|  |  |  |  |         check_cfg_file:close() | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     json_table = json:decode(check_cfg_str) | 
					
						
							|  |  |  |  |     check_erryternum=clac_cfg_errbytenum() | 
					
						
							|  |  |  |  |     local err_str_table={"检测器异常","主电容异常","接触异常","桥丝阻值异常","芯片异常"} | 
					
						
							|  |  |  |  |     local ch_err,ch_code=check_env() | 
					
						
							|  |  |  |  |     if(ch_code~=0) then | 
					
						
							|  |  |  |  |         return ch_err,ch_code | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     -- 展示任务执行结果 | 
					
						
							|  |  |  |  |     show_acks(mid(check_data,1,8)) | 
					
						
							|  |  |  |  |     -- 开始判定 | 
					
						
							|  |  |  |  |     local err_code=judge() | 
					
						
							|  |  |  |  |     if(len(err_code)>1) then | 
					
						
							|  |  |  |  | 		prints:print_a(err_code) | 
					
						
							|  |  |  |  |         return "检测项目不足,无法判定",212 | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  |     prints:print_a(err_code) | 
					
						
							|  |  |  |  |     local err=get_err_code(err_code) | 
					
						
							|  |  |  |  |     if(err~=nil) then | 
					
						
							|  |  |  |  |         if(err==0) then | 
					
						
							|  |  |  |  |             err=judge_list() | 
					
						
							|  |  |  |  |             if(err==nil)then | 
					
						
							|  |  |  |  |                 return "ok",0 | 
					
						
							|  |  |  |  |             else | 
					
						
							|  |  |  |  |                 return find_majer_err(err) | 
					
						
							|  |  |  |  |             end | 
					
						
							|  |  |  |  |         else | 
					
						
							|  |  |  |  |             return err_str_table[err],err | 
					
						
							|  |  |  |  |         end | 
					
						
							|  |  |  |  |     else | 
					
						
							|  |  |  |  |         print("err:err_code is none.") | 
					
						
							|  |  |  |  |         return "数据不合规",213 | 
					
						
							|  |  |  |  |     end | 
					
						
							|  |  |  |  | end | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | -- 先返回错误描述,再返回错误码 | 
					
						
							|  |  |  |  | -- return main() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 |