LUA calls the C function has an array of parameters

LUA sends a packet to the serial port of the data, before transmission needs to be verified, the verification done in C, so to transfer to the C side of an array, as follows:

1. C code check function as

//--- lua中以数组传递参数
static int calc_chksum(lua_State *L)
{
 	int 	i;
	int 	DatLen;
	unsigned char *DatInf;
	unsigned short crc;
	 
	if(!lua_istable(L, 1)){					//判断第一个参数是否为表,否则退出
        lua_pushnil(L);
        return -1;
    }
	DatLen = luaL_checknumber(L,2);			//获取第二个参数,数据长度
	DatInf =(unsigned char *)malloc(DatLen);
	for(i=1; i<=DatLen; i++){
		lua_rawgeti(L, 1, i);
		DatInf[i-1] = lua_tointeger(L, -1);
		lua_pop(L, 1);						//把上一个内容的出栈
	}
	crc = CalcChkSum(DatInf, DatLen);		//计算校验值
	lua_pushinteger(L,(crc>>8)&0xff);		//校验值高位压栈
	lua_pushinteger(L,(crc)&0xff);			//校验值低位压栈
	free(DatInf);
    return 2;   							//返回参数个数
}

2. LUA code is as follows

function collection_data(if_id, ipaddr, port, devaddr)                                                                                                                                                                          
                                                                                                                                                                                                                                
        print("if_id:", if_id);                                                                                                                                                                                                 
        print("ipaddr:", ipaddr);                                                                                                                                                                                               
        print("ip port:", port);                                                                                                                                                                                                
        print("dev addr", devaddr);                                                                                                                                                                                             
                                                                                                                                                                                                                                
        cmd = {devaddr, 0x04, 0x00, 0x00, 0x00, 0x02};                                                                                                                                                                          
        crc1, crc2  = dev_lib.calc_chksum(cmd, 6);          //调用C函数完成数组的计算得到CRC的高低字节                                                                                                                                                                           
        cmd[6] = crc1;                                                                                                                                                                                                          
        cmd[7] = crc2;                                                                                                                                                                                                          
        dev_lib.write_data(if_id, cmd, 8);                  //最后通过接口发送出去                                                                                                                                                                                  
                                                                                                                                                                                                                                
end            

 

Published 106 original articles · won praise 143 · views 550 000 +

Guess you like

Origin blog.csdn.net/shjhuang/article/details/104273145