question

SidiqiUsman-0814 avatar image
0 Votes"
SidiqiUsman-0814 asked SidiqiUsman-0814 commented

C# Windows Form Application with Labjack T7 Pro

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)
         {
            
     }
     }

}

dotnet-csharpwindows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SidiqiUsman-0814. Since this thread is related to Labjack , it is a 3rd party product, I recommend that you could ask the question here.


0 Votes 0 ·

Labjack support usually only helpds with hardware problems. I have asked them already.

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered SidiqiUsman-0814 commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the response. So when I copy the showErrorMessage and perfom Actions do I copy the public void with it? Because it has red lines under public now and this all under the private void button1_Click

0 Votes 0 ·

Also the performActions says its declared but never used.

0 Votes 0 ·