C# Windows Form Application with Labjack T7 Pro

Sidiqi, Usman 1 Reputation point
2021-10-13T22:07:29.05+00:00

I am trying to display a voltage reading from the labjack controller using a windows form application. I have my windows form up and some code from the labjack examples. How would I go about this?

LABJACK CODE:
//-----------------------------------------------------------------------------
// SingleAIN.cs
//
// Demonstrates reading a single analog input (AIN) from a LabJack.
//
// support@labjack.com
//-----------------------------------------------------------------------------
using System;
using LabJack;

namespace SingleAIN
{
class SingleAIN
{
static void Main(string[] args)
{
SingleAIN sAIN = new SingleAIN();
sAIN.performActions();
}

    public void showErrorMessage(LJM.LJMException e)
    {
        Console.Out.WriteLine("LJMException: " + e.ToString());
        Console.Out.WriteLine(e.StackTrace);
    }

    public void performActions()
    {
        int handle = 0;
        int devType = 0;
        int conType = 0;
        int serNum = 0;
        int ipAddr = 0;
        int port = 0;
        int maxBytesPerMB = 0;
        string ipAddrStr = "";

        try
        {
            //Open first found LabJack
            LJM.OpenS("ANY", "ANY", "ANY", ref handle);  // Any device, Any connection, Any identifier
            //LJM.OpenS("T7", "ANY", "ANY", ref handle);  // T7 device, Any connection, Any identifier
            //LJM.OpenS("T4", "ANY", "ANY", ref handle);  // T4 device, Any connection, Any identifier
            //LJM.Open(LJM.CONSTANTS.dtANY, LJM.CONSTANTS.ctANY, "ANY", ref handle);  // Any device, Any connection, Any identifier

            LJM.GetHandleInfo(handle, ref devType, ref conType, ref serNum, ref ipAddr, ref port, ref maxBytesPerMB);
            LJM.NumberToIP(ipAddr, ref ipAddrStr);
            Console.WriteLine("Opened a LabJack with Device type: " + devType + ", Connection type: " + conType + ",");
            Console.WriteLine("Serial number: " + serNum + ", IP address: " + ipAddrStr + ", Port: " + port + ",");
            Console.WriteLine("Max bytes per MB: " + maxBytesPerMB);

            //Setup and call eReadName to read from an AIN on the LabJack.
            string name = "AIN0";
            double value = 0;
            LJM.eReadName(handle, name, ref value);

            Console.WriteLine("\n" + name +" reading : " + value.ToString("F4") + " V");
        }
        catch (LJM.LJMException e)
        {
            showErrorMessage(e);
        }

        LJM.CloseAll();  //Close all handles

        Console.WriteLine("\nDone.\nPress the enter key to exit.");
        Console.ReadLine();  //Pause for user
    }
}

}


FORM CODE:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Windows.Forms;

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

    }
    }

}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,820 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,194 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 111.7K Reputation points
    2021-10-14T07:54:09.127+00:00

    Try using the sample code, putting it inside the button1_Click, for example:

    private void button1_Click(object sender, EventArgs e)
    {
        performActions();
    }
    

    Copy the performActions and showErrorMessage functions too.

    Replace Console.WriteLine and Console.Out.WriteLine with textBox1.AppendText. Also use some *textBox1.AppendText(Environment.NewLine) * to separate the lines.

    Set the Multiline property of textBox1 to "True", Scrollbars to "Both" and WordWrap to "True", and resize it.

    Start the program and press button1.