Sine wave using U3 and c# | LabJack
 
Saturday/Sunday, March 28th&29th, 2020
Support: LabJack engineers available through email and chat.
Shipping: We are processing and shipping all orders.
Stock: 55/56 products in stock.
Supply Chain: Some disruptions.

Sine wave using U3 and c#

6 posts / 0 new
Last post
Vamsi Krishna Surya
vamsikrishnasurya's picture
Sine wave using U3 and c#

Need assistance to make an application that when you press a button it runs a 10Hz sine wave from one of the analog outputs

LabJack Support
LabJack Support's picture
The U3 can be communicated

The U3 can be communicated with using command-response messages that are capable of doing this however the U3's analog outputs are relatively slow unlike that of the T4 or T7 ref: appendix-a: Analog Outputs (DAC) - Time Constant.  Our T-Series devices have stream out and Lua Scripting capabilities (example script: Output Sine Wave) that enable features like this. 

Vamsi Krishna Surya
vamsikrishnasurya's picture
Can you please provide me

Can you please provide me with some C# syntaxes for feeding data to the DAC0 so that i can measure the output coming from it on an oscilloscope. I am not so aware with C# this is my first ever project on both C# and visual studio so please guide me as I am not much aware about programming.

LabJack Support
LabJack Support's picture
Take a look at the C#

Take a look at the C# examples:

https://labjack.com/support/software/examples/ud/dotnet

In particular the U3_EFunctions and U3_Simple examples set a DAC (among other thigs) using two different methods.

General UD driver documentation is here:

https://labjack.com/support/datasheets/u3/high-level-driver

Note that the C# interface syntax differs from the general doc. (it is C/C++ based), but the functions operate the same except the C# functions throw an exception for errors.

If you are new to C#, it will be useful to go through some language tutorials and documentation:

https://docs.microsoft.com/en-us/dotnet/csharp/

 

Vamsi Krishna Surya
vamsikrishnasurya's picture
using System;

using System;
using System.Threading;

// Import the UD .NET wrapper object.  The dll referenced is installed by the
// LabJackUD installer.
using LabJack.LabJackUD;

namespace U3_Sine
{
    class U3_Sine
    {

        // our U3 variable
        private U3 u3;

        static void Main(string[] args)
        {
            U3_Sine a = new U3_Sine();
            a.performActions();
        }

        // If error occured print a message indicating which one occurred. If the error is a group error (communication/fatal), quit
        public void ShowErrorMessage(LabJackUDException e)
        {
            Console.Out.WriteLine("Error: " + e.ToString());
            if (e.LJUDError > U3.LJUDERROR.MIN_GROUP_ERROR)
            {
                Console.ReadLine(); // Pause for the user
                Environment.Exit(-1);
            }
        }

        public void performActions()
        {
            double dblDriverVersion;
            double vout = 0;
            double amplitude = 2;
            double offset = 2.5;
            double radstep = .1;
            double rads = 0;
            double dblValue = 0;
            double[] dummyDoubleArray = { 0 };

            //Read and display the UD version.
            dblDriverVersion = LJUD.GetDriverVersion();
            Console.Out.WriteLine("UD Driver Version = {0:0.000}\n\n", dblDriverVersion);

            try
            {
                //Open the first found LabJack U3.
                u3 = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB

                Console.Out.WriteLine("Output sine wave. Analog output is DAC0.");

                //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 timer/ counter pin offset to 7, which will put the first
                //timer/counter on FIO7.
                LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.TIMER_COUNTER_PIN_OFFSET, 7, 0);

                //Enable Counter1 (FIO7).
                LJUD.ePut(u3.ljhandle, LJUD.IO.PUT_COUNTER_ENABLE, (LJUD.CHANNEL)1, 1, 0);

                //Execute the requests.
                LJUD.GoOne(u3.ljhandle);
            }
            catch (LabJackUDException e)
            {
                ShowErrorMessage(e);
            }
            try
            {
                //Request a read from the counter.
                LJUD.eGet(u3.ljhandle, LJUD.IO.GET_COUNTER, (LJUD.CHANNEL)1, ref dblValue, dummyDoubleArray);
                Console.Out.WriteLine("Counter = {0:0.0}", dblValue);

                while(dblValue > 0.0)
                {
                    if(rads >= 2)
                        radstep *= -1;
                    vout = ((Math.Sin(rads)) * amplitude) + offset;
                    rads += radstep;
                    // Set DAC0
                    LJUD.AddRequest(u3.ljhandle, LJUD.IO.PUT_DAC, 0, vout, 0, 0);
                    Console.Out.WriteLine("vout: {0:0.0}", vout);
                }
            }
            catch (LabJackUDException e)
            {
                ShowErrorMessage(e);
            }
            finally
            {
                //Reset all pin assignments to factory default condition.
                LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);
            }
        }

    }
}

 

 

This is my code but nothing is showing up on the console output when executed. I have got no errors . please help. thank you .

LabJack Support
LabJack Support's picture
I ran your code in a console

I ran your code in a console application project and saw the output (WriteLines) in the console window. By default in Visual Studios when the run time is done, the console window closes. This can make it tough to see the output of simpler code. I throw something like this at the end of my test code so I have time to see the output:

Console.WriteLine("Press Enter to exit.\n");
Console.ReadLine();  //Wait for the Enter key press