long getTempKCalibrated(u6CalibrationInfo *caliInfo, int resolutionIndex, int gainIndex, int bits24, uint32 bytesTemp, double *kelvinTemp) { double value; //convert to voltage first if(getAinVoltCalibrated(caliInfo, resolutionIndex, gainIndex, bits24, bytesTemp, &value) == -1) return -1; *kelvinTemp = caliInfo->ccConstants[22]*value + caliInfo->ccConstants[23]; return 0; }
long eAIN(HANDLE Handle, ue9CalibrationInfo *CalibrationInfo, long ChannelP, long ChannelN, double *Voltage, long Range, long Resolution, long Settling, long Binary, long Reserved1, long Reserved2) { uint8 IOType, Channel, AINM, AINH, ainGain; uint16 bytesVT; if( isCalibrationInfoValid(CalibrationInfo) == 0 ) { printf("eAIN error: calibration information is required"); return -1; } if( Range == LJ_rgBIP5V ) ainGain = 8; else if( Range == LJ_rgUNI5V ) ainGain = 0; else if( Range == LJ_rgUNI2P5V ) ainGain = 1; else if( Range == LJ_rgUNI1P25V ) ainGain = 2; else if( Range == LJ_rgUNIP625V ) ainGain = 3; else { printf("eAIN error: Invalid Range\n"); return -1; } if( ehSingleIO(Handle, 4, (uint8)ChannelP, ainGain, (uint8)Resolution, (uint8)Settling, &IOType, &Channel, NULL, &AINM, &AINH) < 0 ) return -1; bytesVT = AINM + AINH*256; if( Binary != 0 ) { *Voltage = (double)bytesVT; } else { if( ChannelP == 133 || ChannelP == 141 ) { if( getTempKCalibrated(CalibrationInfo, 0, bytesVT, Voltage) < 0 ) return -1; } else { if( getAinVoltCalibrated(CalibrationInfo, ainGain, (uint8)Resolution, bytesVT, Voltage) < 0 ) return -1; } } return 0; }
//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; }
long eAIN(HANDLE Handle, u6CalibrationInfo *CalibrationInfo, long ChannelP, long ChannelN, double *Voltage, long Range, long Resolution, long Settling, long Binary, long Reserved1, long Reserved2) { uint8 sendDataBuff[4], recDataBuff[5]; uint8 diff, gain, Errorcode, ErrorFrame; uint32 bytesV; if(isCalibrationInfoValid(CalibrationInfo) != 1) { printf("eAIN error: Invalid calibration information.\n"); return -1; } //Checking if acceptable positive channel if(ChannelP < 0 || ChannelP > 143) { printf("eAIN error: Invalid ChannelP value.\n"); return -1; } //Checking if single ended or differential readin if(ChannelN == 0 || ChannelN == 15) { //single ended reading diff = 0; } else if((ChannelN&1) == 1 && ChannelN == ChannelP + 1) { //differential reading diff = 1; } else { printf("eAIN error: Invalid ChannelN value.\n"); return -1; } if(Range == LJ_rgAUTO) gain = 15; else if(Range == LJ_rgBIP10V) gain = 0; else if(Range == LJ_rgBIP1V) gain = 1; else if(Range == LJ_rgBIPP1V) gain = 2; else if(Range == LJ_rgBIPP01V) gain = 3; else { printf("eAIN error: Invalid Range value\n"); return -1; } if(Resolution < 0 || Resolution > 13) { printf("eAIN error: Invalid Resolution value\n"); return -1; } if(Settling < 0 && Settling > 4) { printf("eAIN error: Invalid Settling value\n"); return -1; } /* Setting up Feedback command to read analog input */ sendDataBuff[0] = 3; //IOType is AIN24AR sendDataBuff[1] = (uint8)ChannelP; //Positive channel sendDataBuff[2] = (uint8)Resolution + gain*16; //Res Index (0-3), Gain Index (4-7) sendDataBuff[3] = (uint8)Settling + diff*128; //Settling factor (0-2), Differential (7) if(ehFeedback(Handle, sendDataBuff, 4, &Errorcode, &ErrorFrame, recDataBuff, 5) < 0) return -1; if(Errorcode) return (long)Errorcode; bytesV = recDataBuff[0] + ((uint32)recDataBuff[1])*256 + ((uint32)recDataBuff[2])*65536; gain = recDataBuff[3]/16; if(Binary != 0) { *Voltage = (double)bytesV; } else { if(ChannelP == 14) { if(getTempKCalibrated(CalibrationInfo, Resolution, gain, 1, bytesV, Voltage) < 0) return -1; } else { gain = recDataBuff[3]/16; if(getAinVoltCalibrated(CalibrationInfo, Resolution, gain, 1, bytesV, Voltage) < 0) return -1; } } return 0; }
long getAinVoltUncalibrated(int resolutionIndex, int gainIndex, int bits24, uint32 bytesVolt, double *analogVolt) { return getAinVoltCalibrated(&U6_CALIBRATION_INFO_DEFAULT, resolutionIndex, gainIndex, bits24, bytesVolt, analogVolt); }
//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; }
long eAIN(HANDLE Handle, u3CalibrationInfo *CalibrationInfo, long ConfigIO, long *DAC1Enable, long ChannelP, long ChannelN, double *Voltage, long Range, long Resolution, long Settling, long Binary, long Reserved1, long Reserved2) { uint8 sendDataBuff[3], recDataBuff[2]; uint8 FIOAnalog, EIOAnalog, curFIOAnalog, curEIOAnalog, curTCConfig; uint8 settling, quicksample, Errorcode, ErrorFrame; uint8 outDAC1Enable; uint16 bytesVT; long error; double hwver; int hv; int isSpecialRange = 0; if(isCalibrationInfoValid(CalibrationInfo) == 0) { printf("eAIN error: calibration information is required"); return -1; } hwver = CalibrationInfo->hardwareVersion; hv = CalibrationInfo->highVoltage; if(ChannelP < 0 || (ChannelP > 15 && ChannelP != 30 && ChannelP != 31)) { printf("eAIN error: Invalid positive channel\n"); return -1; } if(ChannelN < 0 || (ChannelN > 15 && ChannelN != 30 && ChannelN != 31 && ChannelN != 32) || (hwver >= 1.30 && hv == 1 && ((ChannelP < 4 && ChannelN != 31 && ChannelN != 32) || ChannelN < 4))) { printf("eAIN error: Invalid negative channel\n"); return -1; } if (ChannelN == 32) { isSpecialRange = 1; ChannelN = 30; // Set to 30 for the feedback packet. We'll set it back to 32 for conversion. } if(ConfigIO != 0 && !(hwver >= 1.30 && hv == 1 && ChannelP < 4)) { FIOAnalog = 0; EIOAnalog = 0; //Setting ChannelP and ChannelN channels to analog using //FIOAnalog and EIOAnalog if(ChannelP <= 7) FIOAnalog = pow(2, ChannelP); else if(ChannelP <= 15) EIOAnalog = pow(2, (ChannelP - 8)); if(ChannelN <= 7) FIOAnalog = FIOAnalog | (int)pow(2, ChannelN); else if(ChannelN <= 15) EIOAnalog = EIOAnalog | (int)pow(2, (ChannelN - 8)); //Using ConfigIO to get current FIOAnalog and EIOAnalog settings if((error = ehConfigIO(Handle, 0, 0, 0, 0, 0, &curTCConfig, &outDAC1Enable, &curFIOAnalog, &curEIOAnalog)) != 0) return error; *DAC1Enable = outDAC1Enable; if( !(FIOAnalog == curFIOAnalog && EIOAnalog == curEIOAnalog) ) { //Creating new FIOAnalog and EIOAnalog settings FIOAnalog = FIOAnalog | curFIOAnalog; EIOAnalog = EIOAnalog | curEIOAnalog; //Using ConfigIO to set new FIOAnalog and EIOAnalog settings if((error = ehConfigIO(Handle, 12, curTCConfig, 0, FIOAnalog, EIOAnalog, NULL, NULL, &curFIOAnalog, &curEIOAnalog)) != 0) return error; } } /* Setting up Feedback command to read analog input */ sendDataBuff[0] = 1; //IOType is AIN settling = (Settling != 0) ? 1 : 0; quicksample = (Resolution != 0) ? 1 : 0; sendDataBuff[1] = (uint8)ChannelP + settling*64 + quicksample*128; //Positive channel (bits 0-4), LongSettling (bit 6) //QuickSample (bit 7) sendDataBuff[2] = (uint8)ChannelN; //Negative channel if(ehFeedback(Handle, sendDataBuff, 3, &Errorcode, &ErrorFrame, recDataBuff, 2) < 0) return -1; if(Errorcode) return (long)Errorcode; bytesVT = recDataBuff[0] + recDataBuff[1]*256; if (isSpecialRange) { ChannelN = 32; // Change the negative channel back to 32 from 30 for conversion. } if(Binary != 0) { *Voltage = (double)bytesVT; } else { if(ChannelP == 30) { if(getTempKCalibrated(CalibrationInfo, bytesVT, Voltage) < 0) return -1; } else { if(hwver < 1.30) error = getAinVoltCalibrated(CalibrationInfo, (int)(*DAC1Enable), ChannelN, bytesVT, Voltage); else error = getAinVoltCalibrated_hw130(CalibrationInfo, ChannelP, ChannelN, bytesVT, Voltage); if(error < 0) return -1; } } return 0; }
long getAinVoltUncalibrated(int dacEnabled, uint8 negChannel, uint16 bytesVolt, double *analogVolt) { U3_CALIBRATION_INFO_DEFAULT.hardwareVersion = 1.20; U3_CALIBRATION_INFO_DEFAULT.highVoltage = 0; return getAinVoltCalibrated(&U3_CALIBRATION_INFO_DEFAULT, dacEnabled, negChannel, bytesVolt, analogVolt); }
long getAinVoltUncalibrated(uint8 gainBip, uint8 resolution, uint16 bytesVolt, double *analogVolt) { return getAinVoltCalibrated(&UE9_CALIBRATION_INFO_DEFAULT, gainBip, resolution, bytesVolt, analogVolt); }
//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; }
//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; }
//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; }