/// <summary> /// performs read of stream data on LabJack device /// </summary> /// <param name="source">The object that called the event</param> /// <param name="e">Event details</param> private void MakeReadings(object source, ElapsedEventArgs e) { //init array so we can easily tell if it has changed for (int i = 0; i < numScans * 2; i++) { adblData[i] = 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(ue9.ljhandle, LJUD.IO.GET_STREAM_DATA, LJUD.CHANNEL.ALL_CHANNELS, ref numScansRequested, adblData); ShowReadings(adblData); //Retrieve the current 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. //Likewise, if this program can not retrieve data quickly enough, the UD driver's //own backlog will fill. LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.STREAM_BACKLOG_COMM, ref dblCommBacklog, 0); LJUD.eGet(ue9.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.STREAM_BACKLOG_UD, ref dblUDBacklog, 0); DisplayBacklog(dblUDBacklog, dblCommBacklog); } catch (LabJackUDException exc) { ShowErrorMessage(exc); return; } }
private void tmrPollLJ_Tick(object sender, EventArgs e) { // Read from each bicycle. Note that we want to do this in a single "GoOne" call for each labjack, otherwise things will // be much slower, since there'll be more USB requests. Because of this, we operate over each labjack individually, grouping // by handle. foreach (int thisLJHnd in bicycles.Select(x => x.labjackHandle)) { // Get bikes on this labjack. Note that we .ToArray because we must ensure ordering does not change before we .GetResult // later on. bicycle[] bikesToPoll = bicycles.Where(x => x.labjackHandle == thisLJHnd).ToArray(); // and add a request for each foreach (bicycle thisBike in bikesToPoll) { // Specify negative channel as 32, which (on the U3) will select the special 0-3.6v range LJUD.AddRequest(thisBike.labjackHandle, LJUD.IO.GET_AIN_DIFF, thisBike.FIOChannel, 0, 32, 0); } // Now we can poll this LJ LJUD.GoOne(thisLJHnd); // and get our results, which are in the same order as the bikesToPoll array. foreach (bicycle thisBike in bikesToPoll) { double newVal = 0; LJUD.GetResult(thisBike.labjackHandle, LJUD.IO.GET_AIN_DIFF, thisBike.FIOChannel, ref newVal); thisBike.onRawData(newVal); } } }
[System.Diagnostics.DebuggerNonUserCode] //This is used to supress Exception messages from the LabJack library. private void GetResults() { LJUD.IO ioType = 0; double dblValue = 0; LJUD.CHANNEL channel = 0; int dummyInt = 0; double dummyDouble = 0; bool finished = false; try { LJUD.GetFirstResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { ShowErrorMessage(e); } while (!finished) { HandleResult(ioType, channel, dblValue); 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); } } } }
//Function used to read and display the duty cycle from a timer. private void ReadDutyCycle(int handle, long timerNumber) { double dblValue = 0; double highTime, lowTime, dutyCycle; double dummyDouble = 0; // Satisfies method signatures but does not have any real purpose //Read from Timer. We will go ahead and reset the timer (by writing //a value of 0) at the same time as the read. This way if no new //edges occur (e.g. 0% or 100% duty cycle) the next read will return //the preset high/low times (0/65535 or 65535/0) rather than returning //the old values. LJUD.AddRequest(handle, LJUD.IO.GET_TIMER, (LJUD.CHANNEL)timerNumber, 0, 0, 0); LJUD.AddRequest(handle, LJUD.IO.PUT_TIMER_VALUE, (LJUD.CHANNEL)timerNumber, 0, 0, 0); LJUD.GoOne(handle); LJUD.GetResult(handle, LJUD.IO.PUT_TIMER_VALUE, (LJUD.CHANNEL)timerNumber, ref dummyDouble); //just to check for error LJUD.GetResult(handle, LJUD.IO.GET_TIMER, (LJUD.CHANNEL)timerNumber, ref dblValue); //High time is LSW highTime = (double)(((ulong)dblValue) % (65536)); //Low time is MSW lowTime = (double)(((ulong)dblValue) / (65536)); //Calculate the duty cycle percentage. dutyCycle = 100 * highTime / (highTime + lowTime); Console.Out.WriteLine("\nHigh clicks Timer{0:0.#} = {1:0.#}", timerNumber, highTime); Console.Out.WriteLine("Low clicks Timer{0:0.#} = {1:0.#}", timerNumber, lowTime); Console.Out.WriteLine("Duty cycle Timer{0:0.#} = {1:0.#}", timerNumber, dutyCycle); }
private void Initialize() { double value = 0; evth.LogMessage(this, new LogEventArgs(LogLevel.INFO, "Initializing.")); try { //Open the first found LabJack U3 through USB. u3 = new U3(LJUD.CONNECTION.USB, "0", true); isValid = true; } catch (Exception ex) { evth.LogMessage(this, new LogEventArgs(LogLevel.ERROR, "Initialize exception: " + ex.Message)); return; } value = LJUD.GetDriverVersion(); evth.LogMessage(this, new LogEventArgs(LogLevel.INFO, "UD Driver Version: " + value.ToString())); LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.HARDWARE_VERSION, ref value, 0); evth.LogMessage(this, new LogEventArgs(LogLevel.INFO, "Hardware Version: " + value)); LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.FIRMWARE_VERSION, ref value, 0); evth.LogMessage(this, new LogEventArgs(LogLevel.INFO, "Firmware Version: " + value)); evth.LogMessage(this, new LogEventArgs(LogLevel.INFO, "Initialized.")); Execute(LJU3Commands.PIN_CONFIGURATION_RESET, new object[] { }); //pin assignments in factory default condition. Execute(LJU3Commands.PUT_ANALOG_ENABLE_PORT, new object[] { 0, 0, 16 }); //all assignments digital input. }
private void HandleCommand(LJU3Commands command, object[] args) { LogLevel loglevel = LogLevel.INFO; switch (command) { case LJU3Commands.GET_CONFIG: double value = 0; LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, (LJUD.CHANNEL)args[0], ref value, 0); break; case LJU3Commands.PIN_CONFIGURATION_RESET: LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); break; case LJU3Commands.PUT_ANALOG_ENABLE_PORT: LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_ANALOG_ENABLE_PORT, (LJUD.CHANNEL)args[0], Convert.ToDouble(args[1]), (int)args[2]); break; case LJU3Commands.GET_DIGITAL_BIT: LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_DIGITAL_BIT, (LJUD.CHANNEL)args[0], Convert.ToDouble(args[1]), (int)args[2], Convert.ToDouble(args[3])); loglevel = LogLevel.RAW; break; case LJU3Commands.PUT_DIGITAL_BIT: LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL)args[0], Convert.ToDouble(args[1]), 0, 0); break; default: evth.LogMessage(this, new LogEventArgs(LogLevel.ERROR, command.ToString() + ": Unhandled Command.")); break; } evth.LogMessage(this, new LogEventArgs(loglevel, "Command executed: " + command.ToString())); }
public void postXMLDeserialisation() { // Comment this out if you have a real LabJack return; if (LJDeviceHandlesBySerial.ContainsKey(labjackSerial)) { labjackHandle = LJDeviceHandlesBySerial[labjackSerial]; } else { // This labjack hasn't been opened yet, so lets do that. LJUD.OpenLabJack(LJUD.DEVICE.U3, LJUD.CONNECTION.USB, labjackSerial, false, ref labjackHandle); LJDeviceHandlesBySerial[labjackSerial] = labjackHandle; // Reset the config, in case another application has messed with it LJUD.AddRequest(labjackHandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0, 0); // Set 12-bit sampling resolution LJUD.AddRequest(labjackHandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, 12, 0, 0); } // Configure FIO channel for this bicycle as analogue input. LJUD.AddRequest(labjackHandle, LJUD.IO.PUT_ANALOG_ENABLE_BIT, FIOChannel, 1, 0, 0); }
private void connectToLabjack() { int teller = 0; //Maak verbinding met de Labjack tsslbl_Status.Text = " Verbonden met de Labjack "; try { while (u3 == null && teller++ < 1000000) { u3 = new U3(LJUD.CONNECTION.USB, "0", true); //Probeer direct weer te verbinden } } catch (LabJackUDException e) //Reset en probeer opnieuw { ShowErrorMessage(e); try { resetLabjack(); while (u3 == null && teller++ < 1000000) { u3 = new U3(LJUD.CONNECTION.USB, "0", true); //Probeer direct weer te verbinden } } catch (Exception) { tsslbl_Status.Text = "Geen Labjack aangesloten "; } } if (u3 != null) //test voor labjack aanwezigheid { //Read and display the hardware version of this U3. LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.SERIAL_NUMBER, ref dblValue, 0); serienummerToolStripMenuItem1.Text = "Serienummer: " + String.Format("{0:0}", dblValue); //Read and display the hardware version of this U3. LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.HARDWARE_VERSION, ref dblValue, 0); hardwareVersieToolStripMenuItem.Text = "Hardware versie: " + 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); firmwareVersieToolStripMenuItem.Text = "Firmware versie:" + String.Format("{0:0.000}", dblValue); //Hv or Lv LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.U3HV, ref dblValue, 0); if (dblValue >= 1.0) { tsmi_LabjackHV.Text = "U3-HV (High Voltage, 0-10 V)"; } else { tsmi_LabjackHV.Text = "U3-LV (Low Voltage)"; blLabjackHV = false; } } }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { try { LJUD.ePut(u3.ljhandle, LJUD.IO.STOP_STREAM, 0, 0, 0); LJUD.Close(); } catch (LabJackUDException) { } }
/// <summary> /// Read from the stream /// </summary> private void MakeReadings() { while (streamRunning) { //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(metingInfo.delayms); //Remove if using LJUD.STREAMWAITMODES.SLEEP. //init array so we can easily tell if it has changed for (int i = 0; i < numScans * 2; i++) { adblData[i] = 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); ShowReadings(adblData); saveDataInLists(adblData); frmGrafiek.blNieuweData = true; //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. //Likewise, if this program can not retrieve data quickly enough, the UD driver's //own backlog will fill. LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.STREAM_BACKLOG_COMM, ref dblCommBacklog, 0); LJUD.eGet(u3.ljhandle, LJUD.IO.GET_CONFIG, LJUD.CHANNEL.STREAM_BACKLOG_UD, ref dblUDBacklog, 0); DisplayBacklog(dblUDBacklog, dblCommBacklog); } catch (LabJackUDException e) { ShowErrorMessage(e); return; } } }
private void resetLabjack() { tsslbl_Error.Text = ""; if (u3 != null) { LJUD.ResetLabJack(u3.ljhandle); MessageBox.Show("Wacht a.u.b. op het bericht:" + " Verbonden met de Labjack "); Thread.Sleep(5000); u3 = null; btnStartStop.Text = "Start"; } }
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 }
public void performActions() { double dblValue = 0; int intValue = 0; int binary; int[] aEnableTimers = new int[2]; int[] aEnableCounters = new int[2]; int[] aTimerModes = new int[2]; double[] adblTimerValues = new double[2]; int[] aReadTimers = new int[2]; int[] aUpdateResetTimers = new int[2]; int[] aReadCounters = new int[2]; int[] aResetCounters = new int[2]; double[] adblCounterValues = { 0, 0 }; try { //Open the first found LabJack U6. u6 = new U6(LJUD.CONNECTION.USB, "0", true); // Connection through USB //Take a single-ended measurement from AIN3. binary = 0; LJUD.eAIN(u6.ljhandle, 3, 199, ref dblValue, -1, -1, -1, binary); Console.Out.WriteLine("AIN3 = {0:0.###}\n", dblValue); //Set DAC0 to 3.0 volts. dblValue = 3.0; binary = 0; LJUD.eDAC(u6.ljhandle, 0, dblValue, binary, 0, 0); Console.Out.WriteLine("DAC0 set to {0:0.###} volts\n", dblValue); //Read state of FIO0. LJUD.eDI(u6.ljhandle, 0, ref intValue); Console.Out.WriteLine("FIO0 = {0:0.#}\n", intValue); //Set the state of FIO3. intValue = 1; LJUD.eDO(u6.ljhandle, 3, intValue); Console.Out.WriteLine("FIO3 set to = {0:0.#}\n\n", intValue); } catch (LabJackUDException e) { showErrorMessage(e); } Console.ReadLine(); // Pause for user }
/// <summary> /// Actually stops the stream on the LabJack /// </summary> private void StopStreaming() { //Stop the stream try { if (u3 != null) { LJUD.eGet(u3.ljhandle, LJUD.IO.STOP_STREAM, 0, ref dummyDouble, dummyDoubleArray); } } catch (LabJackUDException e) { ShowErrorMessage(e); return; } }
public void outputLow() { try { //Open the first found LabJack U3. u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB //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); //First some configuration commands. These will be done with the ePut //function which combines the add/go/get into a single call. //Configure FIO0-FIO3 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 b0000000000001111 or d15. //LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_ANALOG_ENABLE_PORT, 0, 15, 16); //The following commands will use the add-go-get method to group //multiple requests into a single low-level function. //Set DAC0 to 0 volts. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_DAC, 0, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_DAC, 1, 5, 0, 0); //Set digital output FIO2 to output-high. //LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, 1, 0, 0, 0); LJUD.GoOne(u3.ljhandle); //Set digital output FIO3 to output-low. //LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, 1, 0, 0, 0); } catch (LabJackUDException e) { ShowErrorMessage(e); } }
/// <summary> /// Loads / configures the U3 and displays the UD driver version /// </summary> /// <param name="sender">The object that executed this method</param> /// <param name="e">Event parameters</param> private void TimedWindow_Load(object sender, System.EventArgs e) { double dblDriverVersion; // Disable the go button until the device has loaded goStopButton.Enabled = false; // Create the event timer but do not start it updateTimer = new System.Timers.Timer(); updateTimer.Elapsed += new ElapsedEventHandler(TimerEvent); updateTimer.Interval = TIMER_INTERVAL; //Read and display the UD version. dblDriverVersion = LJUD.GetDriverVersion(); versionDisplay.Text = String.Format("{0:0.000}", dblDriverVersion); try { //Open the first found LabJack U3. u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB //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-FIO3 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 b0000000000001111 or d15. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_ANALOG_ENABLE_PORT, 0, 15, 16); } catch (LabJackUDException exc) { ShowErrorMessage(exc); return; } // Re-enable the go button goStopButton.Enabled = true; }
public void Main() { Initialize(); while (isValid) { for (byte i = 0; i < 16; i++) { Execute(LJU3Commands.GET_DIGITAL_BIT, new object[] { (int)i, 0, 0, 0 }); } DeQueue(); try { LJUD.GoOne(u3.ljhandle); } catch (LabJackUDException e) { evth.LogMessage(this, new LogEventArgs(LogLevel.ERROR, e.Message)); isValid = false; return; } Thread.Sleep(10); GetResults(); } }
public void performActions() { //long lngGetNextIteration; //LJUD.IO ioType=0, channel=0; //double dblValue=0; //double numI2CBytesToWrite; double numBytes = 0; byte[] array = new byte[256]; try { //Open the LabJack. u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB //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); // 1 MHz timer clock base. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.MHZ1_DIV, 0); // Set clock divisor to 1, so timer clock is 1 MHz. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_DIVISOR, (double)LJUD.TIMERCLOCKS.MHZ1_DIV, 0); // Set timer/counter pin offset to 4. TX and RX appear after any timers and counters on U3 // hardware rev 1.30. We have no timers or counters enabled, so TX=FIO4 and RX=FIO5. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_COUNTER_PIN_OFFSET, 4, 0); // Set data rate for 9600 bps communication. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.ASYNCH_BAUDFACTOR, 204, 0); // Enable UART. LJUD.ePut(u3.ljhandle, LJUD.IO.ASYNCH_COMMUNICATION, LJUD.CHANNEL.ASYNCH_ENABLE, 1, 0); // Transmit 2 bytes. numBytes = 3; array[0] = 20; array[1] = 75; LJUD.eGet(u3.ljhandle, LJUD.IO.ASYNCH_COMMUNICATION, LJUD.CHANNEL.ASYNCH_TX, ref numBytes, array); // Read 2 bytes. numBytes = 9999; //Dummy values so we can see them change. array[0] = 111; array[1] = 111; LJUD.eGet(u3.ljhandle, LJUD.IO.ASYNCH_COMMUNICATION, LJUD.CHANNEL.ASYNCH_RX, ref numBytes, array); } catch (LabJackUDException e) { showErrorMessage(e); } //Display the read data. Console.Out.WriteLine("Pre-read buffer size = {0:0}\n\n", numBytes); Console.Out.WriteLine("Read data = {0:0.#}, {1:0.#}\n\n", array[0], array[1]); Console.ReadLine(); // Pause for user }
public void performActions() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double numSPIBytesToTransfer; byte[] dataArray = new byte[50]; //Open the LabJack U6. try { u6 = new U6(LJUD.CONNECTION.USB, "0", true); // Connection through USB //First, configure the SPI communication. //Enable automatic chip-select control. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_AUTO_CS, 1, 0, 0); //Do not disable automatic digital i/o direction configuration. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_DISABLE_DIR_CONFIG, 0, 0, 0); //Mode A: CPHA=1, CPOL=1. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_MODE, 0, 0, 0); //125kHz clock. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_CLOCK_FACTOR, 0, 0, 0); //MOSI is FIO2 LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_MOSI_PIN_NUM, 2, 0, 0); //MISO is FIO3 LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_MISO_PIN_NUM, 3, 0, 0); //CLK is FIO0 LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SPI_CLK_PIN_NUM, 0, 0, 0); //CS is FIO1 LJUD.AddRequest(u6.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(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); } } } //This example transfers 4 test bytes. numSPIBytesToTransfer = 4; dataArray[0] = 175; dataArray[1] = 245; dataArray[2] = 170; dataArray[3] = 240; //Transfer the data. The write and read is done at the same time. try { LJUD.eGet(u6.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() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblVal = 0; double ValueDIPort = 0; int intVal = 0; double val = 0; double[] ValueAIN = new double[16]; long time = 0; long numIterations = 1; int numChannels = 16; //Number of AIN channels, 0-16. long quickSample = 0; //Set to TRUE for quick AIN sampling. See section 2.6 / 3.1 of the User's Guide long longSettling = 1; //Set to TRUE for extra AIN settling time. try { //Open the first found LabJack. u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB //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 quickSample. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, quickSample, 0); //Configure longSettling. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_SETTLING_TIME, longSettling, 0); //Configure the necessary lines as analog. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_ANALOG_ENABLE_PORT, 0, Math.Pow(2, numChannels) - 1, numChannels); //Now an Add/Go/Get block to configure the timers and counters. These //are configured on EIO0-EIO3, so if more than 8 analog inputs are //enabled then the analog inputs use these lines. if (numChannels <= 8) { //Set the timer/counter pin offset to 8, which will put the first //timer/counter on EIO0. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_COUNTER_PIN_OFFSET, 8, 0, 0); //Use the default clock source. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_CLOCK_BASE, (double)LJUD.TIMERCLOCKS.MHZ48, 0, 0); //Enable 2 timers. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.NUMBER_TIMERS_ENABLED, 2, 0, 0); //Configure Timer0 as 8-bit PWM. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_MODE, 0, (double)LJUD.TIMERMODE.PWM8, 0, 0); //Set the PWM duty cycle to 50%. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 32768, 0, 0); //Configure Timer1 as 8-bit PWM. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_MODE, 1, (double)LJUD.TIMERMODE.PWM8, 0, 0); //Set the PWM duty cycle to 50%. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 1, 32768, 0, 0); //Enable Counter0. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 0, 1, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, 1, 1, 0, 0); //Execute the requests. LJUD.GoOne(u3.ljhandle); } //Now add requests that will be processed every iteration of the loop. //Add analog input requests. for (int j = 0; j < numChannels; j++) { LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_AIN, j, 0, 0, 0); } //Set DAC0 to 2.5 volts. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_DAC, 0, 2.5, 0, 0); //Read CIO digital lines. LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_DIGITAL_PORT, 16, 0, 4, 0); //Only do the timer/counter stuff if there are less than 8 analog inputs. if (numChannels <= 8) { LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_COUNTER, 0, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_COUNTER, 1, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_TIMER, 0, 0, 0, 0); LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_TIMER, 1, 0, 0, 0); //Set the PWM duty cycle to 50%. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 0, 32768, 0, 0); //Set the PWM duty cycle to 50%. LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_TIMER_VALUE, 1, 32768, 0, 0); } time = Environment.TickCount; } catch (LabJackUDException e) { showErrorMessage(e); } for (int i = 0; i < numIterations; i++) { try { //Execute the requests. LJUD.GoOne(u3.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(u3.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(u3.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); } } } } time = Environment.TickCount - time; Console.Out.WriteLine("Milleseconds per iteration = {0:0.000}\n", (double)time / (double)numIterations); Console.Out.WriteLine("\nDigital Input = {0:0.###}\n", ValueDIPort); Console.Out.WriteLine("\nAIN readings from last iteration:\n"); for (int j = 0; j < numChannels; j++) { Console.Out.WriteLine("{0:0.000}\n", ValueAIN[j]); } 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> /// performs read on LabJack device /// </summary> /// <param name="source">The object that called the event</param> /// <param name="e">Event details</param> public void TimerEvent(object source, ElapsedEventArgs e) { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double Value2 = 0, Value3 = 0; double ValueDIBit = 0; // dummy variables to satisfy certian method signatures double dummyDouble = 0; int dummyInt = 0; try { //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); //Read digital input FIO1. LJUD.AddRequest(u6.ljhandle, LJUD.IO.GET_DIGITAL_BIT, 1, 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. The rest of the results are in the below loop. 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; } 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); return; } } } // Display results ain2Display.Text = String.Format("{0:0.###}", Value2); ain3Display.Text = String.Format("{0:0.###}", Value3); fio1Display.Text = String.Format("{0:0.###}", ValueDIBit); }
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 }
/// <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 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 U6 try { //Open the device u6 = new U6(LJUD.CONNECTION.USB, "0", true); //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.AddRequest(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.AIN_RESOLUTION, 0, 0, 0); //Configure the analog input range on channels 2 and 3 for bipolar 10v. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 2, (double)LJUD.RANGES.BIP10V, 0, 0); LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_AIN_RANGE, (LJUD.CHANNEL) 3, (double)LJUD.RANGES.BIP10V, 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. The rest of the results are in the below loop. 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) { 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); return; } } } // Enable the start button goStopButton.Enabled = true; }
/// <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 U6. u6 = new U6(LJUD.CONNECTION.USB, "0", true); // Connection through USB //Read and display the hardware version of this U6. LJUD.eGet(u6.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 U6. LJUD.eGet(u6.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 +-10 volts. LJUD.AddRequest(u6.ljhandle, LJUD.IO.PUT_AIN_RANGE, 0, (double)LJUD.RANGES.BIP10V, 0, 0); //Configure the stream: //Set the scan rate. LJUD.AddRequest(u6.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(u6.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(u6.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(u6.ljhandle, LJUD.IO.CLEAR_STREAM_CHANNELS, 0, 0, 0, 0); LJUD.AddRequest(u6.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 0, 0, 0, 0); LJUD.AddRequest(u6.ljhandle, LJUD.IO.ADD_STREAM_CHANNEL, 1, 0, 0, 0); //Execute the list of requests. LJUD.GoOne(u6.ljhandle); } catch (LabJackUDException e) { ShowErrorMessage(e); return(false); } //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 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(u6.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() { 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() { double dblDriverVersion; LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double Value22 = 9999, Value32 = 9999, Value42 = 9999; double Value23 = 9999, Value33 = 9999, Value43 = 9999; // Variables to satisfy certain method signatures int dummyInt = 0; double dummyDouble = 0; //Read and display the UD version. dblDriverVersion = LJUD.GetDriverVersion(); Console.Out.WriteLine("UD Driver Version = {0:0.000}\n\n", dblDriverVersion); //Open the U3 with local ID 2. try { unit2 = new U3(LJUD.CONNECTION.USB, "2", false); // Connection through USB unit3 = new U3(LJUD.CONNECTION.USB, "3", false); // Connection through USB //Start by using the pin_configuration_reset IOType so that all //pin assignments are in the factory default condition. LJUD.ePut(unit2.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); LJUD.ePut(unit3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0); //First a configuration command. These will be done with the ePut //function which combines the add/go/get into a single call. //Configure FIO2-FIO4 as analog, all else as digital, on both devices. //That means we will start from channel 0 and update all 16 flexible bits. //We will pass a value of b0000000000011100 or d28. LJUD.ePut(unit2.ljhandle, LJUD.IO.PUT_ANALOG_ENABLE_PORT, 0, 28, 16); LJUD.ePut(unit3.ljhandle, LJUD.IO.PUT_ANALOG_ENABLE_PORT, 0, 28, 16); //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 AIN2. LJUD.AddRequest(unit2.ljhandle, LJUD.IO.GET_AIN, 2, 0, 0, 0); LJUD.AddRequest(unit3.ljhandle, LJUD.IO.GET_AIN, 2, 0, 0, 0); //Request a single-ended reading from AIN3. LJUD.AddRequest(unit2.ljhandle, LJUD.IO.GET_AIN, 3, 0, 0, 0); LJUD.AddRequest(unit3.ljhandle, LJUD.IO.GET_AIN, 3, 0, 0, 0); //Request a reading from AIN4 using the Special 0-3.6 range. LJUD.AddRequest(unit2.ljhandle, LJUD.IO.GET_AIN_DIFF, 4, 0, 32, 0); LJUD.AddRequest(unit3.ljhandle, LJUD.IO.GET_AIN_DIFF, 4, 0, 32, 0); } catch (LabJackUDException e) { showErrorMessage(e); } bool isFinished = false; while (!isFinished) { try { //Execute all requests on all open LabJacks. LJUD.Go(); //Get all the results for unit 2. The input measurement results are stored. LJUD.GetFirstResult(unit2.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } bool unit2Finished = false; while (!unit2Finished) { switch (ioType) { case LJUD.IO.GET_AIN: switch ((int)channel) { case 2: Value22 = dblValue; break; case 3: Value32 = dblValue; break; } break; case LJUD.IO.GET_AIN_DIFF: Value42 = dblValue; break; } try { LJUD.GetNextResult(unit2.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) { unit2Finished = true; } else { showErrorMessage(e); } } } //Get all the results for unit 3. The input measurement results are stored. try { LJUD.GetFirstResult(unit3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); } catch (LabJackUDException e) { showErrorMessage(e); } bool unit3Finished = false; while (!unit3Finished) { switch (ioType) { case LJUD.IO.GET_AIN: switch ((int)channel) { case 2: Value23 = dblValue; break; case 3: Value33 = dblValue; break; } break; case LJUD.IO.GET_AIN_DIFF: Value43 = dblValue; break; } try { LJUD.GetNextResult(unit3.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) { unit3Finished = true; } else { showErrorMessage(e); } } } Console.Out.WriteLine("AIN2 (Unit 2) = {0:0.###}\n", Value22); Console.Out.WriteLine("AIN2 (Unit 3) = {0:0.###}\n", Value23); Console.Out.WriteLine("AIN3 (Unit 2) = {0:0.###}\n", Value32); Console.Out.WriteLine("AIN3 (Unit 3) = {0:0.###}\n", Value33); Console.Out.WriteLine("AIN4 (Unit 2) = {0:0.###}\n", Value42); Console.Out.WriteLine("AIN4 (Unit 3) = {0:0.###}\n", Value43); Console.Out.WriteLine("\nPress Enter to go again or (q) to quit\n"); isFinished = Console.ReadLine() == "q"; // Pause for user } }
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 }
public void performActions() { double dblValue = 0; //Open the first found LabJack U3. try { u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB } catch (LabJackUDException e) { showErrorMessage(e); } 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); //Set the Data line to FIO4, which is the default anyway. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SHT_DATA_CHANNEL, 4, 0); //Set the Clock line to FIO5, which is the default anyway. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SHT_CLOCK_CHANNEL, 5, 0); //Set FIO6 to output-high to provide power to the EI-1050. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL) 6, 1, 0); } catch (LabJackUDException e) { showErrorMessage(e); } /* * //Use this code if only a single EI-1050 is connected. * // Connections for one probe: * // Red (Power) FIO6 * // Black (Ground) GND * // Green (Data) FIO4 * // White (Clock) FIO5 * // Brown (Enable) FIO6 * * try * { * //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 (u3.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, 0, 0, 0); * * //Request a humidity reading from the EI-1050. * LJUD.AddRequest (u3.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 (u3.ljhandle); * * //Get the temperature reading. * LJUD.GetResult (u3.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 (u3.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) FIO6 // Black (Ground) GND // Green (Data) FIO4 // White (Clock) FIO5 // // Probe A: // Brown (Enable) FIO7 // // Probe B: // Brown (Enable) DAC0 try { //Set FIO7 to output-low to disable probe A. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL) 7, 0, 0); //Set DAC0 to 0 volts to disable probe B. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_DAC, 0, 0.0, 0); //Set FIO7 to output-high to enable probe A. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL) 7, 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(u3.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, 0, 0, 0); //Request a humidity reading from the EI-1050. LJUD.AddRequest(u3.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(u3.ljhandle); //Get the temperature reading. LJUD.GetResult(u3.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(u3.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 FIO7 to output-low to disable probe A. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL) 7, 0, 0); //Set DAC0 to 3.3 volts to enable probe B. LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_DAC, 0, 3.3, 0); //Since the DACs on the U3 are slower than the communication speed, //we put a delay here to make sure the DAC has time to rise to 3.3 volts //before communicating with the EI-1050. Thread.Sleep(30); //Wait 30 ms. //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(u3.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, 0, 0, 0); //Request a humidity reading from the EI-1050. LJUD.AddRequest(u3.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(u3.ljhandle); //Get the temperature reading. LJUD.GetResult(u3.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(u3.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(u3.ljhandle, LJUD.IO.PUT_DAC, 0, 0.0, 0); //If we were going to loop and talk to probe A next, we would //want a delay here to make sure the DAC falls to 0 volts //before enabling probe A. Thread.Sleep(30); //Wait 30 ms. } catch (LabJackUDException e) { showErrorMessage(e); } //End of dual probe code. //*/ Console.ReadLine(); // Pause for user }
public void performActions() { // long lngGetNextIteration; // LJUD.IO ioType=0, channel=0; // double dblValue=0; long i = 0; double pinNum = 4; //4 means the LJTick-DAC is connected to FIO4/FIO5. 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 U3(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; }