Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

labjack and C#


  • Please log in to reply
9 replies to this topic

#1 Mariett89

Mariett89
  • Members
  • 6 posts

Posted 22 October 2013 - 08:24 AM

hi i need some help... i want to send my output using the labcjack with C# in visual studio is a simple signal, but i don't know how to do that...

i'm using this code to read the input, but i have understood that i can do the same with only one line of code

 

 public void Medicion()
        {
            double dblDriverVersion;
            LJUD.IO ioType = 0;
            LJUD.CHANNEL channel = 0;
            double dblValue = 0;
            int dummyInt = 0;
            double dummyDouble = 0;
 
            dblDriverVersion = LJUD.GetDriverVersion();
            try
            {
                //Request a single-ended reading from AIN0.
                LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_AIN, 0, 0, 0, 0);
 
            }
            catch (LabJackUDException a)
            {
                //showErrorMessage(e);
            }
            try
            {
                //Execute the requests.
                LJUD.GoOne(u3.ljhandle);
                LJUD.GetFirstResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble);
            }
            catch (LabJackUDException a)
            {
                //showErrorMessage(e);
            }
 
 
            try { LJUD.GetNextResult(u3.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); }
 
            catch (LabJackUDException a)
            {
                //showErrorMessage(e);
            }
            AIN0 = dblValue;
            //this.SetText(AIN0.ToString());
            //this.SetChart(chart1);
            tiempo = tiempo + To;
 
        }
can you help me to sum this  in just a line?,,, and how to put the output?


#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 22 October 2013 - 10:14 AM

If you need C# specific help someone else will need to follow up, but as a general answer you can do the same thing with a single call to eAIN:

 

http://labjack.com/s...rs-guide/4.2.17

http://labjack.com/s...ers-guide/4.3.9

 

... so rather than using the add/go/get technique, just use the easy function.  Read a little more about this in the UD driver overview:

 

http://labjack.com/s...users-guide/4.1

 

 

As for output, there are easy functions for digital or analog output:

 

http://labjack.com/s...rs-guide/4.2.20

http://labjack.com/s...rs-guide/4.2.18

 

If you are interested in waveform generation, see the following app note:

 

http://labjack.com/s...form-generation



#3 Mariett89

Mariett89
  • Members
  • 6 posts

Posted 23 October 2013 - 12:37 PM

hi my code is this.. i'm not sure if this is the way to use the functions LJUD.eAIN and  LJUD.eDAC.....  can i use LJUD.eAIN to measure two inputs or is only for one?

 

 

 

 

 

using  System;

using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Windows.Forms.DataVisualization.Charting;
using LabJack.LabJackUD;
 
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        Thread h1;
        double AIN0, Ref, To = 1, U, Uant = 7, tiempo = 0;
        double[,] Ada = new double[2, 2] { { 0.9984, 0.0016 }, { 0.0024, 0.9928 } };
        double[] Bda = new double[2] { 0.0105, 0 };
        double[] Xo = new double[3] { 10.5423, 2.1734, 1005.011 };
        double[] Xac = new double[3];
        String aux;
        delegate void SetTextCallback(string text);
        delegate void SetChartCallback(Chart chart);
        private U3 u3;
        public Form1()
        {
            InitializeComponent();
 
            u3 = new U3(LJUD.CONNECTION.USB, "0", true);
            LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);
        }
 
        private void comenzar_Click(object sender, EventArgs e)
        {
            Ref = Convert.ToDouble(textBox1.Text);
            h1 = new Thread(new ThreadStart(control));
            h1.Start();
        }
        private void control()
        {
 
            while (true)
            {
                this.SetText(Convert.ToString(Xo[0]));
                this.SetChart(chart1);
                this.SetChart2(chart1);
                aux = Convert.ToString(Xo[0]);
                aux = aux + " " + Convert.ToString(tiempo) + " " + Convert.ToString(Uant);
                try
                {
                    string fileName = "data.txt";
                    // esto inserta texto en un archivo existente, si el archivo no existe lo crea
                    StreamWriter writer = File.AppendText(fileName);
                    writer.WriteLine(aux);
                    writer.Close();
                }
                catch
                {
                    Console.WriteLine("Error");
                }
                U = Uant;
                Medicion();
                Xac[0] = AIN0;
                Xac[2] = Xo[2] - To * 0.007230 * Xo[2] + To * 0.812 * Ref - To * 0.1267 * Xo[0];
                U = Xac[2] - 94.6673 * Xac[0];
                enviar(U);
                Xo[0] = Xac[0];
                Xo[1] = Xac[1];
                Xo[2] = Xac[2];
                Uant = U;
                tiempo = tiempo + To;
                Thread.Sleep(1000);
            }
        }
        public void enviar(double entrada) {
            //Set DAC0 to 3.0 volts.
            dblValue = 3.0;
            binary = 0;
            LJUD.eDAC(u3.ljhandle, 0, dblValue, binary, 0, 0);
            Console.Out.WriteLine("DAC0 set to {0:0.###} volts\n", dblValue);
 
        
        }
        public void Medicion()
        {
            //Take a single-ended measurement from AIN0.
            binary = 0;
            LJUD.eAIN(u3.ljhandle, 0, 31, ref dblValue, -1, -1, -1, binary);
            Console.Out.WriteLine("AIN0 = {0:0.###}\n", dblValue);
 
        }
        private void SetText(string text2)
        {
            if (this.textBox2.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text2 });
            }
            else
            {
                this.textBox2.Text = text2;
            }
        }
        private void SetChart(Chart chart)
        {
            if (this.chart1.InvokeRequired)
            {
                SetChartCallback g = new SetChartCallback(SetChart);
                this.Invoke(g, new object[] { chart });
            }
            else
            {
                chart1.Series["Series1"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                chart1.Series["Series1"].Points.AddXY(tiempo, Xo[0]);
                chart1.Series["Series1"].ChartArea = "ChartArea1";
 
            }
 
        }
        private void SetChart2(Chart chartj)
        {
            if (this.chart1.InvokeRequired)
            {
                SetChartCallback g = new SetChartCallback(SetChart2);
                this.Invoke(g, new object[] { chartj });
            }
            else
            {
                chart1.Series["Series2"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                chart1.Series["Series2"].Points.AddXY(tiempo, Uant);
                chart1.Series["Series2"].ChartArea = "ChartArea2";
 
            }
 
        }
 
    }
}


#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 23 October 2013 - 04:08 PM

In your eAIN call you should change your -1s to 0 or 1. For Range it doesn't matter, but for Resolution, Settling and Binary nonzero values enable settings. I'm guessing you wanted them disabled, so pass all zeroes instead of -1 if that's the case. Otherwise your eAIN and eDAC calls look correct. eAIN is reading single ended AIN0 and eDAC is setting DAC0 to 3.0 V.

 

eAIN only performs 1 reading per call. For reading multiple channels you can perform multiple eAIN calls indicating the channel you want to read, or use AddRequest/Go/GetResult method to group multiple readings into one USB command/response. Multiple C# examples demonstrate this, and here's pseudocode on it:

 

http://labjack.com/s...ers-guide/4.3.3



#5 Mariett89

Mariett89
  • Members
  • 6 posts

Posted 25 October 2013 - 01:28 PM

hi thank you for your help... i have a another question, how can i measure two input at the same time? and simulate this signals simultaneously,  because if i put two  of this functions   LJUD.eAIN(u3.ljhandle, 0, 31, ref dblValue, 0, 0, 0, binary);

            Console.Out.WriteLine("AIN0 = {0:0.###}\n", dblValue);, in different ports, i am reading first one input and then the another one, and in my code... i need to read the inputs in real time  at the same time..
 
thak u 


#6 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 25 October 2013 - 02:06 PM

You will need to use the AddRequest/Go/GetRequest method, which sends a list of requests to the U3. Many of the C# examples demonstrate this and the link I provided in my last post showed pseudocode for reading multiple analog inputs using AddRequests. To read AIN0 and AIN1 for example it would look like this:

 

LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_AIN, 0, 0, 0, 0); //Request single ended AIN0 reading

LJUD.AddRequest(u3.ljhandle, LJUD.IO.GET_AIN, 1, 0, 0, 0); //Request single ended AIN1 reading

 

//Execute the requests

LJUD.GoOne(u3.ljhandle);

 

LJUD.GetResult(u3.ljhandle, LJUD.IO.GET_AIN, 0, ref value0); //AIN0 reading

LJUD.GetResult(u3.ljhandle, LJUD.IO.GET_AIN, 1, ref value1); //AIN1 reading

 

The examples tend to show GetFirstResult/GetNextResult to read out values which is just an alternate method to using GetResult.



#7 Mariett89

Mariett89
  • Members
  • 6 posts

Posted 05 December 2013 - 06:45 AM

Hi... i have a question... i have this function (enviar), but i want that every time i call  to (enviar), the port DAC0  is restarted... i have this      LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);
in the beginning of my code, but it only restart the ports when button start is clicked and i need that everytime i read a value en dblvalue, the value is cleared. That is, I need to reset the port each time it read a measure

 

public void enviar(double entrada)

        {
            double dblValue0;
            if (entrada == 0)
                dblValue0 = 0;
            else
            dblValue0 = (0.44 * entrada) + 0.9;
            
            int binary = 0;
            LJUD.eDAC(u3.ljhandle, 0, dblValue0, binary, 0, 0);
          
 
        }


#8 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 05 December 2013 - 08:00 AM

 

 

... i have this function (enviar), but i want that every time i call  to (enviar), the port DAC0  is restarted...

 

So each time you call enviar() you want DAC to be "restarted".  What do you mean by "restarted"?  Do you mean set to 0.0?  I see that your call to enviar() does set DAC0 to 0.0 if entrada==0.



#9 Mariett89

Mariett89
  • Members
  • 6 posts

Posted 16 December 2013 - 01:27 PM

hi... i need help because and using this function to mesuare:

 public void Medicion()
        {
           
            double dblValue = 99999;
            int binary = 0;
            //LJUD.eGet(u3.ljhandle, LJUD.IO.GET_AIN, 0, ref dblValue, 0);
            LJUD.eAIN(u3.ljhandle, 0, 31, ref dblValue, 0, 0, 0, binary);
            AIN0 = (6.7 * (dblValue));
            
 
        }
but i have a problem, because sometime it looks like the measure doesn't change, in the reality is a number, and the labjack still measuring the previous measure,  can you tell me why? you have to know that the measuring is in real time. 
 
another question, is a good idea to use LJUD.eAIN?, because I also read about  LJUD.eGET, my measurements are real time every second, so i do not know if it is remaining waste, from the previous measure, in the read port, in this case AIN0.
 
if i try with LJUD.eGET I have to use some method to send Compatible?, i ask because now i'm using to send eDAC, and to read eAIN, if i use eGet to read,  can i use e.DAC to send? or i need to use another method compatible with e.Get.


#10 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 17 December 2013 - 03:34 PM

 

 

 sometime it looks like the measure doesn't change, in the reality is a number, and the labjack still measuring the previous measure,

 

If you do not get an exception from the call, then it has taken a new measurement.  If you are seeing the same value, then you are actually getting the same value back.  That is not unusual.  For example, I just did a quick test with my U3-HV.  Here are 10 consecutive readings from AIN0 where I connected a jumper from AIN0 to GND:

 

-0.000536
-0.005601
-0.000536
-0.005601
-0.005601
-0.000536
-0.005601
-0.005601
-0.005601
-0.000536
 
You can see it is bouncing between 2 values.  The difference between these values is about 5mV, which is the resolution of a high-voltage analog input on the U3-HV.

 

 

 

 

 

 is a good idea to use LJUD.eAIN?

 

You could use an add/go/get block, a call to eGet, or a call to eAIN.  The results would be the same with any of those methods, so I would just use eAIN as that is the easiest way to read an analog input.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users