分类: C/C++
2008-08-05 14:01:44
hT2C = CreateFile(TEXT("I2C1:"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hT2C == INVALID_HANDLE_VALUE) { if (GetLastError() == ERROR_DEV_NOT_EXIST) { // register I2C device driver hDriver = RegisterDevice(TEXT("I2C"), 1, TEXT("I2C.DLL"), 0); if (!hDriver) { RETAILMSG(1, (TEXT("Failed to register I2C device driver.\r\n"))); } hT2C = CreateFile(TEXT("I2C1:"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); } if (hT2C == INVALID_HANDLE_VALUE) { dwErrorCode = GetLastError(); return dwErrorCode ; } } //set mode: master dwIoControlCode = I2C_MASTER_MODE;//这个值CPU厂家给 DeviceIoControl(hT2C, IOCTL_I2C_SET_MODE, (PVOID)&dwIoControlCode, sizeof(DWORD), NULL, 0, &dwRet, NULL); //set baudrate: DeviceIoControl(hT2C, IOCTL_I2C_SET_BAUDRATE,//dwData CPU厂家给 (PVOID)&dwData, sizeof(DWORD), NULL, 0, &dwRet, NULL); //set buffer mode: single dwIoControlCode = I2C_SINGLE_STAGE; DeviceIoControl(hT2C, IOCTL_I2C_SET_BUFFER_MODE,//dwIoControlCode CPU厂家给 (PVOID)&dwIoControlCode, sizeof(DWORD), NULL, 0, &dwRet, NULL);二:写I2C数据
//写校验码 WriteFile ( hT2C, BUFFER, writesize, &wRetSize, NULL); //判断存储是否成功 if (!wRetSize) { dwErrorCode = GetLastError(); CloseHandle( hT2C ); return 1; }这里得说明一下,writefile的第5个参数必须为NULL,因为CE系统是不支持重叠的。writefile是BOOL型的,刚开始我用writefile的返回值来判断操作是否成功,结果判断结果根本不准确,用writefile完成之后实际写的大小来判断操作是否成功才是最准确的。