public void performActions() { long time = 0, i = 0; long numIterations = 1000; double numBytesToWrite; double numBytesToRead; byte[] writeArray = { 0x70, 0x70 }; byte[] readArray = { 0 }; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } try { time = Environment.TickCount; for (i = 0; i < numIterations; i++) { numBytesToWrite = 2; numBytesToRead = 2; //Raw Out LJUD.eGet(ue9.ljhandle, LJUD.IO.RAW_OUT, 0, ref numBytesToWrite, writeArray); //Raw In LJUD.eGet(ue9.ljhandle, LJUD.IO.RAW_IN, 0, ref numBytesToRead, readArray); } } catch (LabJackUDException e) { showErrorMessage(e); } // Display the results time = Environment.TickCount - time; Console.Out.WriteLine("Milleseconds per iteration = {0:0.###}\n", (double)time / (double)numIterations); Console.ReadLine(); // Pause for user }
/// <summary> /// Configure and start the stream on the LabJack /// </summary> /// <returns>True if successful and false otherwise</returns> private bool StartStreaming() { //Read and display the UD version. dblValue = LJUD.GetDriverVersion(); versionDisplay.Text = String.Format("{0:0.000}", dblValue); try { //Open the first found LabJack UE9. ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //Read and display the hardware version of this UE9. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.HARDWARE_VERSION, ref dblValue, 0); hardwareDisplay.Text = String.Format("{0:0.000}", dblValue); //Read and display the firmware version of this UE9. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.FIRMWARE_VERSION, ref dblValue, 0); firmwareDisplay.Text = String.Format("{0:0.000}", dblValue); //Configure the analog input range on channel 0 for bipolar +-5 volts. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, 0, (double)LJUD.RANGES.BIP5V, 0, 0); //Configure the stream: //Set the scan rate. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_SCAN_FREQUENCY, scanRate, 0, 0); //Give the driver a 5 second buffer (scanRate * 2 channels * 5 seconds). LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_BUFFER_SIZE, scanRate * 2 * 5, 0, 0); //Configure reads to retrieve whatever data is available without waiting (wait mode LJUD.STREAMWAITMODES.NONE). //See comments below to change this program to use LJUD.STREAMWAITMODES.SLEEP mode. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_WAIT_MODE, (double)LJUD.STREAMWAITMODES.NONE, 0, 0); //Define the scan list as AIN0 then AIN1. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.CLEAR_STREAM_CHANNELS, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 1, 0, 0, 0); //Execute the list of requests. LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { ShowErrorMessage(e); return(false); } //Get all the results just to check for errors. try { LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { ShowErrorMessage(e); } bool finished = false; while (!finished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { finished = true; } else { ShowErrorMessage(e); } } } //Start the stream. try { LJUD.eGet(ue9.ljhandle, LJUD.IO.START_STREAM, 0, ref dblValue, 0); } catch (LabJackUDException e) { ShowErrorMessage(e); return(false); } //The actual scan rate is dependent on how the desired scan rate divides into //the LabJack clock. The actual scan rate is returned in the value parameter //from the start stream command. scanDisplay.Text = String.Format("{0:0.000}", dblValue); sampleDisplay.Text = String.Format("{0:0.000}", 2 * dblValue); // # channels * scan rate // The stream started successfully return(true); }
public void performActions() { // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } // Variables to pass to and from the driver object long ainResolution = 12; UE9.IO IOType = (UE9.IO) 0; UE9.CHANNEL channel = (UE9.CHANNEL) 0; double val = 0; double dblVal = 0; // Temporary variable required by GetFirstResult int intVal = 0; // Temporary variable required by GetFirstResult // General variable settings long numIterations = 1000; long numChannels = 6; //Number of AIN channels, 0-16. // Variables to store results double ValueDIPort = 0; double[] ValueAIN = new double[16]; try { // Set DAC0 to 2.5 volts LJUD.AddRequest(ue9.ljhandle, UE9.IO.PUT_DAC, 0, 2.5, 0, 0); // Set DAC1 to 3.5 volts LJUD.AddRequest(ue9.ljhandle, UE9.IO.PUT_DAC, 1, 3.5, 0, 0); //Write all digital I/O. Doing a bunch of bit instructions, rather than //the following port instruction, should not make a noticable difference //in the overall execution time. LJUD.AddRequest(ue9.ljhandle, UE9.IO.PUT_DIGITAL_PORT, 0, 0, 23, 0); //Configure the desired resolution. Note that depending on resolution and //number of analog inputs, numIterations might need to be reduced from the //default above so the program does not take too long to execute. LJUD.AddRequest(ue9.ljhandle, UE9.IO.PUT_CONFIG, UE9.CHANNEL.AIN_RESOLUTION, ainResolution, 0, 0); //Add analog input requests. for (int j = 0; j < numChannels; j++) { LJUD.AddRequest(ue9.ljhandle, UE9.IO.GET_AIN, j, 0, 0, 0); } //Request a read of all digital I/O. Doing a bunch of bit instructions, //rather than the following port instruction, should not make a noticable //difference in the overall execution time. LJUD.AddRequest(ue9.ljhandle, UE9.IO.GET_DIGITAL_PORT, 0, 0, 23, 0); } catch (LabJackUDException e) { showErrorMessage(e); } // Get the current tick count for a reference frame double time = System.Environment.TickCount; for (int i = 0; i < numIterations; i++) { try { //Execute the requests. LJUD.GoOne(ue9.ljhandle); //Get all the results. The input measurement results are stored. All other //results are for configuration or output requests so we are just checking //whether there was an error. LJUD.GetFirstResult(ue9.ljhandle, ref IOType, ref channel, ref val, ref intVal, ref dblVal); } catch (LabJackUDException e) { showErrorMessage(e); } // Get results until there is no more data available bool isFinished = false; while (!isFinished) { switch (IOType) { case LJUD.IO.GET_AIN: ValueAIN[(int)channel] = val; break; case LJUD.IO.GET_DIGITAL_PORT: ValueDIPort = val; break; } try { LJUD.GetNextResult(ue9.ljhandle, ref IOType, ref channel, ref val, ref intVal, ref dblVal); } catch (LabJackUDException e) { // If we get an error, report it. If there is no more data available we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } } // Determine how long it took to perform the above tasks and display the millisecounds per iteration along with the readings time = System.Environment.TickCount - time; Console.Out.WriteLine("Milleseconds per iteration = " + ((double)time / (double)numIterations) + "\n"); Console.Out.WriteLine("\nDigital Input = " + ValueDIPort + "\n"); Console.Out.WriteLine("\nAIN readings from last iteration:\n"); for (int j = 0; j < numChannels; j++) { Console.Out.WriteLine("{0:0.###}\n", ValueAIN[j]); } Console.ReadLine(); // Pause for the user }
/// <summary> /// Actually performs actions on the UE9 and updates the displaye /// </summary> /// <param name="sender">The object that executed this method</param> /// <param name="e">Event parameters</param> private void goButton_Click(object sender, System.EventArgs e) { double dblDriverVersion; LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double Value2 = 0, Value3 = 0; double ValueDIBit = 0, ValueDIPort = 0, ValueCounter = 0; // dummy variables to satisfy certian method signatures double dummyDouble = 0; int dummyInt = 0; //Read and display the UD version. dblDriverVersion = LJUD.GetDriverVersion(); versionDisplay.Text = String.Format("{0:0.000}", dblDriverVersion); // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB } catch (LabJackUDException exc) { ShowErrorMessage(exc); return; } try { //First some configuration commands. These will be done with the ePut //function which combines the add/go/get into a single call. //Configure for 16-bit analog input measurements. LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, 16, 0); //Configure the analog input range on channels 2 and 3 for bipolar 5v. LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 2, (double)LJUD.RANGES.BIP5V, 0); LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 3, (double)LJUD.RANGES.BIP5V, 0); //Enable Counter0 which will appear on FIO0 (assuming no other //program has enabled any timers or Counter1). LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 1, 0); //Now we add requests to write and read I/O. These requests //will be processed repeatedly by go/get statements in every //iteration of the while loop below. //Request AIN2 and AIN3. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_AIN, 2, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_AIN, 3, 0, 0, 0); //Set DAC0 to 2.5 volts. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_DAC, 0, 2.5, 0, 0); //Read digital input FIO1. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_DIGITAL_BIT, 1, 0, 0, 0); //Read digital inputs FIO2 through FIO3. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_DIGITAL_PORT, 2, 0, 2, 0); // LJUD.AddRequest (ue9.ljhandle, LJUD.IO.GET_DIGITAL_PORT, 2, 0, 3, 0); would request through FIO4 //Request the value of Counter0. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_COUNTER, 0, 0, 0, 0); } catch (LabJackUDException exc) { ShowErrorMessage(exc); return; } try { //Execute the requests. LJUD.GoOne(ue9.ljhandle); //Get all the results. The input measurement results are stored. All other //results are for configuration or output requests so we are just checking //whether there was an error. LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException exc) { ShowErrorMessage(exc); return; } bool isFinished = false; while (!isFinished) { switch (ioType) { case LJUD.IO.GET_AIN: switch ((int)channel) { case 2: Value2 = dblValue; break; case 3: Value3 = dblValue; break; } break; case LJUD.IO.GET_DIGITAL_BIT: ValueDIBit = dblValue; break; case LJUD.IO.GET_DIGITAL_PORT: ValueDIPort = dblValue; break; case LJUD.IO.GET_COUNTER: ValueCounter = dblValue; break; } try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException exc) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (exc.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { ShowErrorMessage(exc); } } } // Display results ain2Display.Text = String.Format("{0:0.###}", Value2); ain3Display.Text = String.Format("{0:0.###}", Value3); fio1Display.Text = String.Format("{0:0.###}", ValueDIBit); fio2Display.Text = String.Format("{0:0.###}", ValueDIPort); //Will read 30 (binary 11) if both lines are pulled-high as normal. counter0Display.Text = String.Format("{0:0.###}", ValueCounter); }
public void performActions() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double numSPIBytesToTransfer; byte[] dataArray = new byte[50]; // Variables to satisfy certain method signatures int dummyInt = 0; double dummyDouble = 0; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } try { //First, configure the SPI communication. //Enable automatic chip-select control. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_AUTO_CS, 1, 0, 0); //Do not disable automatic digital i/o direction configuration. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_DISABLE_DIR_CONFIG, 0, 0, 0); //Mode A: CPHA=1, CPOL=1. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_MODE, 0, 0, 0); //125kHz clock. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_CLOCK_FACTOR, 0, 0, 0); //MOSI is FIO2 LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_MOSI_PIN_NUM, 2, 0, 0); //MISO is FIO3 LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_MISO_PIN_NUM, 3, 0, 0); //CLK is FIO0 LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_CLK_PIN_NUM, 0, 0, 0); //CS is FIO1 LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_CS_PIN_NUM, 1, 0, 0); //Execute the requests on a single LabJack. The driver will use a //single low-level TimerCounter command to handle all the requests above. LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } // Get results until there is no more data available for error checking bool isFinished = false; while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dummyDouble, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } //This example transfers 4 test bytes. numSPIBytesToTransfer = 4; dataArray[0] = 170; dataArray[1] = 240; dataArray[2] = 170; dataArray[3] = 240; //Transfer the data. The write and read is done at the same time. try { LJUD.eGet(ue9.ljhandle, LJUD.IO.SPI_COMMUNICATION, 0, ref numSPIBytesToTransfer, dataArray); } catch (LabJackUDException e) { showErrorMessage(e); } //Display the read data. Console.Out.WriteLine("dataArray[0] = {0:0.#}\n", dataArray[0]); Console.Out.WriteLine("dataArray[1] = {0:0.#}\n", dataArray[1]); Console.Out.WriteLine("dataArray[2] = {0:0.#}\n", dataArray[2]); Console.Out.WriteLine("dataArray[3] = {0:0.#}\n", dataArray[3]); Console.ReadLine(); // Pause for user }
public void performActions() { // long lngGetNextIteration; // LJUD.IO ioType=0, channel=0; // double dblValue=0; long i = 0; double pinNum = 0; //0 means the LJTick-DAC is connected to FIO0/FIO1. int[] achrUserMem = new int[64]; double[] adblCalMem = new double[4]; double serialNumber = 0; Random random = new Random(); // Dummy variables to satisfy certain method signatures double dummyDouble = 0; //Open the LabJack. try { device = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB } catch (LabJackUDException e) { showErrorMessage(e); } try { //Specify where the LJTick-DAC is plugged in. //This is just setting a parameter in the driver, and not actually talking //to the hardware, and thus executes very fast. LJUD.ePut(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TDAC_SCL_PIN_NUM, pinNum, 0); //Set DACA to 1.2 volts. If the driver has not previously talked to an LJTDAC //on the specified pins, it will first retrieve and store the cal constants. The //low-level I2C command can only update 1 DAC channel at a time, so there //is no advantage to doing two updates within a single add-go-get block. LJUD.ePut(device.ljhandle, LJUD.IO.TDAC_COMMUNICATION, LJUD.CHANNEL.TDAC_UPDATE_DACA, 1.2, 0); Console.Out.WriteLine("DACA set to 1.2 volts\n\n"); //Set DACB to 2.3 volts. LJUD.ePut(device.ljhandle, LJUD.IO.TDAC_COMMUNICATION, LJUD.CHANNEL.TDAC_UPDATE_DACB, 2.3, 0); Console.Out.WriteLine("DACB set to 2.3 volts\n\n"); //Now for more advanced operations. //If at this point you removed that LJTDAC and plugged a different one //into the same pins, the driver would not know and would use the wrong //cal constants on future updates. If we do a cal constant read, //the driver will store the constants from the new read. LJUD.eGet(device.ljhandle, LJUD.IO.TDAC_COMMUNICATION, LJUD.CHANNEL.TDAC_READ_CAL_CONSTANTS, ref dummyDouble, adblCalMem); Console.Out.WriteLine("DACA Slope = {0:0.0} bits/volt\n", adblCalMem[0]); Console.Out.WriteLine("DACA Offset = {0:0.0} bits\n", adblCalMem[1]); Console.Out.WriteLine("DACB Slope = {0:0.0} bits/volt\n", adblCalMem[2]); Console.Out.WriteLine("DACB Offset = {0:0.0} bits\n\n", adblCalMem[3]); //Read the serial number. LJUD.eGet(device.ljhandle, LJUD.IO.TDAC_COMMUNICATION, LJUD.CHANNEL.TDAC_SERIAL_NUMBER, ref serialNumber, 0); Console.Out.WriteLine("LJTDAC Serial Number = {0:0}\n\n", serialNumber); } catch (LabJackUDException e) { showErrorMessage(e); } Console.ReadLine(); // Pause for user return; }
public void performActions() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double valueAIN = 0; //Analog Voltage Value long time = 0; LJUD.CHANNEL tempChannel = 0; //Channel which the TC/LJTIA is on (AIN0). long ainResolution = 17; // Variables to satisfy certain method signatures double dummyDouble = 0; int dummyInt = 0; double tcVolts = 0, cjTempK = 0, pTCTempK = 0; LJUD.THERMOCOUPLETYPE tcType = LJUD.THERMOCOUPLETYPE.K; //Set the temperature sensor to a k type thermocouple //Possible Thermocouple types are: //LJUD.THERMOCOUPLETYPE.B = 6001 //LJUD.THERMOCOUPLETYPE.E = 6002 //LJUD.THERMOCOUPLETYPE.J = 6003 //LJUD.THERMOCOUPLETYPE.K = 6004 //LJUD.THERMOCOUPLETYPE.N = 6005 //LJUD.THERMOCOUPLETYPE.R = 6006 //LJUD.THERMOCOUPLETYPE.S = 6007 //LJUD.THERMOCOUPLETYPE.T = 6008 //Offset calibration: The nominal voltage offset of the LJTick is //0.4 volts. For improved accuracy, though, you should measure the //overall system offset. We know that if the end of the TC is at the //same temperature as the cold junction, the voltage should be zero. //Put the end of the TC near the LJTIA to make sure they are at the same //temperature, and note the voltage measured by AIN0. This is the actual //offset that can be entered below. double offsetVoltage = 0.4; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } //Configure the desired resolution. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, ainResolution, 0, 0); Console.Out.WriteLine("Press any key to quit\n"); //Constantly acquire temperature readings until a key is pressed bool keyPressed = false; while (!keyPressed) { ioType = 0; channel = 0; time = 0; tcVolts = 0; cjTempK = 0; pTCTempK = 0; //Add analog input requests. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_AIN, tempChannel, 0, 0, 0); //Add request for internal temperature reading -- Internal temp sensor uses //analog input channel 133. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_AIN, 133, 0, 0, 0); //Execute all requests on the labjack ue9.ljhandle. LJUD.GoOne(ue9.ljhandle); //Get all the results. The first result should be the voltage reading of the //temperature channel. LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); //Get the rest of the results. There should only be one more on the request //queue. bool finished = false; while (!finished) { if (ioType == LJUD.IO.GET_AIN) { if (channel == tempChannel) { valueAIN = dblValue; } if (channel == (LJUD.CHANNEL) 133) { cjTempK = dblValue; } } try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { if (e.LJUDError == LJUD.LJUDERROR.NO_DATA_AVAILABLE) { finished = true; } else if (e.LJUDError > LJUD.LJUDERROR.MIN_GROUP_ERROR) { finished = true; } else { showErrorMessage(e); } } } //Display Voltage Reading Console.Out.WriteLine("Analog {0:0}: {1:0.######}\n", (int)tempChannel, valueAIN); //Display the internal temperature sensor reading. This example uses //that value for cold junction compensation. Console.Out.WriteLine("UE9 internal sensor:{0:0.0} deg K\n", (double)cjTempK); //To get the thermocouple voltage we subtract the offset from the AIN //voltage and divide by the LJTIA gain. tcVolts = (valueAIN - offsetVoltage) / 51; //Convert TC voltage to temperature. LJUD.TCVoltsToTemp(tcType, tcVolts, cjTempK, ref pTCTempK); //Display Temperature Console.Out.WriteLine("Thermocouple sensor:{0:0.0} deg K\n\n", pTCTempK); Thread.Sleep(1500); // Short pause keyPressed = Win32Interop._kbhit() != 0; // If a key was hit break out of the loop } }
public void performActions() { double dblValue = 0; int intValue = 0; LJUD.RANGES range; int intResolution; int intBinary; int[] aintEnableTimers = new int[6]; int[] aintEnableCounters = new int[2]; int intTimerClockBaseIndex; int intTimerClockDivisor; int[] aintTimerModes = new int[6]; double[] adblTimerValues = new double[6]; int[] aintReadTimers = new int[6]; int[] aintUpdateResetTimers = new int[6]; int[] aintReadCounters = new int[2]; int[] aintResetCounters = new int[2]; double[] adblCounterValues = { 0, 0 }; double highTime, lowTime, dutyCycle; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } try { //Take a measurement from AIN3. range = LJUD.RANGES.BIP5V; intResolution = 17; intBinary = 0; LJUD.eAIN(ue9.ljhandle, 3, 0, ref dblValue, (int)range, (int)intResolution, 0, 0); Console.Out.WriteLine("AIN3 = {0:0.###}\n", dblValue); //Set DAC0 to 3.0 volts. dblValue = 3.0; intBinary = 0; LJUD.eDAC(ue9.ljhandle, 0, dblValue, intBinary, 0, 0); Console.Out.WriteLine("DAC0 set to {0:0.###} volts\n", dblValue); //Read state of FIO2. LJUD.eDI(ue9.ljhandle, 2, ref intValue); Console.Out.WriteLine("FIO2 = {0:0.###}\n", intValue); //Set the state of FIO3. intValue = 0; LJUD.eDO(ue9.ljhandle, 3, intValue); Console.Out.WriteLine("FIO3 set to = {0:0.###}\n\n", intValue); //Timers and Counters example. //First, a call to eTCConfig. Fill the arrays with the desired values, then make the call. intTimerClockBaseIndex = (int)LJUD.TIMERCLOCKS.KHZ750; //Choose 750 kHz base clock. intTimerClockDivisor = 3; //Divide by 3, thus timer clock is 250 kHz. aintEnableTimers[0] = 1; //Enable Timer0 (uses FIO0). aintEnableTimers[1] = 1; //Enable Timer1 (uses FIO1). aintEnableTimers[2] = 1; //Enable Timer2 (uses FIO2). aintEnableTimers[3] = 1; //Enable Timer3 (uses FIO3). aintEnableTimers[4] = 0; //Disable Timer4. aintEnableTimers[5] = 0; //Disable Timer5. aintTimerModes[0] = (int)LJUD.TIMERMODE.PWM8; //Timer0 is 8-bit PWM output. Frequency is 250k/256 = 977 Hz. aintTimerModes[1] = (int)LJUD.TIMERMODE.DUTYCYCLE; //Timer1 is duty cyle input. aintTimerModes[2] = (int)LJUD.TIMERMODE.FIRMCOUNTER; //Timer2 is firmware counter input. aintTimerModes[3] = (int)LJUD.TIMERMODE.RISINGEDGES16; //Timer3 is 16-bit period measurement. aintTimerModes[4] = 0; //Timer4 not enabled. aintTimerModes[5] = 0; //Timer5 not enabled. adblTimerValues[0] = 16384; //Set PWM8 duty-cycle to 75%. adblTimerValues[1] = 0; adblTimerValues[2] = 0; adblTimerValues[3] = 0; adblTimerValues[4] = 0; adblTimerValues[5] = 0; aintEnableCounters[0] = 1; //Enable Counter0 (uses FIO4). aintEnableCounters[1] = 1; //Enable Counter1 (uses FIO5). LJUD.eTCConfig(ue9.ljhandle, aintEnableTimers, aintEnableCounters, 0, (int)intTimerClockBaseIndex, intTimerClockDivisor, aintTimerModes, adblTimerValues, 0, 0); Console.Out.WriteLine("Timers and Counters enabled.\n"); Thread.Sleep(1000); //Wait 1 second. //Now, a call to eTCValues. aintReadTimers[0] = 0; //Don't read Timer0 (output timer). aintReadTimers[1] = 1; //Read Timer1; aintReadTimers[2] = 1; //Read Timer2; aintReadTimers[3] = 1; //Read Timer3; aintReadTimers[4] = 0; //Timer4 not enabled. aintReadTimers[5] = 0; //Timer5 not enabled. aintUpdateResetTimers[0] = 1; //Update Timer0; aintUpdateResetTimers[1] = 1; //Reset Timer1; aintUpdateResetTimers[2] = 1; //Reset Timer2; aintUpdateResetTimers[3] = 1; //Reset Timer3; aintUpdateResetTimers[4] = 0; //Timer4 not enabled. aintUpdateResetTimers[5] = 0; //Timer5 not enabled. aintReadCounters[0] = 1; //Read Counter0; aintReadCounters[1] = 1; //Read Counter1; aintResetCounters[0] = 1; //Reset Counter0. aintResetCounters[1] = 1; //Reset Counter1. adblTimerValues[0] = 32768; //Change Timer0 duty-cycle to 50%. adblTimerValues[1] = 0; adblTimerValues[2] = 0; adblTimerValues[3] = 0; adblTimerValues[4] = 0; adblTimerValues[5] = 0; LJUD.eTCValues(ue9.ljhandle, aintReadTimers, aintUpdateResetTimers, aintReadCounters, aintResetCounters, adblTimerValues, adblCounterValues, 0, 0); Console.Out.WriteLine("Timer1 value = {0:0.###}", adblTimerValues[1]); Console.Out.WriteLine("Timer2 value = {0:0.###}", adblTimerValues[2]); Console.Out.WriteLine("Timer3 value = {0:0.###}", adblTimerValues[3]); Console.Out.WriteLine("Counter0 value = {0:0.###}", adblCounterValues[0]); Console.Out.WriteLine("Counter1 value = {0:0.###}", adblCounterValues[1]); //Convert Timer1 value to duty-cycle percentage. //High time is LSW highTime = (double)(((ulong)adblTimerValues[1]) % (65536)); //Low time is MSW lowTime = (double)(((ulong)adblTimerValues[1]) / (65536)); //Calculate the duty cycle percentage. dutyCycle = 100 * highTime / (highTime + lowTime); Console.Out.WriteLine("\nHigh clicks Timer1 = {0:0.###}", highTime); Console.Out.WriteLine("Low clicks Timer1 = {0:0.###}", lowTime); Console.Out.WriteLine("Duty cycle Timer1 = {0:0.###}", dutyCycle); //Disable all timers and counters. aintEnableTimers[0] = 0; aintEnableTimers[1] = 0; aintEnableTimers[2] = 0; aintEnableTimers[3] = 0; aintEnableTimers[4] = 0; aintEnableTimers[5] = 0; aintEnableCounters[0] = 0; aintEnableCounters[1] = 0; LJUD.eTCConfig(ue9.ljhandle, aintEnableTimers, aintEnableCounters, 0, intTimerClockBaseIndex, intTimerClockDivisor, aintTimerModes, adblTimerValues, 0, 0); } catch (LabJackUDException e) { showErrorMessage(e); } // Pause for the user Console.ReadLine(); }
public void performActions() { long i = 0, k = 0; LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0, dblCommBacklog = 0; // Dummy variables to satisfy certain method signatures double dummyDouble = 0; int dummyInt = 0; double[] dummyDoubleArray = { 0.0 }; //The actual scan rate is determined by the external clock, but we need //an idea of how fast the scan rate will be so that we can make //the buffers big enough. Also, the driver needs to have an idea of the //expected scan rate to help it decide how big of packets to transfer. double scanRate = 1000; int delayms = 1000; double numScans = 2000; //2x the expected # of scans (2*scanRate*delayms/1000) double numScansRequested; double[] adblData = new double[12000]; //Max buffer size (#channels*numScansRequested) // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } try { //Make sure the UE9 is not streaming. LJUD.eGet(ue9.ljhandle, LJUD.IO.STOP_STREAM, (LJUD.CHANNEL) 0, ref dummyDouble, 0); } catch (LabJackUDException e) { // If the error indicates that the stream could not be stopped it is because the stream has not started yet and can be ignored if (e.LJUDError != LJUD.LJUDERROR.UNABLE_TO_STOP_STREAM) { showErrorMessage(e); } } try { //Disable all timers and counters to put everything in a known initial state. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 1, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); //First we will configure Timer0 as system timer low and configure Timer1 to //output a 1000 Hz square wave. //Use the fixed 750kHz timer clock source. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.KHZ750, 0, 0); //Set the divisor to 3 so the actual timer clock is 250 kHz. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_DIVISOR, 3, 0, 0); //Enable 2 timers. They will use FIO0-FIO1. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 2, 0, 0); //Configure Timer0 as system timer low. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, (LJUD.CHANNEL) 0, (double)LJUD.TIMERMODE.SYSTIMERLOW, 0, 0); //Configure Timer1 as frequency output. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, (LJUD.CHANNEL) 1, (double)LJUD.TIMERMODE.FREQOUT, 0, 0); //Set the frequency output on Timer1 to 1000 Hz (250000/(2*125) = 1000). LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 1, 125, 0, 0); //Execute the requests on a single LabJack. The driver will use a //single low-level TimerCounter command to handle all the requests above. LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } //Get all the results just to check for errors. bool isFinished = false; try { LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } try { //Configure the stream: //Configure resolution for all analog inputs. Since the test external clock //is at 1000 Hz, and we are scanning 6 channels, we will have a //sample rate of 6000 samples/second. That means the maximum resolution //we could use is 13-bit. We will use 12-bit in this example. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, 12, 0, 0); //Configure the analog input range on channel 0 for bipolar +-5 volts. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, 0, (double)LJUD.RANGES.BIP5V, 0, 0); //Configure the analog input range on channel 1 for bipolar +-5 volts. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, 1, (double)LJUD.RANGES.BIP5V, 0, 0); //Give the driver a 5 second buffer (scanRate * 6 channels * 5 seconds). LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_BUFFER_SIZE, scanRate * 6 * 5, 0, 0); //Configure reads to retrieve whatever data is available without waiting (wait mode LJ_swNONE). //See comments below to change this program to use LJ_swSLEEP mode. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_WAIT_MODE, (double)LJUD.STREAMWAITMODES.NONE, 0, 0); //Configure for external triggering. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_EXTERNAL_TRIGGER, 1, 0, 0); //Define the scan list. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.CLEAR_STREAM_CHANNELS, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 0, 0, 0, 0); //AIN0 LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 1, 0, 0, 0); //AIN1 LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 193, 0, 0, 0); //EIO_FIO LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 194, 0, 0, 0); //MIO_CIO LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 200, 0, 0, 0); //Timer0 LSW LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 224, 0, 0, 0); //Timer0 MSW //Execute the list of requests. LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } //Get all the results just to check for errors. try { LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } isFinished = false; while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } try { //Start the stream. LJUD.eGet(ue9.ljhandle, LJUD.IO.START_STREAM, 0, ref dblValue, 0); //Read data while (Win32Interop._kbhit() == 0) //Loop will run until any key is hit { //Since we are using wait mode LJUD.STREAMWAITMODES.NONE, we will wait a little, then //read however much data is available. Thus this delay will control how //fast the program loops and how much data is read each loop. An //alternative common method is to use wait mode LJUD.STREAMWAITMODES.SLEEP where the //stream read waits for a certain number of scans. In such a case //you would not have a delay here, since the stream read will actually //control how fast the program loops. // //To change this program to use sleep mode, // -change numScans to the actual number of scans desired per read, // -change wait mode addrequest value to LJ_swSLEEP, // -comment out the following Sleep command. Thread.Sleep(delayms); //Remove if using LJUD.STREAMWAITMODES.SLEEP //init array so we can easily tell if it has changed for (k = 0; k < numScans * 2; k++) { adblData[k] = 9999.0; } //Read the data. We will request twice the number we expect, to //make sure we get everything that is available. //Note that the array we pass must be sized to hold enough SAMPLES, and //the Value we pass specifies the number of SCANS to read. numScansRequested = numScans; LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_STREAM_DATA, LJUD.CHANNEL.ALL_CHANNELS, ref numScansRequested, adblData); //This displays the number of scans that were actually read. Console.Out.WriteLine("\nIteration # {0:0.###}\n", i); Console.Out.WriteLine("Number read = {0:0.###}\n", numScansRequested); //This displays just the first scan. Console.Out.WriteLine("First scan = {0:0.###},{0:0.###},{0:0.###},{0:0.###},{0:0.###},{0:0.###}\n", adblData[0], adblData[1], adblData[2], adblData[3], adblData[4], adblData[5]); //Retrieve the current Comm backlog. The UD driver retrieves stream data from //the UE9 in the background, but if the computer is too slow for some reason //the driver might not be able to read the data as fast as the UE9 is //acquiring it, and thus there will be data left over in the UE9 buffer. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.STREAM_BACKLOG_COMM, ref dblCommBacklog, 0); Console.Out.WriteLine("Comm Backlog = {0:0.###}\n", dblCommBacklog); i++; } //Stop the stream LJUD.eGet(ue9.ljhandle, LJUD.IO.STOP_STREAM, 0, ref dummyDouble, 0); //Disable the timers. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } Console.Out.WriteLine("\nDone"); Console.ReadLine(); // Pause for user return; }
public void performActions() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; int dummyInt = 0; double dummyDouble = 0; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } //Disable all timers and counters to put everything in a known initial state. //Disable the timer and counter, and the FIO lines will return to digital I/O. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 1, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); //Output a PWM output on Timer0 (FIO0) and measure //the duty cycle on Timer1 FIO1 and Timer2 FIO2. //Use the fixed 750kHz timer clock source. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.KHZ750, 0, 0); //Set the divisor to 3 so the actual timer clock is 250kHz. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_DIVISOR, 3, 0, 0); //Enable 2 timers. They will use FIO0 and FIO1. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 3, 0, 0); //Configure Timer0 as 8-bit PWM. Frequency will be 250k/256 = 977 Hz. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 0, (double)LJUD.TIMERMODE.PWM8, 0, 0); //Set the PWM duty cycle to 50%. The passed value is the low time. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 32768, 0, 0); //Configure Timer1 and Timer2 as duty cycle measurement. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 1, (double)LJUD.TIMERMODE.DUTYCYCLE, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 2, (double)LJUD.TIMERMODE.DUTYCYCLE, 0, 0); //Execute the requests on a single LabJack. The driver will use a //single low-level TimerCounter command to handle all the requests above. LJUD.GoOne(ue9.ljhandle); //Get all the results just to check for errors. LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); bool isFinished = false; while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } //Set the PWM duty cycle to 25%. The passed value is the low time. LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 49152, 0); //Now we will reset the duty cycle input timers, so we are sure the //reads we do are not old values from before the PWM output was updated. Thread.Sleep(10); LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, (LJUD.CHANNEL) 1, 0, 0); LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, (LJUD.CHANNEL) 2, 0, 0); //Wait a little so we are sure a duty cycle measurement has occured. Thread.Sleep(10); //Read from Timer1. ReadDutyCycle(ue9.ljhandle, 1); //Read from Timer2. ReadDutyCycle(ue9.ljhandle, 2); //Set the PWM duty cycle to 0%. The passed value is the low time. //We are specifying 65535 out of 65536 clicks to be low. Since //this is 8-bit PWM, we actually get 255 low clicks out of 256 total //clicks, so the minimum duty cycle is 0.4%. LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 65535, 0); //Now we will reset the duty cycle input timers, so we are sure the //reads we do are not old values from before the PWM output was updated. Thread.Sleep(10); LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, (LJUD.CHANNEL) 1, 0, 0); LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, (LJUD.CHANNEL) 2, 0, 0); //Wait a little so we are sure a duty cycle measurement has occured. Thread.Sleep(10); //Read from Timer1. ReadDutyCycle(ue9.ljhandle, 1); //Read from Timer2. ReadDutyCycle(ue9.ljhandle, 2); //Set the PWM duty cycle to 100%. The passed value is the low time. //We are specifying 0 out of 65536 clicks to be low, so the signal //will be high the entire time, meaning there are no edges //for the input timers to detect, and no measurement should be made. LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 0, 0); //Now we will reset the duty cycle input timers, so we are sure the //reads we do are not old values from before the PWM output was updated. Thread.Sleep(10); LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, (LJUD.CHANNEL) 1, 0, 0); LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, (LJUD.CHANNEL) 2, 0, 0); //Wait a little so we are sure a duty cycle measurement has been attempted. Thread.Sleep(10); //Read from Timer1. ReadDutyCycle(ue9.ljhandle, 1); //Read from Timer2. ReadDutyCycle(ue9.ljhandle, 2); //Disable all timers and counters, and the FIO lines will return to digital I/O. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 1, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); Console.ReadLine(); // Pause for user }
public void performActions() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double numI2CBytesToWrite; double numI2CBytesToRead; byte[] writeArray = new byte[128]; byte[] readArray = new byte[128]; long i = 0; long serialNumber = 0; double slopeDACA = 0, offsetDACA = 0, slopeDACB = 0, offsetDACB = 0; double writeACKS = 0, expectedACKS = 0; byte[] bytes; // Dummy variables to satify certain method signatures double dummyDouble = 0; int dummyInt = 0; // Setup random number generator Random random = new Random(); //Open the LabJack. try { device = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB } catch (LabJackUDException e) { showErrorMessage(e); } //Configure the I2C communication. //The address of the EEPROM on the LJTick-DAC is 0xA0. LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_ADDRESS_BYTE, 160, 0, 0); //SCL is FIO0 LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_SCL_PIN_NUM, 0, 0, 0); //SDA is FIO1 LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_SDA_PIN_NUM, 1, 0, 0); //See description of low-level I2C function. LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_OPTIONS, 0, 0, 0); //See description of low-level I2C function. 0 is max speed of about 130 kHz. LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_SPEED_ADJUST, 0, 0, 0); //Execute the requests on a single LabJack. LJUD.GoOne(device.ljhandle); //Get all the results just to check for errors. LJUD.GetFirstResult(device.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); bool finished = false; while (!finished) { try{ LJUD.GetNextResult(device.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { if (e.LJUDError == LJUD.LJUDERROR.NO_MORE_DATA_AVAILABLE) { finished = true; } else { showErrorMessage(e); } } } //Initial read of EEPROM bytes 0-3 in the user memory area. //We need a single I2C transmission that writes the address and then reads //the data. That is, there needs to be an ack after writing the address, //not a stop condition. To accomplish this, we use Add/Go/Get to combine //the write and read into a single low-level call. numI2CBytesToWrite = 1; writeArray[0] = 0; //Memory address. User area is 0-63. LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, numI2CBytesToWrite, writeArray, 0); LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, 0, 0, 0); numI2CBytesToRead = 4; LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_READ, numI2CBytesToRead, readArray, 0); //Execute the requests. LJUD.GoOne(device.ljhandle); //Get the result of the write just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, ref dummyDouble); //Get the write ACKs and compare to the expected value. We expect bit 0 to be //the ACK of the last data byte progressing up to the ACK of the address //byte (data bytes only for Control firmware 1.43 and less). So if n is the //number of data bytes, the ACKs value should be (2^(n+1))-1. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, ref writeACKS); expectedACKS = Math.Pow(2, numI2CBytesToWrite + 1) - 1; if (writeACKS != expectedACKS) { Console.Out.WriteLine("Expected ACKs = {0:0}, Received ACKs = %0.f\n", expectedACKS, writeACKS); } //When the GoOne processed the read request, the read data was put into the readArray buffer that //we passed, so this GetResult is also just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_READ, ref dummyDouble); //Display the first 4 elements. Console.Out.WriteLine("Read User Mem [0-3] = {0:0.#}, {1:0.#}, {2:0.#}, {3:0.#}\n", readArray[0], readArray[1], readArray[2], readArray[3]); //Write EEPROM bytes 0-3 in the user memory area, using the page write technique. Note //that page writes are limited to 16 bytes max, and must be aligned with the 16-byte //page intervals. For instance, if you start writing at address 14, you can only write //two bytes because byte 16 is the start of a new page. numI2CBytesToWrite = 5; writeArray[0] = 0; //Memory address. User area is 0-63. //Create 4 new pseudo-random numbers to write. for (i = 1; i < 5; i++) { writeArray[i] = (byte)random.Next(256); } LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, numI2CBytesToWrite, writeArray, 0); LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, 0, 0, 0); //Execute the requests. LJUD.GoOne(device.ljhandle); //Get the result of the write just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, ref dummyDouble); //Get the write ACKs and compare to the expected value. We expect bit 0 to be //the ACK of the last data byte progressing up to the ACK of the address //byte (data bytes only for Control firmware 1.43 and less). So if n is the //number of data bytes, the ACKs value should be (2^(n+1))-1. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, ref writeACKS); expectedACKS = Math.Pow(2, numI2CBytesToWrite + 1) - 1; if (writeACKS != expectedACKS) { Console.Out.WriteLine("Expected ACKs = {0:0}, Received ACKs = %0.f\n", expectedACKS, writeACKS); } //Delay to allow the EEPROM to complete the write cycle. Datasheet says 1.5 ms max. System.Threading.Thread.Sleep(2); Console.Out.WriteLine("Write User Mem [0-3] = {0:0.#}, {1:0.#}, {2:0.#}, {3:0.#}\n", writeArray[1], writeArray[2], writeArray[3], writeArray[4]); //Final read of EEPROM bytes 0-3 in the user memory area. //We need a single I2C transmission that writes the address and then reads //the data. That is, there needs to be an ack after writing the address, //not a stop condition. To accomplish this, we use Add/Go/Get to combine //the write and read into a single low-level call. numI2CBytesToWrite = 1; writeArray[0] = 0; //Memory address. User area is 0-63. LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, numI2CBytesToWrite, writeArray, 0); LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, 0, 0, 0); numI2CBytesToRead = 4; LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_READ, numI2CBytesToRead, readArray, 0); //Execute the requests. LJUD.GoOne(device.ljhandle); //Get the result of the write just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, ref dummyDouble); //Get the write ACKs and compare to the expected value. We expect bit 0 to be //the ACK of the last data byte progressing up to the ACK of the address //byte (data bytes only for Control firmware 1.43 and less). So if n is the //number of data bytes, the ACKs value should be (2^(n+1))-1. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, ref writeACKS); expectedACKS = Math.Pow(2, numI2CBytesToWrite + 1) - 1; if (writeACKS != expectedACKS) { Console.Out.WriteLine("Expected ACKs = {0:0}, Received ACKs = %0.f\n", expectedACKS, writeACKS); } //When the GoOne processed the read request, the read data was put into the readArray buffer that //we passed, so this GetResult is also just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_READ, ref dummyDouble); //Display the first 4 elements. Console.Out.WriteLine("Read User Mem [0-3] = {0:0.#}, {1:0.#}, {2:0.#}, {3:0.#}\n\n", readArray[0], readArray[1], readArray[2], readArray[3]); //Read cal constants and serial number. //We need a single I2C transmission that writes the address and then reads //the data. That is, there needs to be an ack after writing the address, //not a stop condition. To accomplish this, we use Add/Go/Get to combine //the write and read into a single low-level call. // //64-71 DACA Slope //72-79 DACA Offset //80-87 DACB Slope //88-95 DACB Offset //96-99 Serial Number // numI2CBytesToWrite = 1; writeArray[0] = 64; //Memory address. Cal constants start at 64. LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, numI2CBytesToWrite, writeArray, 0); LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, 0, 0, 0); numI2CBytesToRead = 36; LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_READ, numI2CBytesToRead, readArray, 0); //Execute the requests. LJUD.GoOne(device.ljhandle); //Get the result of the write just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, ref dummyDouble); //Get the write ACKs and compare to the expected value. We expect bit 0 to be //the ACK of the last data byte progressing up to the ACK of the address //byte (data bytes only for Control firmware 1.43 and less). So if n is the //number of data bytes, the ACKs value should be (2^(n+1))-1. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, ref writeACKS); expectedACKS = Math.Pow(2, numI2CBytesToWrite + 1) - 1; if (writeACKS != expectedACKS) { Console.Out.WriteLine("Expected ACKs = {0:0}, Received ACKs = %0.f\n", expectedACKS, writeACKS); } //When the GoOne processed the read request, the read data was put into the readArray buffer that //we passed, so this GetResult is also just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_READ, ref dummyDouble); //Convert fixed point values to floating point doubles. slopeDACA = BitConverter.ToInt64(readArray, 0) / (double)4294967296; offsetDACA = BitConverter.ToInt64(readArray, 8) / (double)4294967296; slopeDACB = BitConverter.ToInt64(readArray, 16) / (double)4294967296; offsetDACB = BitConverter.ToInt64(readArray, 24) / (double)4294967296; Console.Out.WriteLine("DACA Slope = {0:0.0} bits/volt\n", slopeDACA); Console.Out.WriteLine("DACA Offset = {0:0.0} bits\n", offsetDACA); Console.Out.WriteLine("DACB Slope = {0:0.0} bits/volt\n", slopeDACB); Console.Out.WriteLine("DACB Offset = {0:0.0} bits\n", offsetDACB); //Convert serial number bytes to long. serialNumber = (int)readArray[32] + ((int)readArray[33] << 8) + ((int)readArray[34] << 16) + ((int)readArray[35] << 24); Console.Out.WriteLine("Serial Number = {0:0.#}\n\n", serialNumber); //Update both DAC outputs. //Set the I2C address in the UD driver so that we not talk to the DAC chip. //The address of the DAC chip on the LJTick-DAC is 0x24. LJUD.ePut(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_ADDRESS_BYTE, 36, 0); ///Set DACA to 2.3 volts. numI2CBytesToWrite = 3; writeArray[0] = 48; //Write and update DACA. writeArray[1] = Convert.ToByte((ulong)((2.3 * slopeDACA) + offsetDACA) / 256); //Upper byte of binary DAC value. writeArray[2] = Convert.ToByte((ulong)((2.3 * slopeDACA) + offsetDACA) % 256); //Lower byte of binary DAC value. LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, numI2CBytesToWrite, writeArray, 0); LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, 0, 0, 0); //Execute the requests. LJUD.GoOne(device.ljhandle); //Get the result of the write just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, ref dummyDouble); //Get the write ACKs and compare to the expected value. We expect bit 0 to be //the ACK of the last data byte progressing up to the ACK of the address //byte (data bytes only for Control firmware 1.43 and less). So if n is the //number of data bytes, the ACKs value should be (2^(n+1))-1. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, ref writeACKS); expectedACKS = Math.Pow(2, numI2CBytesToWrite + 1) - 1; if (writeACKS != expectedACKS) { Console.Out.WriteLine("Expected ACKs = {0:0}, Received ACKs = %0.f\n", expectedACKS, writeACKS); } Console.Out.WriteLine("DACA set to 2.3 volts\n\n"); //Set DACB to 1.5 volts. numI2CBytesToWrite = 3; writeArray[0] = 49; //Write and update DACB. writeArray[1] = Convert.ToByte((ulong)((1.5 * slopeDACB) + offsetDACB) / 256); //Upper byte of binary DAC value. writeArray[2] = Convert.ToByte((ulong)((1.5 * slopeDACB) + offsetDACB) % 256); //Lower byte of binary DAC value. LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, numI2CBytesToWrite, writeArray, 0); LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, 0, 0, 0); //Execute the requests. LJUD.GoOne(device.ljhandle); //Get the result of the write just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, ref dummyDouble); //Get the write ACKs and compare to the expected value. We expect bit 0 to be //the ACK of the last data byte progressing up to the ACK of the address //byte (data bytes only for Control firmware 1.43 and less). So if n is the //number of data bytes, the ACKs value should be (2^(n+1))-1. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, ref writeACKS); expectedACKS = Math.Pow(2, numI2CBytesToWrite + 1) - 1; if (writeACKS != expectedACKS) { Console.Out.WriteLine("Expected ACKs = {0:0}, Received ACKs = %0.f\n", expectedACKS, writeACKS); } Console.Out.WriteLine("DACB set to 1.5 volts\n"); Console.ReadLine(); // Pause for user return; }
/// <summary> /// Opens the LabJack, gets the UD driver version, and /// configures the device. /// </summary> /// <param name="sender">The object that called this event</param> /// <param name="e">Event details</param> private void TimedWindow_Load(object sender, System.EventArgs e) { double dblDriverVersion; LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; // dummy variables to satisfy certian method signatures double dummyDouble = 0; int dummyInt = 0; // Create the event timer but do not start it yet updateTimer = new System.Timers.Timer(); updateTimer.Elapsed += new ElapsedEventHandler(TimerEvent); updateTimer.Interval = TIMER_INTERVAL; // Disable the start button while the device is loading goStopButton.Enabled = false; Update(); //Read and display the UD version. dblDriverVersion = LJUD.GetDriverVersion(); versionDisplay.Text = String.Format("{0:0.000}", dblDriverVersion); // Open and configure UE9 try { //Open the device ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); //Configure for 16-bit analog input measurements. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, 16, 0, 0); //Configure the analog input range on channels 2 and 3 for bipolar 5v. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 2, (double)LJUD.RANGES.BIP5V, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 3, (double)LJUD.RANGES.BIP5V, 0, 0); } catch (LabJackUDException exc) { ShowErrorMessage(exc); return; } try { //Execute the requests. LJUD.GoOne(ue9.ljhandle); //Get all the results. The input measurement results are stored. All other //results are for configuration or output requests so we are just checking //whether there was an error. The rest of the results are in the below loop. LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException exc) { ShowErrorMessage(exc); return; } bool isFinished = false; while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException exc) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (exc.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { ShowErrorMessage(exc); return; } } } // Enable the start button goStopButton.Enabled = true; }
public void performActions() { long lngGetNextIteration; LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; // Dummy variables to complete certain method signatures int dummyInt = 0; double dummyDouble = 0; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } try { //Disable all timers and counters to put everything in a known initial state. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 1, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); //First enable the quadrature input. //Enable 2 timers for phases A and B. They will use FIO0 and FIO1. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 2, 0, 0); //Configure Timer0 as quadrature. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 0, (double)LJUD.TIMERMODE.QUAD, 0, 0); //Configure Timer1 as quadrature. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 1, (double)LJUD.TIMERMODE.QUAD, 0, 0); //Execute the requests on a single LabJack. The driver will use a //single low-level TimerCounter command to handle all the requests above. LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } //Get all the results just to check for errors. try { LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } bool isFinished = false; while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } try { while (Win32Interop._kbhit() == 0) //Program will run until any key is hit { //Wait 500 milliseconds Thread.Sleep(500); //Request a read from Timer0. Timer0 and Timer1 both return the same //quadrature value. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_TIMER, 0, ref dblValue, 0); Console.Out.WriteLine("Quad Counter = {0:0.0}\n", dblValue); } //Disable the timers and the FIO lines will return to digital I/O. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } Console.ReadLine(); // Pause for user }
public void performActions() { double[] achrUserMem = new double[1024]; double[] adblCalMem = new double[128]; // Dummy variables that will be used to satisfy certain method signatures but have no other purpose double dummyDouble = 0; //Make a long parameter which holds the address of the data arrays. We do this //so the compiler does not generate a warning in the eGet call that passes //the data. Note that the x1 parameter in eGet (and AddRequest) is fairly //generic, in that sometimes it could just be a write parameter, and sometimes //it has the address of an array. Since x1 is not declared as a pointer, the //compiler will complain if you just pass the array pointer without casting //it to a long as follows. long pachrUserMem = (long)achrUserMem[0]; long padblCalMem = (long)adblCalMem[0]; //Seed the random number function. Random rand = new Random(Environment.TickCount); // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } //First a user memory example. We will read the memory, update a few elements, //and write the memory. The entire memory area is read and written each time. //The user memory is just stored as bytes, so almost any information can be //put in there such as integers, doubles, or strings. try { //Read the user memory. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.USER_MEM, ref dummyDouble, achrUserMem); //Display the first 4 elements. Console.Out.WriteLine("Read User Mem [0-3] = {0:0}, {1:0}, {2:0}, {3:0}\n", achrUserMem[0], achrUserMem[1], achrUserMem[2], achrUserMem[3]); //Create 4 new pseudo-random numbers to write. We will update the first //4 elements of user memory, but the rest will be unchanged. for (int i = 0; i < 4; i++) { achrUserMem[i] = rand.Next(100); } Console.Out.WriteLine("Write User Mem [0-3] = {0:0}, {1:0}, {2:0}, {3:0}\n", achrUserMem[0], achrUserMem[1], achrUserMem[2], achrUserMem[3]); LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.USER_MEM, 0, achrUserMem); //Re-read the user memory. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.USER_MEM, ref dummyDouble, achrUserMem); //Display the first 4 elements. Console.Out.WriteLine("Read User Mem [0-3] = {0:0}, {1:0}, {2:0}, {3:0}\n", achrUserMem[0], achrUserMem[1], achrUserMem[2], achrUserMem[3]); } catch (LabJackUDException e) { showErrorMessage(e); } //Now a cal constants example. The calibration memory is passed as doubles. //The memory area consists of 8 blocks (0-7) of 16 doubles each, for a total //of 128 elements. As of this writing, block 7 is not used, so we will //use the last 4 elements of block 7 for testing, which is elements 124-127. //We will read the constants, update a few elements, and write the constants. //The entire memory area is read and written each time. //This cal example is commented out by default, as writing and reading //the cal area is an advanced operation. /* * //Read the cal constants. * LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.CAL_CONSTANTS, ref dummyDouble, adblCalMem); * //Display the last 4 elements. * Console.Out.WriteLine("Read Cal Constants [124-127] = {0:0}, {1:0}, {2:0}, {3:0}\n",adblCalMem[124],adblCalMem[125],adblCalMem[126],adblCalMem[127]); * //Create 4 new pseudo-random numbers to write. We will update the last * //4 cal constants, but the rest will be unchanged. * for(int i=124;i<128;i++) * { * adblCalMem[i] = (100*((double)rand.Next(100)/100))-50; * } * Console.Out.WriteLine("Write Cal Constants [124-127] = {0:0}, {1:0}, {2:0}, {3:0}\n",adblCalMem[124],adblCalMem[125],adblCalMem[126],adblCalMem[127]); * //The special value (0x4C6C) must be put in to write the cal constants. * LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.CAL_CONSTANTS, 19564, adblCalMem); * //Re-read the cal constants. * LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.CAL_CONSTANTS, ref dummyDouble, adblCalMem); * //Display the first 4 elements. * Console.Out.WriteLine("Read Cal Constants [124-127] = {0:0}, {1:0}, {2:0}, {3:0}\n",adblCalMem[124],adblCalMem[125],adblCalMem[126],adblCalMem[127]); */ Console.ReadLine(); // Pause for user }
public void performActions() { long i = 0, k = 0; LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0, dblCommBacklog = 0, dblUDBacklog = 0; double scanRate = 1000; //scan rate = sample rate / #channels int delayms = 1000; double numScans = 2000; //Max number of scans per read. 2x the expected # of scans (2*scanRate*delayms/1000). double numScansRequested; double[] adblData = new double[4000]; //Max buffer size (#channels*numScansRequested) // Dummy variables to satisfy certain method signatures double dummyDouble = 0; double[] dummyDoubleArray = { 0 }; int dummyInt = 0; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } try { //Configure the stream: //Configure all analog inputs for 12-bit resolution LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, 12, 0, 0); //Configure the analog input range on channel 0 for bipolar +-5 volts. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, 0, (double)LJUD.RANGES.BIP5V, 0, 0); //Set the scan rate. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_SCAN_FREQUENCY, scanRate, 0, 0); //Give the driver a 5 second buffer (scanRate * 2 channels * 5 seconds). LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_BUFFER_SIZE, scanRate * 2 * 5, 0, 0); //Configure reads to retrieve whatever data is available without waiting (wait mode LJUD.STREAMWAITMODES.NONE). //See comments below to change this program to use LJUD.STREAMWAITMODES.SLEEP mode. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_WAIT_MODE, (double)LJUD.STREAMWAITMODES.NONE, 0, 0); //Define the scan list as AIN0 then AIN1. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.CLEAR_STREAM_CHANNELS, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 1, 0, 0, 0); //Execute the list of requests. LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } // Get results until there is no more data available for error checking bool isFinished = false; while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dummyDouble, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } //Start the stream. LJUD.eGet(ue9.ljhandle, LJUD.IO.START_STREAM, 0, ref dblValue, 0); //The actual scan rate is dependent on how the desired scan rate divides into //the LabJack clock. The actual scan rate is returned in the value parameter //from the start stream command. Console.Out.WriteLine("Actual Scan Rate = {0:0.###}\n", dblValue); Console.Out.WriteLine("Actual Sample Rate = {0:0.###}\n", 2 * dblValue); //Read data while (Win32Interop._kbhit() == 0) //Loop will run until any key is hit { //Since we are using wait mode LJUD.STREAMWAITMODES.NONE, we will wait a little, then //read however much data is available. Thus this delay will control how //fast the program loops and how much data is read each loop. An //alternative common method is to use wait mode LJUD.STREAMWAITMODES.SLEEP where the //stream read waits for a certain number of scans. In such a case //you would not have a delay here, since the stream read will actually //control how fast the program loops. // //To change this program to use sleep mode, // -change numScans to the actual number of scans desired per read, // -change wait mode addrequest value to LJUD.STREAMWAITMODES.SLEEP, // -comment out the following Thread.Sleep command. Thread.Sleep(delayms); //Remove if using LJUD.STREAMWAITMODES.SLEEP. //init array so we can easily tell if it has changed for (k = 0; k < numScans * 2; k++) { adblData[k] = 9999.0; } //Read the data. We will request twice the number we expect, to //make sure we get everything that is available. //Note that the array we pass must be sized to hold enough SAMPLES, and //the Value we pass specifies the number of SCANS to read. numScansRequested = numScans; LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_STREAM_DATA, LJUD.CHANNEL.ALL_CHANNELS, ref numScansRequested, adblData); //The displays the number of scans that were actually read. Console.Out.WriteLine("\nIteration # {0:0}\n", i); Console.Out.WriteLine("Number scans read = {0:0}\n", numScansRequested); //Display just the first scan. Console.Out.WriteLine("First scan = {0:0.###}, {1:0.###}\n", adblData[0], adblData[1]); //Retrieve the current Comm backlog. The UD driver retrieves stream data from //the UE9 in the background, but if the computer is too slow for some reason //the driver might not be able to read the data as fast as the UE9 is //acquiring it, and thus there will be data left over in the UE9 buffer. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.STREAM_BACKLOG_COMM, ref dblCommBacklog, 0); Console.Out.WriteLine("Comm Backlog = {0:0.###}\n", dblCommBacklog); //Retrieve the current UD driver backlog. If this is growing, then the application //software is not pulling data from the UD driver fast enough. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.STREAM_BACKLOG_UD, ref dblUDBacklog, 0); Console.Out.WriteLine("UD Backlog = {0:0.###}\n", dblUDBacklog); i++; } //Stop the stream LJUD.eGet(ue9.ljhandle, LJUD.IO.STOP_STREAM, 0, ref dummyDouble, dummyDoubleArray); Console.Out.WriteLine("\nDone"); Console.ReadLine(); // Pause for user }
public void performActions() { double dblValue = 0; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } ///* //Use this code if only a single EI-1050 is connected. // Connections for the probe: // Red (Power) FIO2 // Black (Ground) GND // Green (Data) FIO0 // White (Clock) FIO1 // Brown (Enable) FIO2 try { //Set the Data line to FIO0, which is the default anyway. LJUD.ePut(ue9.ljhandle, LJUD.IO.SHT_DATA_CHANNEL, 0, 0, 0); //Set the Clock line to FIO1, which is the default anyway. LJUD.ePut(ue9.ljhandle, LJUD.IO.SHT_CLOCK_CHANNEL, (LJUD.CHANNEL) 1, 0, 0); //Set FIO2 to output-high to provide power to the EI-1050s. LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL) 2, 1, 0); //Now, an add/go/get block to get the temp & humidity at the same time. //Request a temperature reading from the EI-1050. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, 0, 0, 0); //Request a humidity reading from the EI-1050. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_RH, 0, 0, 0); //Execute the requests. Will take about 0.5 seconds with a USB high-high //or Ethernet connection, and about 1.5 seconds with a normal USB connection. LJUD.GoOne(ue9.ljhandle); //Get the temperature reading. LJUD.GetResult(ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, ref dblValue); Console.Out.WriteLine("Temp Probe A = {0:0.###} deg K\n", dblValue); Console.Out.WriteLine("Temp Probe A = {0:0.###} deg C\n", (dblValue - 273.15)); Console.Out.WriteLine("Temp Probe A = {0:0.###} deg F\n", (((dblValue - 273.15) * 1.8) + 32)); //Get the humidity reading. LJUD.GetResult(ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_RH, ref dblValue); Console.Out.WriteLine("RH Probe A = {0:0.###} percent\n\n", dblValue); } catch (LabJackUDException e) { showErrorMessage(e); } //End of single probe code. //*/ /* * //Use this code if two EI-1050 probes are connected. * // Connections for both probes: * // Red (Power) FIO2 * // Black (Ground) GND * // Green (Data) FIO0 * // White (Clock) FIO1 * // * // Probe A: * // Brown (Enable) FIO3 * // * // Probe B: * // Brown (Enable) DAC0 * try * { * //Set FIO3 to output-low to disable probe A. * LJUD.ePut (ue9.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL) 3, 0, 0); * * //Set DAC0 to 0 volts to disable probe B. * LJUD.ePut (ue9.ljhandle, LJUD.IO.PUT_DAC, (LJUD.CHANNEL) 0, 0.0, 0); * * //Set FIO3 to output-high to enable probe A. * LJUD.ePut (ue9.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL) 3, 1, 0); * * //Now, an add/go/get block to get the temp & humidity at the same time. * //Request a temperature reading from the EI-1050. * LJUD.AddRequest (ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, 0, 0, 0); * * //Request a humidity reading from the EI-1050. * LJUD.AddRequest (ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_RH, 0, 0, 0); * * //Execute the requests. Will take about 0.5 seconds with a USB high-high * //or Ethernet connection, and about 1.5 seconds with a normal USB connection. * LJUD.GoOne (ue9.ljhandle); * * //Get the temperature reading. * LJUD.GetResult (ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, ref dblValue); * Console.Out.WriteLine("Temp Probe A = {0:0.###} deg K\n",dblValue); * Console.Out.WriteLine("Temp Probe A = {0:0.###} deg C\n",(dblValue-273.15)); * Console.Out.WriteLine("Temp Probe A = {0:0.###} deg F\n",(((dblValue-273.15)*1.8)+32)); * * //Get the humidity reading. * LJUD.GetResult (ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_RH, ref dblValue); * Console.Out.WriteLine("RH Probe A = {0:0.###} percent\n\n",dblValue); * * //Set FIO3 to output-low to disable probe A. * LJUD.ePut (ue9.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL) 3, 0, 0); * * //Set DAC0 to 3.3 volts to enable probe B. * LJUD.ePut (ue9.ljhandle, LJUD.IO.PUT_DAC, 0, 3.3, 0); * * //Now, an add/go/get block to get the temp & humidity at the same time. * //Request a temperature reading from the EI-1050. * LJUD.AddRequest (ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, 0, 0, 0); * * //Request a humidity reading from the EI-1050. * LJUD.AddRequest (ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_RH, 0, 0, 0); * * //Execute the requests. Will take about 0.5 seconds with a USB high-high * //or Ethernet connection, and about 1.5 seconds with a normal USB connection. * LJUD.GoOne (ue9.ljhandle); * * //Get the temperature reading. * LJUD.GetResult (ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, ref dblValue); * Console.Out.WriteLine("Temp Probe B = {0:0.###} deg K\n",dblValue); * Console.Out.WriteLine("Temp Probe B = {0:0.###} deg C\n",(dblValue-273.15)); * Console.Out.WriteLine("Temp Probe B = {0:0.###} deg F\n",(((dblValue-273.15)*1.8)+32)); * * //Get the humidity reading. * LJUD.GetResult (ue9.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_RH, ref dblValue); * Console.Out.WriteLine("RH Probe B = {0:0.###} percent\n\n",dblValue); * * //Set DAC0 to 0 volts to disable probe B. * LJUD.ePut (ue9.ljhandle, LJUD.IO.PUT_DAC, 0, 0.0, 0); * } * catch (LabJackUDException e) * { * showErrorMessage(e); * } * * //End of dual probe code. */ Console.ReadLine(); // Pause for user }
public void performActions() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double highTime, lowTime; double period16 = -1, period32 = -1; // Variables that satisfy a method signature int dummyInt = 0; double dummyDouble = 0; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } //Disable all timers and counters to put everything in a known initial state. //Disable the timer and counter, and the FIO lines will return to digital I/O. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 1, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); //First we will output a square wave and count the number of pulses for about 1 second. //Connect a jumper on the UE9 from FIO0 (PWM output) to //FIO1 (Counter0 input). //Use the fixed 750kHz timer clock source. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.KHZ750, 0, 0); //Set the divisor to 3 so the actual timer clock is 250kHz. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_DIVISOR, 3, 0, 0); //Enable 1 timer. It will use FIO0. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 1, 0, 0); //Configure Timer0 as 8-bit PWM. Frequency will be 250k/256 = 977 Hz. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 0, (double)LJUD.TIMERMODE.PWM8, 0, 0); //Set the PWM duty cycle to 50%. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 32768, 0, 0); //Enable Counter0. It will use FIO1 since 1 timer is enabled. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 1, 0, 0); //Execute the requests on a single LabJack. The driver will use a //single low-level TimerCounter command to handle all the requests above. LJUD.GoOne(ue9.ljhandle); //Get all the results just to check for errors. try { LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } bool isFinished = false; while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } try { //Wait 1 second. Thread.Sleep(1000); //Request a read from the counter. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_COUNTER, 0, ref dblValue, 0); //This should read roughly 977 counts. Console.Out.WriteLine("Counter = {0:0.0}\n\n", dblValue); //Disable the timer and counter, and the FIO lines will return to digital I/O. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); //Output a square wave and measure the period. //Connect a jumper on the UE9 from FIO0 (PWM8 output) to //FIO1 (RISINGEDGES32 input) and FIO2 (RISINGEDGES16). //Use the fixed 750kHz timer clock source. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.KHZ750, 0, 0); //Set the divisor to 3 so the actual timer clock is 250kHz. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_DIVISOR, 3, 0, 0); //Enable 3 timers. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 3, 0, 0); //Configure Timer0 as 8-bit PWM. Frequency will be 250k/256 = 977 Hz. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 0, (double)LJUD.TIMERMODE.PWM8, 0, 0); //Set the PWM duty cycle to 50%. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 32768, 0, 0); //Configure Timer1 as 32-bit period measurement. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 1, (double)LJUD.TIMERMODE.RISINGEDGES32, 0, 0); //Configure Timer2 as 16-bit period measurement. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 2, (double)LJUD.TIMERMODE.RISINGEDGES16, 0, 0); //Execute the requests on a single LabJack. The driver will use a //single low-level TimerCounter command to handle all the requests above. LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } //Get all the results just to check for errors. try { LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } isFinished = false; while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } //Wait 1 second. Thread.Sleep(1000); //Now read the period measurements from the 2 timers. We //will use the Add/Go/Get method so that both //reads are done in a single low-level call. //Request a read from Timer1 LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_TIMER, 1, 0, 0, 0); //Request a read from Timer2 LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_TIMER, 2, 0, 0, 0); //Execute the requests on a single LabJack. The driver will use a //single low-level TimerCounter command to handle all the requests above. LJUD.GoOne(ue9.ljhandle); //Get the results of the two read requests. LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); isFinished = false; while (!isFinished) { switch (ioType) { case LJUD.IO.GET_TIMER: switch ((int)channel) { case 1: period32 = dblValue; break; case 2: period16 = dblValue; break; } break; } try{ LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // After encountering a if (e.LJUDError > UE9.LJUDERROR.MIN_GROUP_ERROR) { isFinished = true; } else { showErrorMessage(e); } } } try { //Both period measurements should read about 256. The timer //clock was set to 250 kHz, so each tick equals 4 microseconds, so //256 ticks means a period of 1024 microseconds which is a frequency //of 977 Hz. Console.Out.WriteLine("Period32 = {0:0.0}\n", period32); Console.Out.WriteLine("Period16 = {0:0.0}\n\n", period16); //Disable the timer and counter, and the FIO lines will return to digital I/O. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); //Now we will output a 25% duty-cycle PWM output on Timer0 (FIO0) and measure //the duty cycle on Timer1 FIO1. Requires Control firmware V1.21 or higher. //Use the fixed 750kHz timer clock source. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.KHZ750, 0, 0); //Set the divisor to 3 so the actual timer clock is 250kHz. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_DIVISOR, 3, 0, 0); //Enable 2 timers. They will use FIO0 and FIO1. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 2, 0, 0); //Configure Timer0 as 8-bit PWM. Frequency will be 250k/256 = 977 Hz. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 0, (double)LJUD.TIMERMODE.PWM8, 0, 0); //Set the PWM duty cycle to 25%. The passed value is the low time. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 49152, 0, 0); //Configure Timer1 as duty cycle measurement. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_TIMER_MODE, 1, (double)LJUD.TIMERMODE.DUTYCYCLE, 0, 0); //Execute the requests on a single LabJack. The driver will use a //single low-level TimerCounter command to handle all the requests above. LJUD.GoOne(ue9.ljhandle); } catch (LabJackUDException e) { // After encountering a if (e.LJUDError > UE9.LJUDERROR.MIN_GROUP_ERROR) { isFinished = true; } else { showErrorMessage(e); } } //Get all the results just to check for errors. try { LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } isFinished = false; while (!isFinished) { try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } //Wait a little so we are sure a duty cycle measurement has occured. Thread.Sleep(100); try { //Request a read from Timer1. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_TIMER, (LJUD.CHANNEL) 1, ref dblValue, 0); //High time is LSW highTime = (double)(((ulong)dblValue) % (65536)); //Low time is MSW lowTime = (double)(((ulong)dblValue) / (65536)); Console.Out.WriteLine("High clicks = {0:0.0}\n", highTime); Console.Out.WriteLine("Low clicks = {0:0.0}\n", lowTime); Console.Out.WriteLine("Duty cycle = {0:0.0}\n", 100 * highTime / (highTime + lowTime)); //Disable the timers, and the FIO lines will return to digital I/O. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 0, 0, 0); LJUD.GoOne(ue9.ljhandle); //The PWM output sets FIO0 to output, so we do a read here to set //FIO0 to input. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_DIGITAL_BIT, 0, ref dblValue, 0); } catch (LabJackUDException e) { // After encountering a if (e.LJUDError > UE9.LJUDERROR.MIN_GROUP_ERROR) { isFinished = true; } else { showErrorMessage(e); } } Console.ReadLine(); // Pause for user }
public void performActions() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double Value2 = 0, Value3 = 0; double ValueDIBit = 0, ValueDIPort = 0, ValueCounter = 0; // dummy variables to satisfy certian method signatures double dummyDouble = 0; int dummyInt = 0; // Open UE9 try { ue9 = new UE9(LJUD.CONNECTION.USB, "0", true); // Connection through USB //ue9 = new UE9(LJUD.CONNECTION.ETHERNET, "192.168.1.50", true); // Connection through ethernet } catch (LabJackUDException e) { showErrorMessage(e); } try { //First some configuration commands. These will be done with the ePut //function which combines the add/go/get into a single call. //Configure for 16-bit analog input measurements. LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, 16, 0); //Configure the analog input range on channels 2 and 3 for bipolar gain=1. LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 2, (double)LJUD.RANGES.BIP5V, 0); LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 3, (double)LJUD.RANGES.BIP5V, 0); //Enable Counter0 which will appear on FIO0 (assuming no other //program has enabled any timers or Counter1). LJUD.ePut(ue9.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 1, 0); //Now we add requests to write and read I/O. These requests //will be processed repeatedly by go/get statements in every //iteration of the while loop below. //Request AIN2 and AIN3. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_AIN, 2, 0, 0, 0); LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_AIN, 3, 0, 0, 0); //Set DAC0 to 2.5 volts. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_DAC, 0, 2.5, 0, 0); //Read digital input FIO1. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_DIGITAL_BIT, 1, 0, 0, 0); //Set digital output FIO2 to output-high. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, 2, 1, 0, 0); //Read digital inputs FIO3 through FIO7. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_DIGITAL_PORT, 3, 0, 5, 0); //Request the value of Counter0. LJUD.AddRequest(ue9.ljhandle, LJUD.IO.GET_COUNTER, 0, 0, 0, 0); } catch (LabJackUDException e) { showErrorMessage(e); } bool requestedExit = false; while (!requestedExit) { try { //Execute the requests. LJUD.GoOne(ue9.ljhandle); //Get all the results. The input measurement results are stored. All other //results are for configuration or output requests so we are just checking //whether there was an error. LJUD.GetFirstResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } bool isFinished = false; while (!isFinished) { switch (ioType) { case LJUD.IO.GET_AIN: switch ((int)channel) { case 2: Value2 = dblValue; break; case 3: Value3 = dblValue; break; } break; case LJUD.IO.GET_DIGITAL_BIT: ValueDIBit = dblValue; break; case LJUD.IO.GET_DIGITAL_PORT: ValueDIPort = dblValue; break; case LJUD.IO.GET_COUNTER: ValueCounter = dblValue; break; } try { LJUD.GetNextResult(ue9.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { // If we get an error, report it. If the error is NO_MORE_DATA_AVAILABLE we are done if (e.LJUDError == UE9.LJUDERROR.NO_MORE_DATA_AVAILABLE) { isFinished = true; } else { showErrorMessage(e); } } } // Output the results Console.Out.WriteLine("AIN2 = {0:0.00000}\n", Value2); Console.Out.WriteLine("AIN3 = {0:0.00000}\n", Value3); Console.Out.WriteLine("FIO1 = {0:0.00000}\n", ValueDIBit); Console.Out.WriteLine("FIO3-FIO7 = {0:0.00000}\n", ValueDIPort); //Will read 31 if all 5 lines are pulled-high as normal. Console.Out.WriteLine("Counter0 (FIO0) = {0:0.00000}\n", ValueCounter); Console.Out.WriteLine("\nPress Enter to go again or (q) to quit\n"); requestedExit = Console.ReadLine().Equals("q"); } }