/************************************************************************* Things to put in your header file *************************************************************************/ #include "usbi2cio.h" extern HANDLE hDevInstance; // stores handle to UsbI2cIo device extern I2C_TRANS TransI2c; // I2C transaction structure // the following are used for explicit linking to UsbI2cIo API DLL functions typedef WORD(CALLBACK* GETDLLVERSION)(void); typedef HANDLE(CALLBACK* OPENDEVICEINSTANCE)(LPSTR, BYTE); typedef HANDLE(CALLBACK* OPENDEVICEBYSERIALID)(LPSTR, LPSTR); typedef BOOL(CALLBACK* CLOSEDEVICEINSTANCE)(HANDLE); typedef BOOL(CALLBACK* CONFIGIOPORTS)(HANDLE, ULONG); typedef BOOL(CALLBACK* READIOPORTS)(HANDLE, LPLONG); typedef BOOL(CALLBACK* WRITEIOPORTS)(HANDLE, ULONG, ULONG); typedef LONG(CALLBACK* WRITEI2C)(HANDLE, I2C_TRANS*); typedef LONG(CALLBACK* READI2C)(HANDLE, I2C_TRANS*); extern HINSTANCE hDLL; // stores handle to UsbI2cIo dll // function pointers to API functions in UsbI2cIo dll extern GETDLLVERSION GetDllVersion; extern OPENDEVICEINSTANCE OpenDeviceInstance; extern OPENDEVICEBYSERIALID OpenDeviceBySerialId; extern CLOSEDEVICEINSTANCE CloseDeviceInstance; extern CONFIGIOPORTS ConfigIoPorts; extern READIOPORTS ReadIoPorts; extern WRITEIOPORTS WriteIoPorts; extern WRITEI2C WriteI2c; extern READI2C ReadI2c; /************************************************************************* Things to put in your c file *************************************************************************/ // globals HANDLE hDevInstance = INVALID_HANDLE_VALUE; // handle to UsbI2cIo device I2C_TRANS TransI2c; // I2C transaction structure HINSTANCE hDLL; // handle to UsbI2cIo dll // initialization code hDLL = LoadLibrary("UsbI2cIo.dll"); // attempt load dll if( hDLL != NULL) { // got handle to dll, now initialize function pointers GetDllVersion=(GETDLLVERSION)GetProcAddress(hDLL,"DAPI_GetDllVersion"); OpenDeviceInstance=(OPENDEVICEINSTANCE)GetProcAddress(hDLL,"DAPI_OpenDeviceInstance"); OpenDeviceBySerialId=(OPENDEVICEBYSERIALID)GetProcAddress(hDLL,"DAPI_OpenDeviceBySerialId"); CloseDeviceInstance=(CLOSEDEVICEINSTANCE)GetProcAddress(hDLL,"DAPI_CloseDeviceInstance"); ConfigIoPorts=(CONFIGIOPORTS)GetProcAddress(hDLL,"DAPI_ConfigIoPorts"); ReadIoPorts=(READIOPORTS)GetProcAddress(hDLL,"DAPI_ReadIoPorts"); WriteIoPorts=(WRITEIOPORTS)GetProcAddress(hDLL,"DAPI_WriteIoPorts"); WriteI2c=(WRITEI2C)GetProcAddress(hDLL,"DAPI_WriteI2c"); ReadI2c=(READI2C)GetProcAddress(hDLL,"DAPI_ReadI2c"); } else { // unable to get handle to dll, no sense continuing, exit application } /************************************************************************* how to open a handle to a specific UsbI2cIo device (use your serial number) *************************************************************************/ hDevInstance = OpenDeviceBySerialId("UsbI2cIo","01010004"); if(hDevInstance == INVALID_HANDLE_VALUE) { // error if here } /************************************************************************* how to do an I2C Write *************************************************************************/ long lBytesWritten; // used to hold return value // setup for I2C communication TransI2C.bySlvDevAddr = 0x42; // put your device id here TransI2C.byTransType = I2C_TRANS_NOADR; // device does not use sub-address TransI2C.wMemoryAddr = 0; // unused for I2C_TRANS_NOADR TransI2C.wCount = 1; // transfer 1 byte TransI2C.Data[0] = 0x71; // data to transfer // call the API function lBytesWritten = WriteI2c(hStations[0], &TransI2C); if(lBytesWritten != TransI2c.wCount) { // error if here, number actually written doesn't equal requested } /************************************************************************* how to do an I2C Read *************************************************************************/ long lBytesRead; // used to hold return value // setup for I2C communication TransI2C.bySlvDevAddr = 0x42; // put your device id here TransI2C.byTransType = I2C_TRANS_NOADR; // device does not use sub-address TransI2C.wMemoryAddr = 0; // unused for I2C_TRANS_NOADR TransI2C.wCount = 1; // transfer 1 byte // call the API function lBytesRead = ReadI2c(hStations[0], &TransI2C); if(lBytesRead != TransI2c.wCount) { // error if here, number actually read doesn't equal requested }