C#
C# 1
From the Windows Start menu, open "CONTEC API-TOOL" → "WDM Configuration" (or simply "Configuration Tool").
Select a registered demo device (e.g., DIO000) and press the "Diagnostic" button.
A screen with a row of "Input (IN)" and "Output (OUT)" lamps and buttons will appear.
| using System;
using System.Windows.Forms;
// In some cases, you might need to add the "Cdio.cs" file to your project instead of a DLL.
using CdioCs;
namespace ContecController
{
public partial class Form1 : Form
{
short Id = -1; // Initialize with an invalid ID
// [Check Required] The device name registered in the Device Manager (API-TOOL configuration)
string DeviceName = "DIO000";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int ret;
// Device initialization
ret = Cdio.DioInit(DeviceName, out Id);
if (ret != 0) // Non-zero indicates an error
{
string errorMsg = "";
Cdio.DioGetErrorString(ret, out errorMsg); // Retrieve error details as a string
MessageBox.Show($"Initialization failed.\nError Code: {ret}\nDetail: {errorMsg}\n\nPlease check if the device name is correct.");
// Disable buttons if initialization fails
btnSignalOn.Enabled = false;
btnSignalOff.Enabled = false;
}
else
{
labelStatus.Text = "Ready";
}
}
// Signal ON Button
private void btnSignalOn_Click(object sender, EventArgs e)
{
if (Id == -1) return;
// Set Output Port 0, Bit 0 to 1 (ON)
int ret = Cdio.DioOutBit(Id, 0, 0, 1);
if (ret != 0) ShowError(ret);
else labelStatus.Text = "Port0-Bit0 : ON";
}
// Signal OFF Button
private void btnSignalOff_Click(object sender, EventArgs e)
{
if (Id == -1) return;
// Set Output Port 0, Bit 0 to 0 (OFF)
int ret = Cdio.DioOutBit(Id, 0, 0, 0);
if (ret != 0) ShowError(ret);
else labelStatus.Text = "Port0-Bit0 : OFF";
}
// Cleanup on exit
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (Id != -1)
{
// Release the device
Cdio.DioExit(Id);
}
}
// Method for displaying errors
private void ShowError(int errorCode)
{
string errorMsg = "";
Cdio.DioGetErrorString(errorCode, out errorMsg);
MessageBox.Show($"An error occurred.\nCode: {errorCode}\nDetail: {errorMsg}");
}
}
}
|