void extendedChecksum(std::array<unsigned char, MAXIMUM_BUFFER>* bytes, int count) { uint16_t accumulator = extendedChecksum16(*bytes, count); (*bytes)[4] = static_cast<unsigned char>(accumulator & 0xff); (*bytes)[5] = static_cast<unsigned char>((accumulator / 256) & 0xff); (*bytes)[0] = extendedChecksum8(*bytes); }
void extendedChecksum(uint8 *b, int n) { uint16 a; a = extendedChecksum16(b,n); b[4] = (uint8)(a & 0xff); b[5] = (uint8)((a/256) & 0xff); b[0] = extendedChecksum8(b); }
int errorCheck(uint8 *buffer) { uint16 checksumTotal; checksumTotal = extendedChecksum16(buffer, 40); if( (uint8)((checksumTotal / 256) & 0xff) != buffer[5]) { printf("Error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != buffer[4]) { printf("Error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(buffer) != buffer[0]) { printf("Error : read buffer has bad checksum8\n"); return -1; } if(buffer[1] != (uint8)(0xF8) || buffer[2] != (uint8)(0x11) || buffer[3] != (uint8)(0x18)) { printf("Error : read buffer has wrong command bytes \n"); return -1; } if( buffer[6] != 0) { printf("Errorcode (byte 6): %d\n", (unsigned int)buffer[6]); return -1; } return 0; }
//Sends a StreamConfig low-level command to configure the stream. int StreamConfig_example(HANDLE hDevice) { int sendBuffSize; sendBuffSize = 14+NumChannels*2; uint8 sendBuff[sendBuffSize], recBuff[8]; int sendChars, recChars; uint16 checksumTotal; uint16 scanInterval; int i; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = 4 + NumChannels; //Number of data words = NumChannels + 4 sendBuff[3] = (uint8)(0x11); //Extended command number sendBuff[6] = NumChannels; //NumChannels sendBuff[7] = 1; //ResolutionIndex sendBuff[8] = SamplesPerPacket; //SamplesPerPacket sendBuff[9] = 0; //Reserved sendBuff[10] = 0; //SettlingFactor: 0 sendBuff[11] = 0; //ScanConfig: // Bit 3: Internal stream clock frequency = b0: 4 MHz // Bit 1: Divide Clock by 256 = b0 scanInterval = 4000; sendBuff[12] = (uint8)(scanInterval&(0x00FF)); //scan interval (low byte) sendBuff[13] = (uint8)(scanInterval/256); //scan interval (high byte) for( i = 0; i < NumChannels; i++ ) { sendBuff[14 + i*2] = i; //ChannelNumber (Positive) = i sendBuff[15 + i*2] = 0; //ChannelOptions: // Bit 7: Differential = 0 // Bit 5-4: GainIndex = 0 (+-10V) } extendedChecksum(sendBuff, sendBuffSize); //Sending command to U6 sendChars = LJUSB_Write(hDevice, sendBuff, sendBuffSize); if( sendChars < sendBuffSize ) { if( sendChars == 0 ) printf("Error : write failed (StreamConfig).\n"); else printf("Error : did not write all of the buffer (StreamConfig).\n"); return -1; } for( i = 0; i < 8; i++ ) recBuff[i] = 0; //Reading response from U6 recChars = LJUSB_Read(hDevice, recBuff, 8); if( recChars < 8 ) { if( recChars == 0 ) printf("Error : read failed (StreamConfig).\n"); else printf("Error : did not read all of the buffer, %d (StreamConfig).\n", recChars); for( i = 0; i < 8; i++) printf("%d ", recBuff[i]); return -1; } checksumTotal = extendedChecksum16(recBuff, 8); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("Error : read buffer has bad checksum16(MSB) (StreamConfig).\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Error : read buffer has bad checksum16(LSB) (StreamConfig).\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Error : read buffer has bad checksum8 (StreamConfig).\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x01) || recBuff[3] != (uint8)(0x11) || recBuff[7] != (uint8)(0x00) ) { printf("Error : read buffer has wrong command bytes (StreamConfig).\n"); return -1; } if( recBuff[6] != 0 ) { printf("Errorcode # %d from StreamConfig read.\n", (unsigned int)recBuff[6]); return -1; } return 0; }
//Sends a StreamConfig low-level command to configure the stream to read //NumChannels analog inputs. int StreamConfig_example(int socketFD) { int sendBuffSize; uint8 *sendBuff; uint8 recBuff[8]; int sendChars, recChars, i, ret; uint16 checksumTotal, scanInterval; sendBuffSize = 12 + 2*NumChannels; sendBuff = malloc(sizeof(uint8)*sendBuffSize); ret = 0; sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = NumChannels + 3; //number of data words : NumChannels + 3 sendBuff[3] = (uint8)(0x11); //extended command number sendBuff[6] = (uint8)NumChannels; //NumChannels sendBuff[7] = ainResolution; //resolution sendBuff[8] = 0; //SettlingTime = 0 sendBuff[9] = 0; //ScanConfig: scan pulse and external scan //trigger disabled, stream clock //frequency = 4 MHz scanInterval = 4000; sendBuff[10] = (uint8)(scanInterval & 0x00FF); //scan interval (low byte) sendBuff[11] = (uint8)(scanInterval / 256); //scan interval (high byte) for(i = 0; i < NumChannels; i++) { sendBuff[12 + i*2] = i; //channel # = i sendBuff[13 + i*2] = 0; //BipGain (Bip = unipolar, Gain = 1) } extendedChecksum(sendBuff, sendBuffSize); //Sending command to UE9 sendChars = send(socketFD, sendBuff, sendBuffSize, 0); if(sendChars < 20) { if(sendChars == -1) printf("Error : send failed (StreamConfig)\n"); else printf("Error : did not send all of the buffer (StreamConfig)\n"); ret = -1; goto cleanmem; } //Receiving response from UE9 recChars = recv(socketFD, recBuff, 8, 0); if(recChars < 8) { if(recChars == -1) printf("Error : receive failed (StreamConfig)\n"); else printf("Error : did not receive all of the buffer (StreamConfig)\n"); goto cleanmem; } checksumTotal = extendedChecksum16(recBuff, 8); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("Error : received buffer has bad checksum16(MSB) (StreamConfig)\n"); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("Error : received buffer has bad checksum16(LBS) (StreamConfig)\n"); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("Error : received buffer has bad checksum8 (StreamConfig)\n"); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x01) || recBuff[3] != (uint8)(0x11) || recBuff[7] != (uint8)(0x00)) { printf("Error : received buffer has wrong command bytes (StreamConfig)\n"); ret = -1; goto cleanmem; } if(recBuff[6] != 0) { printf("Errorcode # %d from StreamConfig received.\n", (unsigned int)recBuff[6]); ret = -1; goto cleanmem; } cleanmem: free(sendBuff); sendBuff = NULL; return ret; }
//Sends 1000 Feedback low-level commands to read digital IO and analog inputs. //On the first send, the following are set: DAC0 to 2.5 volts, DAC1 to 3.5 //volts, and digital IOs to inputs. int allIO(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint8 sendBuff[34], recBuff[64], settlingTime; uint8 numChannels; //Number of AIN channels, 0-16. uint16 checksumTotal, bytesVoltage, ainMask; int sendChars, recChars, i, j; int initialize; //boolean to init. DAC and digital IO settings int numIterations, valueDIPort; long time, ainResolution; double valueAIN[16]; numIterations = 1000; initialize = 1; time = 0; numChannels = 8; ainResolution = 12; for( i = 0; i < 16; i++ ) valueAIN[i] = 9999.0; settlingTime = 0; ainMask = pow(2.0, numChannels) - 1; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x0E); //Number of data words sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 255; //FIOMask : setting the mask of all FIOs sendBuff[7] = 0; //FIODir : setting all FIO directions to input sendBuff[8] = 0; //FIOState : all FIO directions are input, so // state writes do not apply sendBuff[9] = 255; //EIOMask : setting the mask of all EIOs sendBuff[10] = 0; //EIODir : setting all EIO directions to input sendBuff[11] = 0; //EIOState : all EIO directions are input, so // state writes do not apply sendBuff[12] = 15; //CIOMask : setting the mask of all CIOs sendBuff[13] = 0; //CIODirState : setting all CIO directions to input, // state writes do not apply sendBuff[14] = 7; //MIOMask : setting the mask of all MIOs sendBuff[15] = 0; //MIODirState : setting all MIO directions to input, // state writes do not apply //Getting binary DAC0 value of 2.5 volts if( getDacBinVoltCalibrated(caliInfo, 0, 2.500, &bytesVoltage) < 0 ) return -1; //Setting the voltage of DAC0 to 2.5 sendBuff[16] = (uint8)(bytesVoltage & 255); //low bits of DAC0 sendBuff[17] = (uint8)(bytesVoltage/256) + 192; //high bits of DAC0 //(bit 7 : Enable, // bit 6: Update) //Getting binary DAC1 value of 3.5 volts if( getDacBinVoltCalibrated(caliInfo, 1, 3.500, &bytesVoltage) < 0 ) return -1; //Setting the voltage of DAC1 to 3.5 volts sendBuff[18] = (uint8)(bytesVoltage & 255); //low bits of DAC1 sendBuff[19] = (uint8)(bytesVoltage/256) + 192; //high bits of DAC1 //(bit 7 : Enable, // bit 6: Update) //AINMask - reading the number of AINs specified by numChannels sendBuff[20] = ainMask & 255; //AINMask (low byte) sendBuff[21] = ainMask/256; //AINMask (high byte) sendBuff[22] = 14; //AIN14ChannelNumber : setting to channel 14 sendBuff[23] = 15; //AIN15ChannelNumber : setting to channel 15 sendBuff[24] = ainResolution; //Resolution : Resolution specified by // ainResolution sendBuff[25] = settlingTime; //SettlingTime //Setting all BipGains (Gain = 1, Bipolar = 1) for( i = 26; i < 34; i++ ) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff, 34); time = getTickCount(); for( i = 0; i < numIterations; i++ ) { //Sending command to UE9 sendChars = LJUSB_Write(hDevice, sendBuff, 34); if( sendChars < 34 ) { if( sendChars == 0 ) printf("Feedback error (Iteration %d) : write failed\n", i); else printf("Feedback error (Iteration %d) : did not write all of the buffer\n", i); return -1; } //Reading response from UE9 recChars = LJUSB_Read(hDevice, recBuff, 64); if( recChars < 64 ) { if( recChars == 0 ) printf("Feedback error (Iteration %d) : read failed\n", i); else printf("Feedback error (Iteration %d) : did not read all of the buffer\n", i); return -1; } checksumTotal = extendedChecksum16(recBuff, 64); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Feedback error (Iteration %d) : read buffer has bad checksum16(MSB)\n", i); return -1; } if( (uint8)(checksumTotal & 255) != recBuff[4] ) { printf("Feedback error (Iteration %d) : read buffer has bad checksum16(LSB)\n", i); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Feedback error (Iteration %d) : read buffer has bad checksum8\n", i); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x1D) || recBuff[3] != (uint8)(0x00) ) { printf("Feedback error (Iteration %d) : read buffer has wrong command bytes\n", i); return -1; } for( j = 0; j < numChannels && j < 16; j++ ) getAinVoltCalibrated(caliInfo, 0x00, (uint8)ainResolution, recBuff[12 + j*2] + recBuff[13 + j*2]*256, &valueAIN[j]); valueDIPort = recBuff[7] + recBuff[9]*256 + (recBuff[10] & 15)*65536 + (recBuff[11] & 7)*1048576; if( initialize == 1 ) { //Unsetting digital IO bit masks since we only want to read states now sendBuff[6] = 0; //FIOMask sendBuff[9] = 0; //EIOMask sendBuff[12] = 0; //CIOMask sendBuff[14] = 0; //MIOMask //Turning off Update bit of DACs sendBuff[17] = sendBuff[17] - 64; //high bits of DAC0 sendBuff[19] = sendBuff[19] - 64; //high bits of DAC1 extendedChecksum(sendBuff, 34); initialize = 0; } } time = getTickCount() - time; printf("Milleseconds per iteration = %.3f\n", (double)time / (double)numIterations); printf("\nDigital Input (FIO0-7, EIO0-7, CIO0-3, MIO0-2) = %d\n",valueDIPort); printf("\nAIN readings from last iteration:\n"); for( j = 0; j < numChannels; j++ ) printf("%.3f\n", valueAIN[j]); return 0; }
HANDLE openUSBConnection(int localID) { BYTE sendBuffer[26], recBuffer[38]; uint16 checksumTotal = 0; HANDLE hDevice = 0; uint32 numDevices = 0; uint32 dev; int i; numDevices = LJUSB_GetDevCount(U6_PRODUCT_ID); if(numDevices == 0) { printf("Open error: No U6 devices could be found\n"); return NULL; } for(dev = 1; dev <= numDevices; dev++) { hDevice = LJUSB_OpenDevice(dev, 0, U6_PRODUCT_ID); if(hDevice != NULL) { if(localID < 0) { return hDevice; } else { checksumTotal = 0; //setting up a U6Config sendBuffer[1] = (uint8)(0xF8); sendBuffer[2] = (uint8)(0x0A); sendBuffer[3] = (uint8)(0x08); for(i = 6; i < 26; i++) sendBuffer[i] = (uint8)(0x00); extendedChecksum(sendBuffer, 26); if(LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuffer, 26) != 26) goto locid_error; if(LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuffer, 38) != 38) goto locid_error; checksumTotal = extendedChecksum16(recBuffer, 38); if( (uint8)((checksumTotal / 256) & 0xff) != recBuffer[5]) goto locid_error; if( (uint8)(checksumTotal & 0xff) != recBuffer[4]) goto locid_error; if( extendedChecksum8(recBuffer) != recBuffer[0]) goto locid_error; if( recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x10) || recBuffer[3] != (uint8)(0x08) ) goto locid_error; if( recBuffer[6] != 0) goto locid_error; if( (int)recBuffer[21] == localID) return hDevice; else LJUSB_CloseDevice(hDevice); } //else localID >= 0 end } //if hDevice != NULL end } //for end printf("Open error: could not find a U6 with a local ID of %d\n", localID); return NULL; locid_error: printf("Open error: problem when checking local ID\n"); return NULL; }
long ehConfigTimerClock(HANDLE hDevice, uint8 inTimerClockConfig, uint8 inTimerClockDivisor, uint8 *outTimerClockConfig, uint8 *outTimerClockDivisor) { uint8 sendBuff[10], recBuff[10]; uint16 checksumTotal; int sendChars, recChars; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x02); //Number of data words sendBuff[3] = (uint8)(0x0A); //Extended command number sendBuff[6] = 0; //Reserved sendBuff[7] = 0; //Reserved sendBuff[8] = inTimerClockConfig; //TimerClockConfig sendBuff[9] = inTimerClockDivisor; //TimerClockDivisor extendedChecksum(sendBuff, 10); //Sending command to U6 if( (sendChars = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuff, 10)) < 10) { if(sendChars == 0) printf("ehConfigTimerClock error : write failed\n"); else printf("ehConfigTimerClock error : did not write all of the buffer\n"); return -1; } //Reading response from U6 if( (recChars = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuff, 10)) < 10) { if(recChars == 0) printf("ehConfigTimerClock error : read failed\n"); else printf("ehConfigTimerClock error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 10); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5]) { printf("ehConfigTimerClock error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehConfigTimerClock error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehConfigTimerClock error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x02) || recBuff[3] != (uint8)(0x0A) ) { printf("ehConfigTimerClock error : read buffer has wrong command bytes\n"); return -1; } if(outTimerClockConfig != NULL) *outTimerClockConfig = recBuff[8]; if(outTimerClockDivisor != NULL) *outTimerClockDivisor = recBuff[9]; if( recBuff[6] != 0) { printf("ehConfigTimerClock error : read buffer received errorcode %d\n", recBuff[6]); return recBuff[6]; } return 0; }
//Sends a Feedback low-level command to set DAC0, DAC1, read FIO0-FIO3 and //AI0-AI3. int feedback_example(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint8 sendBuff[34], recBuff[64], ainResolution, gainBip; uint16 checksumTotal, bytesVoltage; uint32 tempDir, tempState; int sendChars, recChars, i; double voltage; ainResolution = 12; //ainResolution = 18; //high-res mode for UE9 Pro only gainBip = 0; //(Gain = 1, Bipolar = 0) sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x0E); //Number of data words sendBuff[3] = (uint8)(0x00); //Extended command number //All these bytes are set to zero since we are not changing the FIO, EIO, CIO //and MIO directions and states for( i = 6; i <= 15; i++ ) sendBuff[i] = (uint8)(0x00); if( getDacBinVoltCalibrated(caliInfo, 0, 2.500, &bytesVoltage) < 0 ) return -1; //setting the voltage of DAC0 sendBuff[16] = (uint8)( bytesVoltage & (0x00FF) ); //low bits of DAC0 sendBuff[17] = (uint8)( bytesVoltage / 256 ) + 192; //high bits of DAC0 //(bit 7 : Enable, // bit 6: Update) if( getDacBinVoltCalibrated(caliInfo, 1, 3.500, &bytesVoltage) < 0 ) return -1; //setting the voltage of DAC1 sendBuff[18] = (uint8)( bytesVoltage & (0x00FF) ); //low bits of DAC1 sendBuff[19] = (uint8)( bytesVoltage / 256 ) + 192; //high bits of DAC1 //(bit 7 : Enable, // bit 6: Update) sendBuff[20] = (uint8)(0x0f); //AINMask - reading AIN0-AIN3, not AIN4-AIN7 sendBuff[21] = (uint8)(0x00); //AINMask - not reading AIN8-AIN15 sendBuff[22] = (uint8)(0x00); //AIN14ChannelNumber - not using sendBuff[23] = (uint8)(0x00); //AIN15ChannelNumber - not using sendBuff[24] = ainResolution; //Resolution = 12 //Setting BipGains for( i = 25; i < 34; i++ ) sendBuff[i] = gainBip; extendedChecksum(sendBuff, 34); //Sending command to UE9 sendChars = LJUSB_Write(hDevice, sendBuff, 34); if( sendChars < 34 ) { if( sendChars == 0 ) printf("Error : write failed\n"); else printf("Error : did not write all of the buffer\n"); return -1; } //Reading response from UE9 recChars = LJUSB_Read(hDevice, recBuff, 64); if( recChars < 64 ) { if( recChars == 0 ) printf("Error : read failed\n"); else printf("Error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 64); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("Error : read buffer has bad checksum16(LSB)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x1D) || recBuff[3] != (uint8)(0x00) ) { printf("Error : read buffer has wrong command bytes \n"); return -1; } printf("Set DAC0 to 2.500 volts and DAC1 to 3.500 volts.\n\n"); printf("Flexible digital I/O directions and states (FIO0 - FIO3):\n"); for( i = 0; i < 4; i++ ) { tempDir = ( (uint32)(recBuff[6] / pow(2, i)) & (0x01) ); tempState = ( (uint32)(recBuff[7] / pow(2, i)) & (0x01) ); printf(" FI%d: %u and %u\n", i, tempDir, tempState); } printf("\nAnalog Inputs (AI0 - AI3):\n"); for( i = 0; i < 4; i++ ) { bytesVoltage = recBuff[12 + 2*i] + recBuff[13 + 2*i]*256; //getting analog voltage if( getAinVoltCalibrated(caliInfo, gainBip, ainResolution, bytesVoltage, &voltage) < 0 ) return -1; printf(" AI%d: %.4f V\n", i, voltage); } printf("\n"); return 0; }
//Sends a StreamConfig low-level command to configure the stream to read only 4 //analog inputs (AI0 - AI3). int StreamConfig_example(HANDLE hDevice) { int sendBuffSize; sendBuffSize = 12 + 2*NumChannels; uint8 sendBuff[sendBuffSize], recBuff[8]; uint16 checksumTotal, scanInterval; int sendChars, recChars, i; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = NumChannels + 3; //Number of data words : NumChannels + 3 sendBuff[3] = (uint8)(0x11); //Extended command number sendBuff[6] = (uint8)NumChannels; //NumChannels sendBuff[7] = ainResolution; //Resolution sendBuff[8] = 0; //SettlingTime = 0 sendBuff[9] = 0; //ScanConfig: scan pulse and external scan //trigger disabled stream clock //frequency = 4 MHz scanInterval = 4000; sendBuff[10] = (uint8)(scanInterval&0x00FF); //Scan interval (low byte) sendBuff[11] = (uint8)(scanInterval/256); //Scan interval (high byte) for( i = 0; i < NumChannels; i++ ) { sendBuff[12 + i*2] = i; //channel # = i sendBuff[13 + i*2] = 0; //BipGain (Bip = unipolar, Gain = 1) } extendedChecksum(sendBuff, sendBuffSize); //Sending command to UE9 sendChars = LJUSB_Write(hDevice, sendBuff, sendBuffSize); if( sendChars < sendBuffSize ) { if( sendChars == 0 ) printf("Error : write failed (StreamConfig).\n"); else printf("Error : did not write all of the buffer (StreamConfig).\n"); return -1; } //Reading response from UE9 recChars = LJUSB_Read(hDevice, recBuff, 8); if( recChars < 8 ) { if( recChars == 0 ) printf("Error : read failed (StreamConfig).\n"); else printf("Error : did not read all of the buffer (StreamConfig).\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 8); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Error : read buffer has bad checksum16(MSB) (StreamConfig).\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Error : read buffer has bad checksum16(LSB) (StreamConfig).\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Error : read buffer has bad checksum8 (StreamConfig).\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x01) || recBuff[3] != (uint8)(0x11) ) { printf("Error : read buffer has wrong command bytes (StreamConfig).\n"); return -1; } if( recBuff[6] != 0 ) { printf("Errorcode # %d from StreamConfig read.\n", (unsigned int)recBuff[6]); return -1; } return 0; }
//Reads the StreamData low-level function response in a loop. All voltages from //the stream are stored in the voltages 2D array. int StreamData_example(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint16 voltageBytes, checksumTotal; int recChars, backLog, overflow; int i, j, k, m, packetCounter, currChannel, scanNumber; int totalPackets; //The total number of StreamData responses read int numDisplay; //Number of times to display streaming information int numReadsPerDisplay; //Number of packets to read before displaying streaming information int readSizeMultiplier; //Multiplier for the StreamData receive buffer size long startTime, endTime; packetCounter = 0; currChannel = 0; scanNumber = 0; totalPackets = 0; recChars = 0; numDisplay = 6; numReadsPerDisplay = 3; readSizeMultiplier = 10; /* Each StreamData response contains (16/NumChannels) * readSizeMultiplier * samples for each channel. * Total number of scans = (16 / NumChannels) * 4 * readSizeMultiplier * numReadsPerDisplay * numDisplay */ double voltages[(16/NumChannels)*4*readSizeMultiplier*numReadsPerDisplay*numDisplay][NumChannels]; uint8 recBuff[192*readSizeMultiplier]; printf("Reading Samples...\n"); startTime = getTickCount(); for( i = 0; i < numDisplay; i++ ) { for( j = 0; j < numReadsPerDisplay; j++ ) { /* For USB StreamData, use Endpoint 2 for reads and 192 byte packets * instead of the 46. The 192 byte response is 4 StreamData packet * responses of 48 bytes. * You can read the multiple StreamData responses of 192 bytes to help * improve streaming performance. In this example this multiple is * adjusted by the readSizeMultiplier variable. */ //Reading response from UE9 recChars = LJUSB_Stream(hDevice, recBuff, 192*readSizeMultiplier); if( recChars < 192*readSizeMultiplier ) { if( recChars == 0 ) printf("Error : read failed (StreamData).\n"); else printf("Error : did not read all of the buffer %d (StreamData).\n", recChars); return -1; } overflow = 0; //Checking for errors and getting data out of each StreamData response for( m = 0; m < 4*readSizeMultiplier; m++ ) { totalPackets++; checksumTotal = extendedChecksum16(recBuff + m*48, 46); if( (uint8)((checksumTotal >> 8) & 0xff) != recBuff[m*48 + 5] ) { printf("Error : read buffer has bad checksum16(MSB) (StreamData).\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[m*48 + 4] ) { printf("Error : read buffer has bad checksum16(LSB) (StreamData).\n"); return -1; } checksumTotal = extendedChecksum8(recBuff + m*48); if( checksumTotal != recBuff[m*48] ) { printf("Error : read buffer has bad checksum8 (StreamData).\n"); return -1; } if( recBuff[m*48 + 1] != (uint8)(0xF9) || recBuff[m*48 + 2] != (uint8)(0x14) || recBuff[m*48 + 3] != (uint8)(0xC0) ) { printf("Error : read buffer has wrong command bytes (StreamData).\n"); return -1; } if( recBuff[m*48 + 11] != 0 ) { printf("Errorcode # %d from StreamData read.\n", (unsigned int)recBuff[11]); return -1; } if( packetCounter != (int) recBuff[m*48 + 10] ) { printf("PacketCounter does not match with with current packet count (StreamData).\n"); return -1; } backLog = recBuff[m*48 + 45]&0x7F; //Checking MSB for Comm buffer overflow if( (recBuff[m*48 + 45] & 128) == 128 ) { printf("\nComm buffer overflow detected in packet %d\n", totalPackets); printf("Current Comm backlog: %d\n", recBuff[m*48 + 45]&0x7F); overflow = 1; } for( k = 12; k < 43; k += 2 ) { voltageBytes = (uint16)recBuff[m*48 + k] + (uint16)recBuff[m*48 + k+1]*256; getAinVoltCalibrated(caliInfo, (uint8)(0x00), ainResolution, voltageBytes, &(voltages[scanNumber][currChannel])); currChannel++; if( currChannel > 3 ) { currChannel = 0; scanNumber++; } } if( packetCounter >= 255 ) packetCounter = 0; else packetCounter++; //Handle Comm buffer overflow by stopping, flushing and restarting stream if( overflow == 1 ) { printf("\nRestarting stream...\n"); doFlush(hDevice); if( StreamConfig_example(hDevice) != 0 ) { printf("Error restarting StreamConfig.\n"); return -1; } if( StreamStart(hDevice) != 0 ) { printf("Error restarting StreamStart.\n"); return -1; } packetCounter = 0; break; } } } printf("\nNumber of scans: %d\n", scanNumber); printf("Total packets read: %d\n", totalPackets); printf("Current PacketCounter: %d\n", ((packetCounter == 0) ? 255 : packetCounter-1)); printf("Current Comm backlog: %d\n", backLog); for( k = 0; k < 4; k++ ) printf(" AI%d: %.4f V\n", k, voltages[scanNumber - 1][k]); } endTime = getTickCount(); printf("\nRate of samples: %.0lf samples per second\n", (scanNumber*NumChannels)/((endTime - startTime)/1000.0)); printf("Rate of scans: %.0lf scans per second\n\n", scanNumber/((endTime - startTime)/1000.0)); return 0; }
//Sends a ControlConfig low-level command to read the configuration settings //associated with the Control chip. int controlConfig_example(int socketFD, ue9CalibrationInfo *caliInfo) { uint8 sendBuff[18], recBuff[24]; int sendChars, recChars; int i; uint16 checksumTotal; double dac; sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (uint8)(0x06); //number of data words sendBuff[3] = (uint8)(0x08); //extended command number //WriteMask, PowerLevel, FIODir, etc. are all passed a value of //zero since we only want to read Control configuration settings, //not change them for(i = 6; i < 18; i++) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff,18); //Sending command to UE9 sendChars = send(socketFD, sendBuff, 18, 0); if(sendChars < 18) { if(sendChars == -1) printf("Error : send failed\n"); else printf("Error : did not send all of the buffer\n"); return -1; } //Receiving response from UE9 recChars = recv(socketFD, recBuff, 24, 0); if(recChars < 24) { if(recChars == -1) printf("Error : receive failed\n"); else printf("Error : did not receive all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 24); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("Error : received buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("Error : received buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("Error : received buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x09) || recBuff[3] != (uint8)(0x08) ) { printf("Error : received buffer has wrong command bytes \n"); return -1; } if( recBuff[6] != 0) { printf("Errorcode (byte 6): %d\n", (uint32)recBuff[6]); return -1; } printf("PowerLevel default (byte 7): %d\n", (uint32)recBuff[7]); printf("ResetSource (byte 8): %d\n", (uint32)recBuff[8]); printf("ControlFW Version (bytes 9 and 10): %.3f\n", (uint32)recBuff[10] + (double)recBuff[9]/100.0); printf("ControlBL Version (bytes 11 and 12): %.3f\n", (uint32)recBuff[12] + (double)recBuff[11]/100.0); printf("HiRes Flag (byte 13): %d\n", (uint32)(recBuff[13]) & (0x01)); printf("FIO default directions and states (bytes 14 and 15):\n"); for(i = 0; i < 8; i++) printf(" FIO%d: %d and %d\n", i, ((recBuff[14]/(uint32)pow(2, i)) & 0x01), ((recBuff[15]/(uint32)pow(2, i)) & 0x01) ); printf("EIO default directions and states (bytes 16 and 17):\n"); for(i = 0; i < 8; i++) printf(" EIO%d: %d and %d\n", i, ((recBuff[16]/(uint32)pow(2, i)) & 0x01), ((recBuff[17]/(uint32)pow(2, i)) & 0x01) ); printf("CIO default directions and states (byte 18):\n"); for(i = 0; i <= 3; i++) printf(" CIO%d: %d and %d\n", i, ((recBuff[18]/(uint32)pow(2, 4 + i)) & 0x01), ((recBuff[18]/(uint32)pow(2, i)) & 0x01) ); printf("MIO default directions and states (byte 19):\n"); for(i = 0; i <= 2; i++) printf(" MIO%d: %d and %d\n", i, ((recBuff[19]/(uint32)pow(2, 4 + i)) & 0x01), ((recBuff[19]/(uint32)pow(2, i)) & 0x01) ); printf("DAC0 default (bytes 20 and 21):\n Enabled: %d\n", ((recBuff[21]/128) & 0x01) ); //getting DAC0 binary value dac = (double)( (unsigned int)recBuff[20] + (((unsigned int)recBuff[21] & 15)*256) ); //getting DAC0 analog value (Volts = (Bits - Offset)/Slope ) dac = (dac - caliInfo->DACOffset[0])/caliInfo->DACSlope[0]; printf(" Voltage: %.3f V\n", dac); printf("DAC1 default (bytes 22 and 23):\n Enabled: %d\n", ((recBuff[23]/128) & 0x01) ); //getting DAC1 binary value dac = (double)( (uint32)recBuff[22] + ((recBuff[23] & 15)*256) ); //getting DAC1 analog value ( Volts = (Bits - Offset)/Slope ) dac = (dac - caliInfo->DACOffset[1])/caliInfo->DACSlope[1]; printf(" Voltage: %.3f V\n\n", dac); return 0; }
//Calls the Feedback low-level command numIterations times and calculates the //time per iteration. int allIO(HANDLE hDevice, u6CalibrationInfo *caliInfo) { uint8 *sendBuff, *recBuff; uint16 checksumTotal, bits16; uint32 bits32; int sendChars, recChars, i, j, sendSize, recSize; double valueAIN[14]; long time; int ret = 0; for( i = 0; i < 14; i++ ) valueAIN[i] = 9999; //Setting up a Feedback command that will set CIO0-3 as input, and setting //DAC0 voltage sendBuff = (uint8 *)malloc(18*sizeof(uint8)); //Creating an array of size 18 recBuff = (uint8 *)malloc(10*sizeof(uint8)); //Creating an array of size 10 sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = 6; //Number of data words (.5 word for echo, 5.5 //words for IOTypes and data) sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 0; //Echo sendBuff[7] = 29; //IOType is PortDirWrite sendBuff[8] = 0; //FIO Writemask sendBuff[9] = 0; //EIO Writemask sendBuff[10] = 15; //CIO Writemask sendBuff[11] = 0; //FIO Direction sendBuff[12] = 0; //EIO Direction sendBuff[13] = 0; //CIO Direction //Setting DAC0 with 2.5 volt output sendBuff[14] = 38; //IOType is DAC0(16-bit) //Value is 2.5 volts (in binary) getDacBinVoltCalibrated16Bit(caliInfo, 0, 2.5, &bits16); sendBuff[15] = (uint8)(bits16&255); sendBuff[16] = (uint8)(bits16/256); sendBuff[17] = 0; //extra padding byte extendedChecksum(sendBuff, 18); //Sending command to U6 if( (sendChars = LJUSB_Write(hDevice, sendBuff, 18)) < 18 ) { if(sendChars == 0) printf("Feedback (CIO input) error : write failed\n"); else printf("Feedback (CIO input) error : did not write all of the buffer\n"); ret = -1; goto cleanmem; } //Reading response from U6 if( (recChars = LJUSB_Read(hDevice, recBuff, 10)) < 10 ) { if( recChars == 0 ) { printf("Feedback (CIO input) error : read failed\n"); ret = -1; goto cleanmem; } else printf("Feedback (CIO input) error : did not read all of the buffer\n"); } checksumTotal = extendedChecksum16(recBuff, 10); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Feedback (CIO input) error : read buffer has bad checksum16(MSB)\n"); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Feedback (CIO input) error : read buffer has bad checksum16(LBS)\n"); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Feedback (CIO input) error : read buffer has bad checksum8\n"); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[3] != (uint8)(0x00) ) { printf("Feedback (CIO input) error : read buffer has wrong command bytes \n"); ret = -1; goto cleanmem; } if( recBuff[6] != 0 ) { printf("Feedback (CIO input) error : received errorcode %d for frame %d in Feedback response. \n", recBuff[6], recBuff[7]); ret = -1; goto cleanmem; } free(sendBuff); free(recBuff); //Setting up Feedback command that will run numIterations times if( ((sendSize = 7+numChannels*4) % 2) != 0 ) sendSize++; //Need an extra byte sendBuff = (uint8 *)malloc(sendSize*sizeof(uint8)); //Creating an array of size sendSize if( ((recSize = 9+numChannels*3) % 2) != 0 ) recSize++; //Need an extra byte recBuff = (uint8 *)malloc(recSize*sizeof(uint8)); //Creating an array of size recSize sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (sendSize - 6)/2; //Number of data words sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 0; //Echo sendBuff[sendSize - 1] = 0; //Setting last byte to zero in case it is the extra padding byte //Setting AIN read commands for( j = 0; j < numChannels; j++ ) { sendBuff[7 + j*4] = 2; //IOType is AIN24 //Positive Channel (bits 0 - 4), LongSettling (bit 6) and QuickSample (bit 7) sendBuff[8 + j*4] = j; //Positive Channel sendBuff[9 + j*4] = (uint8)(resolution&15) + (uint8)((gainIndex&15)*16); //ResolutionIndex (Bits 0-3), GainIndex (Bits 4-7) sendBuff[10 + j*4] = (uint8)(settlingFactor&7); //SettlingFactor (Bits 0-2) if( j%2 == 0 ) sendBuff[10 + j*4] += (uint8)((differential&1)*128); //Differential (Bits 7) } extendedChecksum(sendBuff, sendSize); time = getTickCount(); for( i = 0; i < numIterations; i++ ) { //Sending command to U6 if( (sendChars = LJUSB_Write(hDevice, sendBuff, sendSize)) < sendSize ) { if(sendChars == 0) printf("Feedback error (Iteration %d): write failed\n", i); else printf("Feedback error (Iteration %d): did not write all of the buffer\n", i); ret = -1; goto cleanmem; } //Reading response from U6 if( (recChars = LJUSB_Read(hDevice, recBuff, recSize)) < recSize ) { if( recChars == 0 ) { printf("Feedback error (Iteration %d): read failed\n", i); ret = -1; goto cleanmem; } } checksumTotal = extendedChecksum16(recBuff, recChars); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Feedback error (Iteration %d): read buffer has bad checksum16(MSB)\n", i); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Feedback error (Iteration %d): read buffer has bad checksum16(LBS)\n", i); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Feedback error (Iteration %d): read buffer has bad checksum8\n", i); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[3] != (uint8)(0x00) ) { printf("Feedback error (Iteration %d): read buffer has wrong command bytes \n", i); ret = -1; goto cleanmem; } if( recBuff[6] != 0 ) { printf("Feedback error (Iteration %d): received errorcode %d for frame %d in Feedback response. \n", i, recBuff[6], recBuff[7]); ret = -1; goto cleanmem; } if( recChars != recSize ) { printf("Feedback error (Iteration %d): received packet if %d size when expecting %d\n", i, recChars, recSize); ret = -1; goto cleanmem; } //Getting AIN voltages for(j = 0; j < numChannels; j++) { bits32 = recBuff[9+j*3] + recBuff[10+j*3]*256 + recBuff[11+j*3]*65536; getAinVoltCalibrated(caliInfo, resolution, gainIndex, 1, bits32, &valueAIN[j]); } } time = getTickCount() - time; printf("Milliseconds per iteration = %.3f\n", (double)time / (double)numIterations); printf("\nAIN readings from last iteration:\n"); for( j = 0; j < numChannels; j++ ) printf("%.3f\n", valueAIN[j]); cleanmem: free(sendBuff); free(recBuff); sendBuff = NULL; recBuff = NULL; return ret; }
//Sends a CommConfig low-level command to read the configuration settings //associated with the Comm chip. int commConfig_example(int socketFD) { uint8 sendBuff[38]; uint8 recBuff[38]; int sendChars, recChars; int i, j; uint16 checksumTotal; char str[3]; sendBuff[1] = (uint8)(0x78); //command bytes sendBuff[2] = (uint8)(0x10); //number of data words sendBuff[3] = (uint8)(0x01); //extended command number //WriteMask, LocalID, PowerLevel, etc. are all passed a value of //zero since we only want to read Comm configuration settings, //not change them for(i = 6; i < 38; i++) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff, 38); //Sending command to UE9 sendChars = send(socketFD, sendBuff, 38, 0); if(sendChars < 38) { if(sendChars == -1) printf("Error : send failed\n"); else printf("Error : did not send all of the buffer\n"); return -1; } //Receiving response from UE9 recChars = recv(socketFD, recBuff, 38, 0); if(recChars < 38) { if(recChars == -1) printf("Error : receive failed\n"); else printf("Error : did not receive all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 38); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("Error : received buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("Error : received buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("Error : received buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0x78) || recBuff[2] != (uint8)(0x10) || recBuff[3] != (uint8)(0x01) ) { printf("Error : received buffer has wrong command bytes \n"); return -1; } printf("LocalID (byte 8): %d\n", recBuff[8]); printf("PowerLevel (byte 9): %d\n", recBuff[9]); printf("ipAddress (bytes 10-13): %d.%d.%d.%d\n", recBuff[13], recBuff[12], recBuff[11], recBuff[10]); printf("Gateway (bytes 14 - 17): %d.%d.%d.%d\n", recBuff[17], recBuff[16], recBuff[15], recBuff[14]); printf("Subnet (bytes 18 - 21): %d.%d.%d.%d\n", recBuff[21], recBuff[20], recBuff[19], recBuff[18]); printf("PortA (bytes 22 - 23): %d\n", recBuff[22] + (recBuff[23] * 256 )); printf("PortA (bytes 24 - 25): %d\n", recBuff[24] + (recBuff[25] * 256 )); printf("DHCPEnabled (byte 26): %d\n", recBuff[26]); printf("ProductID (byte 27): %d\n", recBuff[27]); printf("MACAddress (bytes 28 - 33): "); for(i = 5; i >= 0 ; i--) { sprintf(str, "%x", (int)(recBuff[i+28])); for(j = 0; j < 3; j++) { if(str[j] == '\0') break; } if(j < 2) { str[1] = str[0]; str[2] = '\0'; str[0] = '0'; } printf("%s", str); if(i > 2) printf("."); if(i == 2) printf(" "); } printf("\nHWVersion (bytes 34-35): %.3f\n", (unsigned int)recBuff[35] + (double)recBuff[34]/100.0); printf("CommFWVersion (bytes 36-37): %.3f\n\n", (unsigned int)recBuff[37] + (double)recBuff[36]/100.0); return 0; }
//Reads the StreamData low-level function response in a loop. //All voltages from the stream are stored in the voltages 2D array. int StreamData_example(HANDLE hDevice, u6CalibrationInfo *caliInfo) { int recBuffSize; recBuffSize = 14 + SamplesPerPacket*2; int recChars, backLog; int i, j, k, m, packetCounter, currChannel, scanNumber; int totalPackets; //The total number of StreamData responses read uint16 voltageBytes, checksumTotal; long startTime, endTime; int autoRecoveryOn; int numDisplay; //Number of times to display streaming information int numReadsPerDisplay; //Number of packets to read before displaying streaming information int readSizeMultiplier; //Multiplier for the StreamData receive buffer size int responseSize; //The number of bytes in a StreamData response (differs with SamplesPerPacket) numDisplay = 6; numReadsPerDisplay = 24; readSizeMultiplier = 5; responseSize = 14 + SamplesPerPacket*2; /* Each StreamData response contains (SamplesPerPacket / NumChannels) * readSizeMultiplier * samples for each channel. * Total number of scans = (SamplesPerPacket / NumChannels) * readSizeMultiplier * numReadsPerDisplay * numDisplay */ double voltages[(SamplesPerPacket/NumChannels)*readSizeMultiplier*numReadsPerDisplay*numDisplay][NumChannels]; uint8 recBuff[responseSize*readSizeMultiplier]; packetCounter = 0; currChannel = 0; scanNumber = 0; totalPackets = 0; recChars = 0; autoRecoveryOn = 0; printf("Reading Samples...\n"); startTime = getTickCount(); for( i = 0; i < numDisplay; i++ ) { for( j = 0; j < numReadsPerDisplay; j++ ) { /* For USB StreamData, use Endpoint 3 for reads. You can read the multiple * StreamData responses of 64 bytes only if SamplesPerPacket is 25 to help * improve streaming performance. In this example this multiple is adjusted * by the readSizeMultiplier variable. */ //Reading stream response from U6 recChars = LJUSB_Stream(hDevice, recBuff, responseSize*readSizeMultiplier); if( recChars < responseSize*readSizeMultiplier ) { if(recChars == 0) printf("Error : read failed (StreamData).\n"); else printf("Error : did not read all of the buffer, expected %d bytes but received %d(StreamData).\n", responseSize*readSizeMultiplier, recChars); return -1; } //Checking for errors and getting data out of each StreamData response for( m = 0; m < readSizeMultiplier; m++ ) { totalPackets++; checksumTotal = extendedChecksum16(recBuff + m*recBuffSize, recBuffSize); if( (uint8)((checksumTotal >> 8) & 0xff) != recBuff[m*recBuffSize + 5] ) { printf("Error : read buffer has bad checksum16(MSB) (StreamData).\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[m*recBuffSize + 4] ) { printf("Error : read buffer has bad checksum16(LSB) (StreamData).\n"); return -1; } checksumTotal = extendedChecksum8(recBuff + m*recBuffSize); if( checksumTotal != recBuff[m*recBuffSize] ) { printf("Error : read buffer has bad checksum8 (StreamData).\n"); return -1; } if( recBuff[m*recBuffSize + 1] != (uint8)(0xF9) || recBuff[m*recBuffSize + 2] != 4 + SamplesPerPacket || recBuff[m*recBuffSize + 3] != (uint8)(0xC0) ) { printf("Error : read buffer has wrong command bytes (StreamData).\n"); return -1; } if( recBuff[m*recBuffSize + 11] == 59 ) { if( !autoRecoveryOn ) { printf("\nU6 data buffer overflow detected in packet %d.\nNow using auto-recovery and reading buffered samples.\n", totalPackets); autoRecoveryOn = 1; } } else if( recBuff[m*recBuffSize + 11] == 60 ) { printf("Auto-recovery report in packet %d: %d scans were dropped.\nAuto-recovery is now off.\n", totalPackets, recBuff[m*recBuffSize + 6] + recBuff[m*recBuffSize + 7]*256); autoRecoveryOn = 0; } else if( recBuff[m*recBuffSize + 11] != 0 ) { printf("Errorcode # %d from StreamData read.\n", (unsigned int)recBuff[11]); return -1; } if( packetCounter != (int)recBuff[m*recBuffSize + 10] ) { printf("PacketCounter (%d) does not match with with current packet count (%d)(StreamData).\n", recBuff[m*recBuffSize + 10], packetCounter); return -1; } backLog = (int)recBuff[m*48 + 12 + SamplesPerPacket*2]; for( k = 12; k < (12 + SamplesPerPacket*2); k += 2 ) { voltageBytes = (uint16)recBuff[m*recBuffSize + k] + (uint16)recBuff[m*recBuffSize + k+1]*256; getAinVoltCalibrated(caliInfo, 1, 0, 0, voltageBytes, &(voltages[scanNumber][currChannel])); currChannel++; if( currChannel >= NumChannels ) { currChannel = 0; scanNumber++; } } if(packetCounter >= 255) packetCounter = 0; else packetCounter++; } } printf("\nNumber of scans: %d\n", scanNumber); printf("Total packets read: %d\n", totalPackets); printf("Current PacketCounter: %d\n", ((packetCounter == 0) ? 255 : packetCounter-1)); printf("Current BackLog: %d\n", backLog); for( k = 0; k < NumChannels; k++ ) printf(" AI%d: %.4f V\n", k, voltages[scanNumber - 1][k]); } endTime = getTickCount(); printf("\nRate of samples: %.0lf samples per second\n", (scanNumber*NumChannels)/((endTime - startTime)/1000.0)); printf("Rate of scans: %.0lf scans per second\n\n", scanNumber/((endTime - startTime)/1000.0)); return 0; }
//Sends a ConfigIO low-level command to turn off timers/counters int ConfigIO_example(HANDLE hDevice) { uint8 sendBuff[16], recBuff[16]; uint16 checksumTotal; int sendChars, recChars, i; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x03); //Number of data words sendBuff[3] = (uint8)(0x0B); //Extended command number sendBuff[6] = 1; //Writemask : Setting writemask for TimerCounterConfig (bit 0) sendBuff[7] = 0; //NumberTimersEnabled : Setting to zero to disable all timers. sendBuff[8] = 0; //CounterEnable: Setting bit 0 and bit 1 to zero to disable both counters sendBuff[9] = 0; //TimerCounterPinOffset for( i = 10; i < 16; i++ ) sendBuff[i] = 0; //Reserved extendedChecksum(sendBuff, 16); //Sending command to U6 if( (sendChars = LJUSB_Write(hDevice, sendBuff, 16)) < 16 ) { if( sendChars == 0 ) printf("ConfigIO error : write failed\n"); else printf("ConfigIO error : did not write all of the buffer\n"); return -1; } //Reading response from U6 if( (recChars = LJUSB_Read(hDevice, recBuff, 16)) < 16 ) { if( recChars == 0 ) printf("ConfigIO error : read failed\n"); else printf("ConfigIO error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 15); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5] ) { printf("ConfigIO error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("ConfigIO error : read buffer has bad checksum16(LSB)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("ConfigIO error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x05) || recBuff[3] != (uint8)(0x0B) ) { printf("ConfigIO error : read buffer has wrong command bytes\n"); return -1; } if( recBuff[6] != 0 ) { printf("ConfigIO error : read buffer received errorcode %d\n", recBuff[6]); return -1; } if( recBuff[8] != 0 ) { printf("ConfigIO error : NumberTimersEnabled was not set to 0\n"); return -1; } if( recBuff[9] != 0 ) { printf("ConfigIO error : CounterEnable was not set to 0\n"); return -1; } return 0; }
HANDLE openUSBConnection(int localID) { BYTE buffer[38]; uint16 checksumTotal = 0; uint32 numDevices = 0; uint32 dev; int i, serial; HANDLE hDevice = 0; numDevices = LJUSB_GetDevCount(UE9_PRODUCT_ID); if( numDevices == 0 ) { printf("Open error: No UE9 devices could be found\n"); return NULL; } for( dev = 1; dev <= numDevices; dev++ ) { hDevice = LJUSB_OpenDevice(dev, 0, UE9_PRODUCT_ID); if( hDevice != NULL ) { if( localID < 0 ) { return hDevice; } else { checksumTotal = 0; //Setting up a CommConfig command buffer[1] = (BYTE)(0x78); buffer[2] = (BYTE)(0x10); buffer[3] = (BYTE)(0x01); for( i = 6; i < 38; i++ ) buffer[i] = (BYTE)(0x00); extendedChecksum(buffer,38); if( LJUSB_Write(hDevice, buffer, 38) != 38 ) goto locid_error; for( i = 0; i < 38; i++ ) buffer[i] = 0; if( LJUSB_Read(hDevice, buffer, 38) != 38 ) goto locid_error; checksumTotal = extendedChecksum16(buffer, 38); if( (BYTE)((checksumTotal / 256) & 0xFF) != buffer[5] ) goto locid_error; if( (BYTE)(checksumTotal & 0xFF) != buffer[4] ) goto locid_error; if( extendedChecksum8(buffer) != buffer[0] ) goto locid_error; if( buffer[1] != (BYTE)(0x78) || buffer[2] != (BYTE)(0x10) || buffer[3] != (BYTE)(0x01) ) goto locid_error; //Check local ID if( (int)buffer[8] == localID ) return hDevice; //Check serial number serial = buffer[28] + buffer[29]*256 + buffer[30]*65536 + 0x10000000; if( serial == localID ) return hDevice; //No matches. Not our device. LJUSB_CloseDevice(hDevice); } //else localID >= 0 end } //if hDevice != NULL end } //for end printf("Open error: could not find a UE9 with a local ID or serial number of %d\n", localID); return NULL; locid_error: printf("Open error: problem when checking local ID and serial number\n"); return NULL; }
long ehFeedback(HANDLE hDevice, uint8 *inIOTypesDataBuff, long inIOTypesDataSize, uint8 *outErrorcode, uint8 *outErrorFrame, uint8 *outDataBuff, long outDataSize) { uint8 *sendBuff, *recBuff; uint16 checksumTotal; int sendChars, recChars, i, sendDWSize, recDWSize, commandBytes, ret; ret = 0; commandBytes = 6; if(((sendDWSize = inIOTypesDataSize + 1)%2) != 0) sendDWSize++; if(((recDWSize = outDataSize + 3)%2) != 0) recDWSize++; sendBuff = malloc(sizeof(uint8)*(commandBytes + sendDWSize)); recBuff = malloc(sizeof(uint8)*(commandBytes + recDWSize)); if(sendBuff == NULL || recBuff == NULL) { ret = -1; goto cleanmem; } sendBuff[sendDWSize + commandBytes - 1] = 0; /* Setting up Feedback command */ sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = sendDWSize/2; //Number of data words (.5 word for echo, 1.5 //words for IOTypes) sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 0; //Echo for(i = 0; i < inIOTypesDataSize; i++) sendBuff[i+commandBytes+1] = inIOTypesDataBuff[i]; extendedChecksum(sendBuff, (sendDWSize+commandBytes)); //Sending command to U6 if( (sendChars = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuff, (sendDWSize+commandBytes))) < sendDWSize+commandBytes) { if(sendChars == 0) printf("ehFeedback error : write failed\n"); else printf("ehFeedback error : did not write all of the buffer\n"); ret = -1; goto cleanmem; } //Reading response from U6 if( (recChars = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuff, (commandBytes+recDWSize))) < commandBytes+recDWSize) { if(recChars == -1) { printf("ehFeedback error : read failed\n"); ret = -1; goto cleanmem; } else if(recChars < 8) { printf("ehFeedback error : response buffer is too small\n"); for(i = 0; i < recChars; i++) printf("%d ", recBuff[i]); ret = -1; goto cleanmem; } else printf("ehFeedback error : did not read all of the expected buffer (received %d, expected %d )\n", recChars, commandBytes+recDWSize); } checksumTotal = extendedChecksum16(recBuff, recChars); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5]) { printf("ehFeedback error : read buffer has bad checksum16(MSB)\n"); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehFeedback error : read buffer has bad checksum16(LBS)\n"); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehFeedback error : read buffer has bad checksum8\n"); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[3] != (uint8)(0x00) ) { printf("ehFeedback error : read buffer has wrong command bytes \n"); ret = -1; goto cleanmem; } *outErrorcode = recBuff[6]; *outErrorFrame = recBuff[7]; for(i = 0; i+commandBytes+3 < recChars && i < outDataSize; i++) outDataBuff[i] = recBuff[i+commandBytes+3]; cleanmem: free(sendBuff); free(recBuff); sendBuff = NULL; recBuff = NULL; return ret; }
long ehConfigIO(HANDLE hDevice, uint8 inWriteMask, uint8 inTimerCounterConfig, uint8 inDAC1Enable, uint8 inFIOAnalog, uint8 inEIOAnalog, uint8 *outTimerCounterConfig, uint8 *outDAC1Enable, uint8 *outFIOAnalog, uint8 *outEIOAnalog) { uint8 sendBuff[12], recBuff[12]; uint16 checksumTotal; int sendChars, recChars; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x03); //Number of data words sendBuff[3] = (uint8)(0x0B); //Extended command number sendBuff[6] = inWriteMask; //Writemask sendBuff[7] = 0; //Reserved sendBuff[8] = inTimerCounterConfig; //TimerCounterConfig sendBuff[9] = inDAC1Enable; //DAC1 enable : not enabling sendBuff[10] = inFIOAnalog; //FIOAnalog sendBuff[11] = inEIOAnalog; //EIOAnalog extendedChecksum(sendBuff, 12); //Sending command to U3 if( (sendChars = LJUSB_BulkWrite(hDevice, U3_PIPE_EP1_OUT, sendBuff, 12)) < 12) { if(sendChars == 0) printf("ehConfigIO error : write failed\n"); else printf("ehConfigIO error : did not write all of the buffer\n"); return -1; } //Reading response from U3 if( (recChars = LJUSB_BulkRead(hDevice, U3_PIPE_EP2_IN, recBuff, 12)) < 12) { if(recChars == 0) printf("ehConfigIO error : read failed\n"); else printf("ehConfigIO error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 12); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5]) { printf("ehConfigIO error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehConfigIO error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehConfigIO error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x03) || recBuff[3] != (uint8)(0x0B) ) { printf("ehConfigIO error : read buffer has wrong command bytes\n"); return -1; } if( recBuff[6] != 0) { printf("ehConfigIO error : read buffer received errorcode %d\n", recBuff[6]); return (int)recBuff[6]; } if(outTimerCounterConfig != NULL) *outTimerCounterConfig = recBuff[8]; if(outDAC1Enable != NULL) *outDAC1Enable = recBuff[9]; if(outFIOAnalog != NULL) *outFIOAnalog = recBuff[10]; if(outEIOAnalog != NULL) *outEIOAnalog = recBuff[11]; return 0; }
long I2C(HANDLE hDevice, uint8 I2COptions, uint8 SpeedAdjust, uint8 SDAPinNum, uint8 SCLPinNum, uint8 Address, uint8 NumI2CBytesToSend, uint8 NumI2CBytesToReceive, uint8 *I2CBytesCommand, uint8 *Errorcode, uint8 *AckArray, uint8 *I2CBytesResponse) { uint8 *sendBuff, *recBuff; int sendChars, recChars, sendSize, recSize, i, ret; uint16 checksumTotal = 0; uint32 ackArrayTotal, expectedAckArray; *Errorcode = 0; ret = 0; sendSize = 6 + 8 + ((NumI2CBytesToSend%2 != 0)?(NumI2CBytesToSend + 1):(NumI2CBytesToSend)); recSize = 6 + 6 + ((NumI2CBytesToReceive%2 != 0)?(NumI2CBytesToReceive + 1):(NumI2CBytesToReceive)); sendBuff = malloc(sizeof(uint8)*sendSize); recBuff = malloc(sizeof(uint8)*recSize); sendBuff[sendSize - 1] = 0; //I2C command sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (sendSize - 6)/2; //number of data words = 4 + NumI2CBytesToSend sendBuff[3] = (uint8)(0x3B); //extended command number sendBuff[6] = I2COptions; //I2COptions sendBuff[7] = SpeedAdjust; //SpeedAdjust sendBuff[8] = SDAPinNum; //SDAPinNum sendBuff[9] = SCLPinNum; //SCLPinNum sendBuff[10] = Address; //Address sendBuff[11] = 0; //Reserved sendBuff[12] = NumI2CBytesToSend; //NumI2CByteToSend sendBuff[13] = NumI2CBytesToReceive; //NumI2CBytesToReceive for(i = 0; i < NumI2CBytesToSend; i++) sendBuff[14 + i] = I2CBytesCommand[i]; //I2CByte extendedChecksum(sendBuff, sendSize); //Sending command to U6 sendChars = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuff, sendSize); if(sendChars < sendSize) { if(sendChars == 0) printf("I2C Error : write failed\n"); else printf("I2C Error : did not write all of the buffer\n"); ret = -1; goto cleanmem; } //Reading response from U6 recChars = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuff, recSize); if(recChars < recSize) { if(recChars == 0) printf("I2C Error : read failed\n"); else { printf("I2C Error : did not read all of the buffer\n"); if(recChars >= 12) *Errorcode = recBuff[6]; } ret = -1; goto cleanmem; } *Errorcode = recBuff[6]; AckArray[0] = recBuff[8]; AckArray[1] = recBuff[9]; AckArray[2] = recBuff[10]; AckArray[3] = recBuff[11]; for(i = 0; i < NumI2CBytesToReceive; i++) I2CBytesResponse[i] = recBuff[12 + i]; if((uint8)(extendedChecksum8(recBuff)) != recBuff[0]) { printf("I2C Error : read buffer has bad checksum (%d)\n", recBuff[0]); ret = -1; } if(recBuff[1] != (uint8)(0xF8)) { printf("I2C Error : read buffer has incorrect command byte (%d)\n", recBuff[1]); ret = -1; } if(recBuff[2] != (uint8)((recSize - 6)/2)) { printf("I2C Error : read buffer has incorrect number of data words (%d)\n", recBuff[2]); ret = -1; } if(recBuff[3] != (uint8)(0x3B)) { printf("I2C Error : read buffer has incorrect extended command number (%d)\n", recBuff[3]); ret = -1; } checksumTotal = extendedChecksum16(recBuff, recSize); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] || (uint8)(checksumTotal & 255) != recBuff[4]) { printf("I2C error : read buffer has bad checksum16 (%d)\n", checksumTotal); ret = -1; } //ackArray should ack the Address byte in the first ack bit ackArrayTotal = AckArray[0] + AckArray[1]*256 + AckArray[2]*65536 + AckArray[3]*16777216; expectedAckArray = pow(2.0, NumI2CBytesToSend+1)-1; if(ackArrayTotal != expectedAckArray) printf("I2C error : expected an ack of %d, but received %d\n", expectedAckArray, ackArrayTotal); cleanmem: free(sendBuff); free(recBuff); sendBuff = NULL; recBuff = NULL; return ret; }
long ehDIO_Feedback(HANDLE hDevice, uint8 channel, uint8 direction, uint8 *state) { uint8 sendBuff[34], recBuff[64]; int sendChars, recChars; int i; uint8 tempDir, tempState, tempByte; uint16 checksumTotal; sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (uint8)(0x0E); //number of data words sendBuff[3] = (uint8)(0x00); //extended command number for(i = 6; i < 34; i++) sendBuff[i] = 0; tempDir = ((direction < 1) ? 0 : 1); tempState = ((*state < 1) ? 0 : 1); if(channel <= 7) { tempByte = pow(2, channel); sendBuff[6] = tempByte; if(tempDir) sendBuff[7] = tempByte; if(tempState) sendBuff[8] = tempByte; } else if(channel <= 15) { tempByte = pow(2, (channel - 8)); sendBuff[9] = tempByte; if(tempDir) sendBuff[10] = tempByte; if(tempState) sendBuff[11] = tempByte; } else if(channel <= 19) { tempByte = pow(2, (channel - 16)); sendBuff[12] = tempByte; if(tempDir) sendBuff[13] = tempByte*16; if(tempState) sendBuff[13] += tempByte; } else if(channel <= 22) { tempByte = pow(2, (channel - 20)); sendBuff[14] = tempByte; if(tempDir) sendBuff[15] = tempByte*16; if(tempState) sendBuff[15] += tempByte; } else { printf("DIO Feedback error: Invalid Channel\n"); return -1; } extendedChecksum(sendBuff, 34); //Sending command to UE9 sendChars = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuff, 34); if(sendChars < 34) { if(sendChars == 0) printf("DIO Feedback error : write failed\n"); else printf("DIO Feedback error : did not write all of the buffer\n"); return -1; } //Reading response from UE9 recChars = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuff, 64); if(recChars < 64) { if(recChars == 0) printf("DIO Feedback error : read failed\n"); else printf("DIO Feedback error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 64); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("DIO Feedback error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("DIO Feedback error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("DIO Feedback error : read buffer has bad checksum8\n"); return -1; } if(recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x1D) || recBuff[3] != (uint8)(0x00)) { printf("DIO Feedback error : read buffer has wrong command bytes \n"); return -1; } if(channel <= 7) *state = ((recBuff[7]&tempByte) ? 1 : 0); else if(channel <= 15) *state = ((recBuff[9]&tempByte) ? 1 : 0); else if(channel <= 19) *state = ((recBuff[10]&tempByte) ? 1 : 0); else if(channel <= 22) *state = ((recBuff[11]&tempByte) ? 1 : 0); return 0; }
long ehConfigIO(HANDLE hDevice, uint8 inWriteMask, uint8 inNumberTimersEnabled, uint8 inCounterEnable, uint8 inPinOffset, uint8 *outNumberTimersEnabled, uint8 *outCounterEnable, uint8 *outPinOffset) { uint8 sendBuff[16], recBuff[16]; uint16 checksumTotal; int sendChars, recChars, i; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x05); //Number of data words sendBuff[3] = (uint8)(0x0B); //Extended command number sendBuff[6] = inWriteMask; //Writemask sendBuff[7] = inNumberTimersEnabled; sendBuff[8] = inCounterEnable; sendBuff[9] = inPinOffset; for(i = 10; i < 16; i++) sendBuff[i] = 0; extendedChecksum(sendBuff, 16); //Sending command to U6 if( (sendChars = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuff, 16)) < 16) { if(sendChars == 0) printf("ehConfigIO error : write failed\n"); else printf("ehConfigIO error : did not write all of the buffer\n"); return -1; } //Reading response from U6 if( (recChars = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuff, 16)) < 16) { if(recChars == 0) printf("ehConfigIO error : read failed\n"); else printf("ehConfigIO error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 16); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5]) { printf("ehConfigIO error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehConfigIO error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehConfigIO error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x05) || recBuff[3] != (uint8)(0x0B) ) { printf("ehConfigIO error : read buffer has wrong command bytes\n"); return -1; } if( recBuff[6] != 0) { printf("ehConfigIO error : read buffer received errorcode %d\n", recBuff[6]); return (int)recBuff[6]; } if(outNumberTimersEnabled != NULL) *outNumberTimersEnabled = recBuff[7]; if(outCounterEnable != NULL) *outCounterEnable = recBuff[8]; if(outPinOffset != NULL) *outPinOffset = recBuff[9]; return 0; }
long ehTimerCounter(HANDLE hDevice, uint8 inTimerClockDivisor, uint8 inEnableMask, uint8 inTimerClockBase, uint8 inUpdateReset, uint8 *inTimerMode, uint16 *inTimerValue, uint8 *inCounterMode, uint32 *outTimer, uint32 *outCounter) { uint8 sendBuff[30], recBuff[40]; int sendChars, recChars, i, j; uint16 checksumTotal; sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (uint8)(0x0C); //number of data words sendBuff[3] = (uint8)(0x18); //extended command number sendBuff[6] = inTimerClockDivisor; //TimerClockDivisor sendBuff[7] = inEnableMask; //EnableMask sendBuff[8] = inTimerClockBase; //TimerClockBase sendBuff[9] = inUpdateReset; //UpdateReset for(i = 0; i < 6; i++) { sendBuff[10 + i*3] = inTimerMode[i]; //TimerMode sendBuff[11 + i*3] = (uint8)(inTimerValue[i]&0x00FF); //TimerValue (low byte) sendBuff[12 + i*3] = (uint8)((inTimerValue[i]&0xFF00)/256); //TimerValue (high byte) } for(i = 0; i < 2; i++) sendBuff[28 + i] = inCounterMode[i]; //CounterMode extendedChecksum(sendBuff, 30); //Sending command to UE9 sendChars = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuff, 30); if(sendChars < 30) { if(sendChars == 0) printf("ehTimerCounter error : write failed\n"); else printf("ehTimerCounter error : did not write all of the buffer\n"); return -1; } //Reading response from UE9 recChars = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuff, 40); if(recChars < 40) { if(recChars == 0) printf("ehTimerCounter error : read failed\n"); else printf("ehTimerCounter error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 40); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("ehTimerCounter error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehTimerCounter error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehTimerCounter error : read buffer has bad checksum8\n"); return -1; } if(recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x11) || recBuff[3] != (uint8)(0x18)) { printf("ehTimerCounter error : read buffer has wrong command bytes for TimerCounter\n"); return -1; } if(outTimer != NULL) { for(i = 0; i < 6; i++) { outTimer[i] = 0; for(j = 0; j < 4; j++) outTimer[i] += recBuff[8 + j + i*4]*pow(2, 8*j); } } if(outCounter != NULL) { for(i = 0; i < 2; i++) { outCounter[i] = 0; for(j = 0; j < 4; j++) outCounter[i] += recBuff[32 + j + i*4]*pow(2, 8*j); } } return recBuff[6]; }
HANDLE openUSBConnection(int localID) { BYTE buffer[38]; uint16 checksumTotal = 0; HANDLE hDevice = 0; uint32 numDevices = 0; uint32 dev; int i; numDevices = LJUSB_GetDevCount(UE9_PRODUCT_ID); if(numDevices == 0) { printf("Open error: No UE9 devices could be found\n"); return NULL; } for(dev = 1; dev <= numDevices; dev++) { hDevice = LJUSB_OpenDevice(dev, 0, UE9_PRODUCT_ID); if(hDevice != NULL) { if(localID < 0) { return hDevice; } else { checksumTotal = 0; //setting up a CommConfig command buffer[1] = (BYTE)(0x78); buffer[2] = (BYTE)(0x10); buffer[3] = (BYTE)(0x01); for(i = 6; i < 38; i++) buffer[i] = (BYTE)(0x00); extendedChecksum(buffer,38); if(LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, buffer, 38) != 38) goto locid_error; for(i = 0; i < 38; i++) buffer[i] = 0; if(LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, buffer, 38) != 38) goto locid_error; checksumTotal = extendedChecksum16(buffer, 38); if( (BYTE)((checksumTotal / 256) & 0xff) != buffer[5]) goto locid_error; if( (BYTE)(checksumTotal & 0xff) != buffer[4]) goto locid_error; if( extendedChecksum8(buffer) != buffer[0]) goto locid_error; if( buffer[1] != (BYTE)(0x78) || buffer[2] != (BYTE)(0x10) || buffer[3] != (BYTE)(0x01) ) goto locid_error; if( (int)buffer[8] == localID) return hDevice; else LJUSB_CloseDevice(hDevice); } //else localID >= 0 end } //if hDevice != NULL end } //for end printf("Open error: could not find a UE9 with a local ID of %d\n", localID); return NULL; locid_error: printf("Open error: problem when checking local ID\n"); return NULL; }
//Reads the StreamData low-level function response in a loop. All voltages from //the stream are stored in the voltages 2D array. int StreamData_example(int socketFDA, int socketFDB, ue9CalibrationInfo *caliInfo) { uint8 *recBuff; double **voltages; int recChars, backLog, overflow, totalScans, ret; int i, k, m, packetCounter, currChannel, scanNumber; int totalPackets; //The total number of StreamData responses read uint16 voltageBytes, checksumTotal; int numDisplay; //Number of times to display streaming information int readSizeMultiplier; //Multiplier for the StreamData receive buffer size long startTime, endTime; packetCounter = 0; currChannel = 0; scanNumber = 0; totalPackets = 0; recChars = 0; numDisplay = 6; readSizeMultiplier = 120; ret = 0; /* Each StreamData response contains (16/NumChannels) * readSizeMultiplier * samples for each channel. * Total number of scans = (16 / NumChannels) * readSizeMultiplier * numDisplay */ totalScans = (16/NumChannels)*readSizeMultiplier*numDisplay; voltages = malloc(sizeof(double)*totalScans); for(i = 0; i < totalScans; i++) voltages[i] = malloc(sizeof(double)*NumChannels); recBuff = malloc(sizeof(uint8)*46*readSizeMultiplier); printf("Reading Samples...\n"); startTime = getTickCount(); for (i = 0; i < numDisplay; i++) { /* You can read the multiple StreamData responses of 46 bytes to help * improve throughput. In this example this multiple is adjusted by the * readSizeMultiplier variable. We may not read 46 * readSizeMultiplier * bytes per each recv call, but we will continue reading until we read * 46 * readSizeMultiplier bytes total. */ recChars = 0; for(k = 0; k < 46*readSizeMultiplier; k += recChars) { //Reading response from UE9 recChars = recv(socketFDB, recBuff + k, 46*readSizeMultiplier - k, 0); if(recChars == 0) { printf("Error : read failed (StreamData).\n"); ret = -1; goto cleanmem; } } overflow = 0; //Checking for errors and getting data out of each StreamData response for (m = 0; m < readSizeMultiplier; m++) { totalPackets++; checksumTotal = extendedChecksum16(recBuff + m*46, 46); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[m*46 + 5]) { printf("Error : read buffer has bad checksum16(MSB) (StreamData).\n"); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[m*46 + 4]) { printf("Error : read buffer has bad checksum16(LBS) (StreamData).\n"); ret = -1; goto cleanmem; } checksumTotal = extendedChecksum8(recBuff + m*46); if( checksumTotal != recBuff[m*46]) { printf("Error : read buffer has bad checksum8 (StreamData).\n"); ret = -1; goto cleanmem; } if( recBuff[m*46 + 1] != (uint8)(0xF9) || recBuff[m*46 + 2] != (uint8)(0x14) || recBuff[m*46 + 3] != (uint8)(0xC0) ) { printf("Error : read buffer has wrong command bytes (StreamData).\n"); ret = -1; goto cleanmem; } if(recBuff[m*46 + 11] != 0) { printf("Errorcode # %d from StreamData read.\n", (unsigned int)recBuff[11]); ret = -1; goto cleanmem; } if(packetCounter != (int)recBuff[m*46 + 10]) { printf("PacketCounter (%d) does not match with with current packet count (%d) (StreamData).\n", packetCounter, (int)recBuff[m*46 + 10]); ret = -1; goto cleanmem; } backLog = recBuff[m*46 + 45] & 0x7F; //Checking MSB for Comm buffer overflow if( (recBuff[m*46 + 45] & 128) == 128) { printf("\nComm buffer overflow detected in packet %d\n", totalPackets); printf("Current Comm backlog: %d\n", recBuff[m*46 + 45] & 0x7F); overflow = 1; } for(k = 12; k < 43; k += 2) { voltageBytes = (uint16)recBuff[m*46 + k] + (uint16)recBuff[m*46 + k+1] * 256; binaryToCalibratedAnalogVoltage(caliInfo, (uint8)(0x00), ainResolution, voltageBytes, &(voltages[scanNumber][currChannel])); currChannel++; if(currChannel > 3) { currChannel = 0; scanNumber++; } } if(packetCounter >= 255) packetCounter = 0; else packetCounter++; //Handle Comm buffer overflow by stopping, flushing and restarting stream if(overflow == 1) { printf("\nRestarting stream...\n"); doFlush(socketFDA); closeTCPConnection(socketFDB); if( (socketFDB = openTCPConnection(ipAddress, ue9_portB)) < 0) goto cleanmem; if(StreamConfig_example(socketFDA) != 0) { printf("Error restarting StreamConfig.\n"); ret = -1; goto cleanmem; } if(StreamStart(socketFDA) != 0) { printf("Error restarting StreamStart.\n"); ret = -1; goto cleanmem; } packetCounter = 0; break; } } printf("\nNumber of scans: %d\n", scanNumber); printf("Total packets read: %d\n", totalPackets); printf("Current PacketCounter: %d\n", ((packetCounter == 0) ? 255 : packetCounter-1)); printf("Current Comm backlog: %d\n", backLog); for(k = 0; k < 4; k++) printf(" AI%d: %.4f V\n", k, voltages[scanNumber - 1][k]); } endTime = getTickCount(); printf("\nRate of samples: %.0lf samples per second\n", (scanNumber*NumChannels)/((endTime - startTime)/1000.0)); printf("Rate of scans: %.0lf scans per second\n\n", scanNumber/((endTime - startTime)/1000.0)); cleanmem: free(recBuff); recBuff = NULL; for(i = 0; i < totalScans; i++) { free(voltages[i]); voltages[i] = NULL; } free(voltages); voltages = NULL; return ret; }