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 }
/// <summary> /// Configure and start the stream on the LabJack /// </summary> /// <returns>True if successful and false otherwise</returns> private bool StartStreaming() { try { //Start by using the pin_configuration_reset IOType so that all //pin assignments are in the factory default condition. LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); //Configure FIO0 and FIO1 as analog, all else as digital. That means we //will start from channel 0 and update all 16 flexible bits. We will //pass a value of b0000000000000011 or d3. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_ANALOG_ENABLE_PORT, 0, metingInfo.instellingAnalogeKanalen, 16); //Configure the stream: //Set the scan rate. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_SCAN_FREQUENCY, metingInfo.sampleFrequentie, 0, 0); //Give the driver a 5 second buffer (metingInfo.sampleFrequentie * 2 channels * 5 seconds). LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_BUFFER_SIZE, metingInfo.sampleFrequentie * metingInfo.aantalGeselecteerdeKanalen * 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(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.STREAM_WAIT_MODE, (double)LJUD.STREAMWAITMODES.NONE, 0, 0); ///Toevoegen van channels LJUD.AddRequest(u3.ljhandle, LJUD.IO.CLEAR_STREAM_CHANNELS, 0, 0, 0, 0); #region KANALEN_INSTELLEN for (int i = 0; i < aantalKanalen; i++) { //De analoge kanalen instellen if (metingInfo.blIsHetAnalogeKanaalGeselecteerd[i] && !expertInfo.cbxDigitaal[i] && !expertInfo.cbxDifferentiaal[i]) //alleen analoog als niet (digitaal & diff) { LJUD.AddRequest(u3.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, i, 0, 0, 0); } if (expertSettingsToolStripMenuItem.Checked) { //differentieel kanalen if (expertInfo.cbxDifferentiaal[i]) { LJUD.AddRequest(u3.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL_DIFF, i, 0, expertInfo.diffChannel[i], 0); } ////digitaal kanalen instellen (sowieso als expert settings is ingesteld) //LJUD.AddRequest(u3.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 193, 0, 0, 0); //193, digitaal } } //De digitale kanalen instellen LJUD.AddRequest(u3.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 193, 0, 0, 0); #endregion //Execute the list of requests. LJUD.GoOne(u3.ljhandle); } catch (LabJackUDException e) { ShowErrorMessage(e); return(false); } //Get all the results just to check for errors. try { LJUD.GetFirstResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { ShowErrorMessage(e); } bool finished = false; while (!finished) { try { LJUD.GetNextResult(u3.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 == U3.LJUDERROR.NO_MORE_DATA_AVAILABLE) { finished = true; } else { ShowErrorMessage(e); } } } //Start the stream. try { LJUD.eGet(u3.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. //tstbSampleFrequentie.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() { 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 }
/// <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 U3. u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB //Read and display the hardware version of this U3. LJUD.eGet(u3.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 U3. LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.FIRMWARE_VERSION, ref dblValue, 0); firmwareDisplay.Text = String.Format("{0:0.000}", dblValue); //Start by using the pin_configuration_reset IOType so that all //pin assignments are in the factory default condition. LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); //Configure FIO0 and FIO1 as analog, all else as digital. That means we //will start from channel 0 and update all 16 flexible bits. We will //pass a value of b0000000000000011 or d3. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_ANALOG_ENABLE_PORT, 0, 3, 16); //Configure the stream: //Set the scan rate. LJUD.AddRequest(u3.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(u3.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(u3.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(u3.ljhandle, LJUD.IO.CLEAR_STREAM_CHANNELS, 0, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 0, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL_DIFF, 1, 0, 32, 0); //Execute the list of requests. LJUD.GoOne(u3.ljhandle); } catch (LabJackUDException e) { ShowErrorMessage(e); return(false); } //Get all the results just to check for errors. try { LJUD.GetFirstResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { ShowErrorMessage(e); } bool finished = false; while (!finished) { try { LJUD.GetNextResult(u3.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(u3.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); }
/// <summary> /// Actually performs actions on the U6 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 U6 try { u6 = new U6(LJUD.CONNECTION.USB, "0", true); } 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 the resolution of the analog inputs (pass a non-zero value for quick sampling). //See section 2.6 / 3.1 for more information. LJUD.ePut(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, 0, 0); //Configure the analog input range on channels 2 and 3 for bipolar 10v. LJUD.ePut(u6.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 2, (double)LJUD.RANGES.BIP10V, 0); LJUD.ePut(u6.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 3, (double)LJUD.RANGES.BIP10V, 0); //Enable Counter0 which will appear on FIO0 (assuming no other //program has enabled any timers or Counter1). LJUD.ePut(u6.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(u6.ljhandle, LJUD.IO.GET_AIN, 2, 0, 0, 0); LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_AIN, 3, 0, 0, 0); //Set DAC0 to 2.5 volts. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_DAC, 0, 2.5, 0, 0); //Read digital input FIO1. LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_DIGITAL_BIT, 1, 0, 0, 0); //Read digital inputs FIO2 through FIO3. LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_DIGITAL_PORT, 2, 0, 2, 0); // LJUD.AddRequest (u6.ljhandle, LJUD.IO.GET_DIGITAL_PORT, 2, 0, 3, 0); would request through FIO4 //Request the value of Counter0. LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_COUNTER, 0, 0, 0, 0); } catch (LabJackUDException exc) { ShowErrorMessage(exc); return; } try { //Execute the requests. LJUD.GoOne(u6.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(u6.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(u6.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 == U6.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 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 U6(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.4 volts. numI2CBytesToWrite = 3; writeArray[0] = 48; //Write and update DACA. writeArray[1] = Convert.ToByte((ulong)((2.4 * slopeDACA) + offsetDACA) / 256); //Upper byte of binary DAC value. writeArray[2] = Convert.ToByte((ulong)((2.4 * 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.4 volts\n\n"); //Set DACB to 0.5 volts. numI2CBytesToWrite = 3; writeArray[0] = 49; //Write and update DACB. writeArray[1] = Convert.ToByte((ulong)((0.5 * slopeDACB) + offsetDACB) / 256); //Upper byte of binary DAC value. writeArray[2] = Convert.ToByte((ulong)((0.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 0.5 volts\n"); Console.ReadLine(); // Pause for user return; }
public void performActions() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; // Variables to satisfy certain method signatures int dummyInt = 0; double dummyDouble = 0; double[] dummyDoubleArray = { 0 }; //Open the Labjack with id 2 try { u6 = new U6(LJUD.CONNECTION.USB, "1", true); // Connection through USB //First requests to configure the timer and counter. These will be //done with and add/go/get block. //Set the timer/counter pin offset to 0, which will put the first //timer/counter on FIO0. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_COUNTER_PIN_OFFSET, 0, 0, 0); //Use the 48 MHz timer clock base with divider. Since we are using clock with divisor //support, Counter0 is not available. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.MHZ48_DIV, 0, 0); //Set the divisor to 48 so the actual timer clock is 1 MHz. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_DIVISOR, 48, 0, 0); //Enable 1 timer. It will use FIO0. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 1, 0, 0); //Make sure Counter0 is disabled. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 0, 0, 0); //Enable Counter1. It will use FIO1 since 1 timer is enabled. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 1, 1, 0, 0); //Configure Timer0 as 8-bit PWM. Frequency will be 1M/256 = 3906 Hz. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_TIMER_MODE, 0, (double)LJUD.TIMERMODE.PWM8, 0, 0); //Set the PWM duty cycle to 50%. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 32768, 0, 0); //Execute the requests. LJUD.GoOne(u6.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } //Get all the results just to check for errors. try { LJUD.GetFirstResult(u6.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } bool finished = false; while (!finished) { try{ LJUD.GetNextResult(u6.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); } } } try { //Wait 1 second. Thread.Sleep(1000); //Request a read from the counter. LJUD.eGet(u6.ljhandle, LJUD.IO.GET_COUNTER, (LJUD.CHANNEL) 1, ref dblValue, dummyDoubleArray); //This should read roughly 4k counts if FIO0 is shorted to FIO1. Console.Out.WriteLine("Counter = {0:0.0}\n", dblValue); //Wait 1 second. Thread.Sleep(1000); //Request a read from the counter. LJUD.eGet(u6.ljhandle, LJUD.IO.GET_COUNTER, (LJUD.CHANNEL) 1, ref dblValue, dummyDoubleArray); //This should read about 3906 counts more than the previous read. Console.Out.WriteLine("Counter = {0:0.0}\n", dblValue); //Reset all pin assignments to factory default condition. LJUD.ePut(u6.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); //The PWM output sets FIO0 to output, so we do a read here to set //it to input. LJUD.eGet(u6.ljhandle, LJUD.IO.GET_DIGITAL_BIT, 0, ref dblValue, 0); } catch (LabJackUDException e) { if (e.LJUDError == LJUD.LJUDERROR.NO_MORE_DATA_AVAILABLE) { finished = true; } else { showErrorMessage(e); } } Console.ReadLine(); // Pause for user }
public void performActions() { double dblDriverVersion; LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double Value0 = 9999, Value1 = 9999, Value2 = 9999; double ValueDIBit = 9999, ValueDIPort = 9999, ValueCounter = 9999; //Read and display the UD version. dblDriverVersion = LJUD.GetDriverVersion(); Console.Out.WriteLine("UD Driver Version = {0:0.000}\n\n", dblDriverVersion); //Open the first found LabJack U6. try { u6 = new U6(LJUD.CONNECTION.USB, "0", true); // Connection through USB } catch (LabJackUDException e) { showErrorMessage(e); } //First some configuration commands. These will be done with the ePut //function which combines the add/go/get into a single call. //Set the timer/counter pin offset to 3, which will put the first //timer/counter on FIO3. LJUD.ePut(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_COUNTER_PIN_OFFSET, 3, 0); //Enable Counter1 (FIO3). LJUD.ePut(u6.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, (LJUD.CHANNEL) 1, 1, 0); //The following commands will use the add-go-get method to group //multiple requests into a single low-level function. //Request a single-ended reading from AIN0. LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_AIN, 0, 0, 0, 0); //Request a single-ended reading from AIN1. LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_AIN, 1, 0, 0, 0); //Request a reading from AIN2 using the Special range. LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_AIN_DIFF, 2, 0, 15, 0); //Set DAC0 to 3.5 volts. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_DAC, 0, 3.5, 0, 0); //Set digital output FIO0 to output-high. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, 0, 1, 0, 0); //Read digital input FIO1. LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_DIGITAL_BIT, 1, 0, 0, 0); //Read digital inputs FIO1 through FIO2. LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_DIGITAL_PORT, 1, 0, 2, 0); //Request the value of Counter1 (FIO3). LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_COUNTER, 1, 0, 0, 0); bool requestedExit = false; while (!requestedExit) { try { //Execute the requests. LJUD.GoOne(u6.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(u6.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } bool finished = false; while (!finished) { switch (ioType) { case LJUD.IO.GET_AIN: switch ((int)channel) { case 0: Value0 = dblValue; break; case 1: Value1 = dblValue; break; } break; case LJUD.IO.GET_AIN_DIFF: Value2 = dblValue; 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(u6.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 == U6.LJUDERROR.NO_MORE_DATA_AVAILABLE) { finished = true; } else { showErrorMessage(e); } } } Console.Out.WriteLine("AIN0 = {0:0.###}\n", Value0); Console.Out.WriteLine("AIN1 = {0:0.###}\n", Value1); Console.Out.WriteLine("AIN2 = {0:0.###}\n", Value2); Console.Out.WriteLine("FIO1 = {0:0.###}\n", ValueDIBit); Console.Out.WriteLine("FIO1-FIO2 = {0:0.###}\n", ValueDIPort); //Will read 3 (binary 11) if both lines are pulled-high as normal. Console.Out.WriteLine("Counter1 (FIO3) = {0:0.###}\n", ValueCounter); Console.Out.WriteLine("\nPress Enter to go again or (q) to quit\n"); str1 = Console.ReadLine(); // Pause for user requestedExit = str1 == "q"; } }
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 = 2000; int delayms = 1000; double numScans = 4000; //2x the expected # of scans (2*scanRate*delayms/1000) double numScansRequested; double[] adblData = new double[8000]; //Max buffer size (#channels*numScansRequested) // Variables to satisfy certain method signatures int dummyInt = 0; double dummyDouble = 0; double[] dummyDoubleArray = { 0 }; //Read and display the UD version. dblValue = LJUD.GetDriverVersion(); Console.Out.WriteLine("UD Driver Version = {0:0.000}\n\n", dblValue); try { //Open the first found LabJack U3. u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB //Read and display the hardware version of this U3. LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.HARDWARE_VERSION, ref dblValue, 0); Console.Out.WriteLine("U3 Hardware Version = {0:0.000}\n\n", dblValue); //Read and display the firmware version of this U3. LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.FIRMWARE_VERSION, ref dblValue, 0); Console.Out.WriteLine("U3 Firmware Version = {0:0.000}\n\n", dblValue); //Start by using the pin_configuration_reset IOType so that all //pin assignments are in the factory default condition. LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); //Configure FIO0 and FIO1 as analog, all else as digital. That means we //will start from channel 0 and update all 16 flexible bits. We will //pass a value of b0000000000000011 or d3. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_ANALOG_ENABLE_PORT, 0, 3, 16); //Configure the stream: //Set the scan rate. LJUD.AddRequest(u3.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(u3.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(u3.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(u3.ljhandle, LJUD.IO.CLEAR_STREAM_CHANNELS, 0, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 0, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL_DIFF, 1, 0, 32, 0); //Execute the list of requests. LJUD.GoOne(u3.ljhandle); } catch (LabJackUDException e) { showErrorMessage(e); } //Get all the results just to check for errors. try { LJUD.GetFirstResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } bool finished = false; while (!finished) { try { LJUD.GetNextResult(u3.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(u3.ljhandle, LJUD.IO.START_STREAM, 0, ref dblValue, 0); } catch (LabJackUDException e) { showErrorMessage(e); } //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.000}\n", dblValue); Console.Out.WriteLine("Actual Sample Rate = {0:0.000}\n", 2 * dblValue); // # channels * scan rate //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; } try { //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(u3.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 read = {0:0}\n", numScansRequested); //This displays just the first scan. Console.Out.WriteLine("First scan = {0:0.000}, {1:0.000}\n", adblData[0], adblData[1]); //Retrieve the current backlog. The UD driver retrieves stream data from //the U3 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 U3 is //acquiring it, and thus there will be data left over in the U3 buffer. LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.STREAM_BACKLOG_COMM, ref dblCommBacklog, 0); Console.Out.WriteLine("Comm Backlog = {0:0}\n", dblCommBacklog); LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.STREAM_BACKLOG_UD, ref dblUDBacklog, 0); Console.Out.WriteLine("UD Backlog = {0:0}\n", dblUDBacklog); i++; } catch (LabJackUDException e) { showErrorMessage(e); } } //Stop the stream try{ LJUD.eGet(u3.ljhandle, LJUD.IO.STOP_STREAM, 0, ref dummyDouble, dummyDoubleArray); } catch (LabJackUDException e) { showErrorMessage(e); } Console.Out.WriteLine("\nDone"); Console.ReadLine(); // Pause for user }