Skip to main content

This article deals with using custom Classes with the GalilTools API.  In the first article located here:

http://www.galil.com/news/software/how-create-custom-class-library-galiltools

we created a new custom class that was able to access the RIO Data Record and parse it.  In this article, we will use the class that was created (RIO_DataRecord.cs) and show how to include it into a new C# application.  First create a new C# project and call it RIO_DR_class_demo as shown here:

Copy the class file created in the previous TechTalk article (named RIO_DataRecord.cs) and put the class file into the folder where your solution is located.

Add the RIO Data Record Class file to Solution Explorer by clicking "Project ->Add Existing Item" and choosing the RIO_DataRecord.cs file.  You should now see the file in the Solution Explorer as part of the project.

Double Click on the Form to get to the "Code View".

Click on Project->Add Reference and click on the COM tab.  Add the Galil component. (Must have GalilTools or GalilTools Lite installed) (Select -> OK)

Add a label to the form and a Button.  Change the text of the button to Get Sample Time. Add a textbox to the form. Delete the default text in the textbox and the label so they are blank.

At the top of the code, we need to add the namespace that is called out in the class file so add the following:

using RIO_DataRecord_namespace;

Then add

private Galil.Galil g = new Galil.GalilClass();
private RIO_DataRecord r;

private const int RIO_ANALOGIO_CHANNELS = 8;

inside the public class Form1  above the public Form1()

The code should look something like this:

Add the following code to the Form1_Load():

try
{
g.address = "";  //open connection Dialog.
label1.Text = label1.Text + g.connection() + "\r\n";
label1.Text = label1.Text + g.commandValue("MG TIME").ToString();

r = new RIO_DataRecord(ref g);
r.AnIn = new ushort[RIO_ANALOGIO_CHANNELS];
r.AnOut = new ushort[RIO_ANALOGIO_CHANNELS];

}
catch (System.Runtime.InteropServices.COMException ex)
{
label1.Text = label1.Text + ex.Message.Trim() + "\r\n";
}
Double click on the button and add the following code to the button click
r.GetRecord(g);
textBox1.Text = r.SampleNumber.ToString();

Save your project.  It should look something like this:

Run it and it should prompt you with a connection dialog.  Click on the controller and hit Connect:

Click on the "Get Sample Time" button to get the current sample time using the RIO_DataRecord class.

That has successfully used the class to grab information out of the RIO data record.  An optional step is to then add another label to the form and include the following code in the button click in order to display all of the RIO data record information:

string str="", AO="",AI="";
str="Header bytes in Hex = "+r.Header.ToString("X")+"\r\n"+
"Sample Number = "+r.SampleNumber.ToString()+"\r\n"+
"Error Code = "+ r.ErrorCode.ToString()+"\r\n"+
"General Status = " + r.GenSatus.ToString()+"\r\n";
for (int i = 0; i < 7; i++)
{
AO=AO+r.AnOut[i].ToString()+"  ";
}

str=str+"Analog Outputs = "+AO+"\r\n";
for (int i = 0; i < 7; i++)
{
AI=AI+r.AnIn[i].ToString()+"  ";
}
str=str+"Analog Inputs = "+AI+"\r\n";
str=str+ "Digital Outputs = "+ r.DOut.ToString()+"\r\n"+
"Digital Inputs = " + r.DIn.ToString()+"\r\n"+
"Pulse Counter = " + r.PulseCount.ToString()+"\r\n"+
"ZC Data = "+ r.ZcData.ToString()+"\r\n"+
"ZD Data = "+r.ZdData.ToString();
label2.Text = str;

The result will look something like this:

The last thing to do is to use the ZC property that was created in the class.   You simply need to add the following code at the top of the button click:

r.ZC = "12";

That will send a value of 12 to the ZC command and the ZC Data in the data record should also reflect the change.

That completes the application code showing how to interface to a custom class from a C# application.  Creating a custom class allows the user to expand the functionality of the GalilTools API and gives the programmer the ability to create routines that can optimize code.  Contact a Galil Applications Engineer for more info on writing custom classes and interfacing them to a .NET application.