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); } } }
//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); }
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() { 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 dblValue = 0; //Open the first found LabJack U6. try { u6 = new U6(LJUD.CONNECTION.USB, "0", true); // Connection through USB //Set the Data line to FIO0 LJUD.ePut(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SHT_DATA_CHANNEL, 0, 0); //Set the Clock line to FIO1 LJUD.ePut(u6.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.SHT_CLOCK_CHANNEL, 1, 0); //Set FIO2 to output-high to provide power to the EI-1050. LJUD.ePut(u6.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL) 2, 1, 0); } catch (LabJackUDException e) { showErrorMessage(e); } ///* //Use this code if only a single EI-1050 is connected. // Connections for one probe: // Red (Power) FIO2 // Black (Ground) GND // Green (Data) FIO0 // White (Clock) FIO1 // Brown (Enable) FIO2 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(u6.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, 0, 0, 0); //Request a humidity reading from the EI-1050. LJUD.AddRequest(u6.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(u6.ljhandle); //Get the temperature reading. LJUD.GetResult(u6.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(u6.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 (u6.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL)3, 0, 0); * * //Set DAC0 to 0 volts to disable probe B. * LJUD.ePut (u6.ljhandle, LJUD.IO.PUT_DAC, 0, 0.0, 0); * * //Set FIO3 to output-high to enable probe A. * LJUD.ePut (u6.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 (u6.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, 0, 0, 0); * * //Request a humidity reading from the EI-1050. * LJUD.AddRequest (u6.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 (u6.ljhandle); * * //Get the temperature reading. * LJUD.GetResult (u6.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 (u6.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 (u6.ljhandle, LJUD.IO.PUT_DIGITAL_BIT, (LJUD.CHANNEL)3, 0, 0); * * //Set DAC0 to 3.3 volts to enable probe B. * LJUD.ePut (u6.ljhandle, LJUD.IO.PUT_DAC, 0, 3.3, 0); * * //Since the DACs on the U6 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 (u6.ljhandle, LJUD.IO.SHT_GET_READING, LJUD.CHANNEL.SHT_TEMP, 0, 0, 0); * * //Request a humidity reading from the EI-1050. * LJUD.AddRequest (u6.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 (u6.ljhandle); * * //Get the temperature reading. * LJUD.GetResult (u6.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 (u6.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 (u6.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() { LJUD.IO ioType = 0; LJUD.CHANNEL channel = 0; double dblValue = 0; double dummyDouble = 0; int dummyInt = 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; //Open the LabJack. try { device = new U3(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 FIO4 LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_SCL_PIN_NUM, 4, 0, 0); //SDA is FIO5 LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_SDA_PIN_NUM, 5, 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. Random rand = new Random((int)DateTime.Now.Ticks); for (i = 1; i < 5; i++) { writeArray[i] = (byte)(rand.NextDouble() * 255); } 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. //double[] readArray; //readArray = (double[])(readArray); 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. bytes = BitConverter.GetBytes((long)((2.4 * slopeDACB) + offsetDACB) / 256); writeArray[1] = bytes[0]; //Upper byte of binary DAC value. writeArray[2] = bytes[1]; //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 1.5 volts. numI2CBytesToWrite = 3; writeArray[0] = 49; //Write and update DACB. bytes = BitConverter.GetBytes((long)((1.5 * slopeDACB) + offsetDACB) / 256); writeArray[1] = bytes[0]; //Upper byte of binary DAC value. writeArray[2] = bytes[1]; //Lower byte of binary DAC value. LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, numI2CBytesToWrite, writeArray, 0); LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, 0, 0, 0); //Execute the requests. LJUD.GoOne(device.ljhandle); //Get the result of the write just to check for an error. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, ref dummyDouble); //Get the write ACKs and compare to the expected value. We expect bit 0 to be //the ACK of the last data byte progressing up to the ACK of the address //byte (data bytes only for Control firmware 1.43 and less). So if n is the //number of data bytes, the ACKs value should be (2^(n+1))-1. LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, ref writeACKS); expectedACKS = Math.Pow(2, numI2CBytesToWrite + 1) - 1; if (writeACKS != expectedACKS) { Console.Out.WriteLine("Expected ACKs = {0:0}, Received ACKs = %0.f\n", expectedACKS, writeACKS); } Console.Out.WriteLine("DACB set to 1.5 volts\n"); Console.ReadLine(); // Pause for user return; }