QLibrary myLib("VT_I2C_Driver.dll");//声明所用到的dll
if(myLib.load()){//判断dll加载正确
Sleep(10);
//引用dll库函数
VT_USB_EnumBoard=(ty_VT_USB_EnumBoard)myLib.resolve("VT_USB_EnumBoard");
VT_USB_SelectBoard=(ty_VT_USB_SelectBoard)myLib.resolve("VT_USB_SelectBoard");
I2C_Init=(ty_I2C_Init)myLib.resolve("VT_I2C_Init");
I2C_WriteData=(ty_I2C_WriteData)myLib.resolve("VT_I2C_WriteData");
I2C_ReadData=(ty_I2C_ReadData)myLib.resolve("VT_I2C_ReadData");
I2C_GetLogData=(ty_I2C_GetLogData)myLib.resolve("VT_I2C_GetLogData");
}else{
QMessageBox::warning(this,"警告","加载动态链接库VT_I2C_Driver.dll失败!",QMessageBox::Ok);
return ;
}
//判断引用dll库函数是否成功
if((I2C_Init==NULL)||(I2C_WriteData==NULL)||(I2C_ReadData==NULL)
||(I2C_GetLogData==NULL)||(VT_USB_EnumBoard==NULL)||(VT_USB_SelectBoard==NULL)){
QMessageBox::warning(this,"警告","加载动态链接库VT_I2C_Driver.dll失败!",QMessageBox::Ok);
return ;
}
int devNum=VT_USB_EnumBoard(vt_board,10);
for(int i=0;i<devNum;i++){
if(vt_board[i].bUsed==0){
VT_USB_SelectBoard(vt_board[i].nBoardID);
I2C_Data.deviceNumber=vt_board[i].nBoardID;
break;
}
}
/*根据传感器初始化I2C控制器*/
I2C_Data.p_getDataBuff=getDataBuf; //初始化接收数据缓冲区指针
I2C_Data.p_sendDataBuff=sendDataBuf; //初始化发送数据缓冲区指针
I2C_Data.SerialNumber=0; //将数据包序号清零
I2C_Data.i2cInitInfo.controlMode=1; //硬件控制
I2C_Data.i2cInitInfo.slaveAddr=0x3A; //传感器地址为0x3A
I2C_Data.i2cInitInfo.speed=100000; //读写速度为100000HZ
I2C_Data.i2cInitInfo.subAddrMode=1; //有子地址
I2C_Data.i2cInitInfo.subAddrWidth=1; //子地址宽度为1字节
I2C_Init(&I2C_Data); //利用初始化函数指针调用初始化函数
/*初始化传感器*/
I2C_Data.subAddr=0x16; //子地址为0x16
I2C_Data.dataLenght=1; //数据长度为1字节
sendDataBuf[0]=0x45; //传感器模式控制,测量模式,2g量程
if(0==I2C_WriteData(&I2C_Data)){//判断写入数据是否成功
}else{
QMessageBox::warning(this,"警告","发送数据失败!",QMessageBox::Ok);
I2C_Data.SerialNumber=0;
needInitFlag=1;
return ;
}