前言
因成本节约,需要省去新增plc。故使用海康VC3000工控机以GPIO通讯方式实现。
一、前期准备
1.VC3000工控机IO通讯线(焊接接线端子,以便使用)。
2.端子板(没有找到,拿纸壳整的不太好看)。
3.连接线、开关和LED灯(没有灯,拿按钮灯代替)。
二、程序如下
1.类代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace NsIOControllerSDK
{
class CIOControllerSDK
{
public const Int32 MV_OK = unchecked((Int32)0x00000000);
public const Int32 MV_E_HANDLE = unchecked((Int32)0x80000000);
public const Int32 MV_E_SUPPORT = unchecked((Int32)0x80000001);
public const Int32 MV_E_BUFOVER = unchecked((Int32)0x80000002);
public const Int32 MV_E_CALLORDER = unchecked((Int32)0x80000003);
public const Int32 MV_E_PARAMETER = unchecked((Int32)0x80000004);
public const Int32 MV_E_RESOURCE = unchecked((Int32)0x80000006);
public const Int32 MV_E_NODATA = unchecked((Int32)0x80000007);
public const Int32 MV_E_PRECONDITION = unchecked((Int32)0x80000008);
public const Int32 MV_E_VERSION = unchecked((Int32)0x80000009);
public const Int32 MV_E_NOENOUGH_BUF = unchecked((Int32)0x8000000A);
public const Int32 MV_E_MATCH = unchecked((Int32)0x8000000B);
public const Int32 MV_E_UPDATING = unchecked((Int32)0x8000000C);
public const Int32 MV_E_UNKNOW = unchecked((Int32)0x800000FF);
public const Int32 MV_E_GC_GENERIC = unchecked((Int32)0x80000100);
public const Int32 MV_E_GC_ARGUMENT = unchecked((Int32)0x80000101);
public const Int32 MV_E_GC_RANGE = unchecked((Int32)0x80000102);
public const Int32 MV_E_GC_PROPERTY = unchecked((Int32)0x80000103);
public const Int32 MV_E_GC_RUNTIME = unchecked((Int32)0x80000104);
public const Int32 MV_E_GC_LOGICAL = unchecked((Int32)0x80000105);
public const Int32 MV_E_GC_ACCESS = unchecked((Int32)0x80000106);
public const Int32 MV_E_GC_TIMEOUT = unchecked((Int32)0x80000107);
public const Int32 MV_E_GC_DYNAMICCAST = unchecked((Int32)0x80000108);
public const Int32 MV_E_GC_UNKNOW = unchecked((Int32)0x800001FF);
public const Int32 MV_E_NOT_IMPLEMENTED = unchecked((Int32)0x80000200);
public const Int32 MV_E_INVALID_ADDRESS = unchecked((Int32)0x80000201);
public const Int32 MV_E_WRITE_PROTECT = unchecked((Int32)0x80000202);
public const Int32 MV_E_ACCESS_DENIED = unchecked((Int32)0x80000203);
public const Int32 MV_E_BUSY = unchecked((Int32)0x80000204);
public const Int32 MV_E_PACKET = unchecked((Int32)0x80000205);
public const Int32 MV_E_NETER = unchecked((Int32)0x80000206);
public const Int32 MV_E_USB_READ = unchecked((Int32)0x80000300);
public const Int32 MV_E_USB_WRITE = unchecked((Int32)0x80000301);
public const Int32 MV_E_USB_DEVICE = unchecked((Int32)0x80000302);
public const Int32 MV_E_USB_GENICAM = unchecked((Int32)0x80000303);
public const Int32 MV_E_USB_BANDWIDTH = unchecked((Int32)0x80000304);
public const Int32 MV_E_USB_UNKNOW = unchecked((Int32)0x800003FF);
public const Int32 MV_E_UPG_FILE_MISMATCH = unchecked((Int32)0x80000400);
public const Int32 MV_E_UPG_LANGUSGE_MISMATCH = unchecked((Int32)0x80000401);
public const Int32 MV_E_UPG_CONFLICT = unchecked((Int32)0x80000402);
public const Int32 MV_E_UPG_INNER_ERR = unchecked((Int32)0x80000403);
public const Int32 MV_E_UPG_UNKNOW = unchecked((Int32)0x800004FF);
public const int nMaxDeviceNum = 32;
public static IntPtr[] m_hDeviceHandleList = new IntPtr[nMaxDeviceNum];
public static string[] m_hDeviceTypeList = new string[nMaxDeviceNum];
public static int nComIndex = 0;
public enum MV_IO_PATTERN_OUT
{
MV_IO_PATTERN_PWM = 0x05,
MV_IO_PATTERN_SINGLE = 0x06,
};
public enum MV_GIO_PATTERN_OUT
{
MV_GIO_PATTERN_SINGLE = 0x00,
MV_GIO_PATTERN_PWM = 0x01,
};
public enum MV_IO_PORT_NUMBER
{
MV_IO_PORT_1 = 0x1,
MV_IO_PORT_2 = 0x2,
MV_IO_PORT_3 = 0x4,
MV_IO_PORT_4 = 0x8,
MV_IO_PORT_5 = 0x10,
MV_IO_PORT_6 = 0x20,
MV_IO_PORT_7 = 0x40,
MV_IO_PORT_8 = 0x80,
};
public enum MV_IO_MAINPORT_NUMBER
{
MV_MAINIO_PORT_1 = 0x1,
MV_MAINIO_PORT_2 = 0x2,
MV_MAINIO_PORT_3 = 0x4,
MV_MAINIO_PORT_4 = 0x8,
MV_MAINIO_PORT_5 = 0x10,
MV_MAINIO_PORT_6 = 0x20,
MV_MAINIO_PORT_7 = 0x40,
MV_MAINIO_PORT_8 = 0x80,
};
public enum MV_GIO_LEVEL
{
MV_GIO_LEVEL_LOW = 0x0000,
MV_GIO_LEVEL_HIGH = 0x0001,
}
public enum MV_IO_PNP_TYPE
{
MV_IO_ENABLE_PNP = 0x01,
MV_IO_ENABLE_NPN = 0x02,
};
public enum MV_IO_EDGE_TYPE
{
MV_IO_EDGE_RISING = 0x01,
MV_IO_EDGE_FALLING = 0x02,
};
public enum MV_IO_ENABLE_TYPE
{
MV_IO_ENABLE_START = 0x00,
MV_IO_ENABLE_END = 0x01,
};
public enum MV_IO_LEVEL
{
MV_IO_LEVEL_LOW = 0x0000,
MV_IO_LEVEL_HIGH = 0x0001,
};
public enum COM_NUMBER
{
RS232 = 0x00,
RS485 = 0x01,
RS422 = 0x02,
};
public enum MV_IO_LIGHTSTATE
{
MV_IO_LIGHTSTATE_ON = 0x0001,
MV_IO_LIGHTSTATE_OFF = 0x0002,
};
public enum MV_IO_EDGE
{
MV_IO_EDGE_RISING = 0x01,
MV_IO_EDGE_DOWN = 0x02,
};
public enum MV_IO_EDGE_NOTICE_STATE
{
MV_IO_EDGE_NOTICE_DISABLE = 0x00,
MV_IO_EDGE_NOTICE_ENABLE = 0x01,
};
public struct MV_IO_SERIAL
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public String strComName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public UInt32[] nReserved;
};
public struct MV_IO_OUTPUT_ENABLE
{
public Byte nPortNumber;
public Byte nType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public UInt32[] nReserved;
} ;
// 沿检测参数
public struct MV_IO_INPUT_EDGE_TYPE
{
public Byte nPortNumber;
public UInt16 nTriggerTimes;
public MV_IO_EDGE_TYPE enEdgeType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public UInt32[] nReserved;
};
public struct MV_IO_LIGHT_PARAM
{
public Byte nPortNumber;
public UInt16 nLightValue;
public UInt16 nLightState;
public UInt16 nLightEdge;
public UInt16 nDurationTime;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public UInt32[] nReserved;
};
public struct MV_IO_INPUT_LEVEL
{
public Byte nPortNumber;
public Byte nLevel0;
public Byte nLevel1;
public Byte nLevel2;
public Byte nLevel3;
public Byte nLevel4;
public Byte nLevel5;
public Byte nLevel6;
public Byte nLevel7;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public UInt32[] nReserved;
};
public struct MV_IO_VERSION
{
public UInt32 nMainVersion;
public UInt32 nSubVersion;
public UInt32 nModifyVersion;
public UInt32 nYear;
public UInt32 nMonth;
public UInt32 nDay;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public UInt32[] nReserved;
};
public struct MV_IO_SET_INPUT
{
public UInt32 nPort;
public UInt32 nEnable;
public UInt32 nEdge;
public UInt32 nDelayTime;
public UInt32 nGlitch;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public UInt32[] nReserved;
};
public struct MV_IO_SET_OUTPUT
{
public UInt32 nPort;
public UInt32 nType;
public UInt32 nPulseWidth;
public UInt32 nPulsePeriod;
public UInt32 nDurationTime;
public UInt32 nValidLevel;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public UInt32[] nReserved;
}
public struct MV_RS_CONFIG
{
public UInt32 nCOM1Type;
public UInt32 nCOM2Type;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public UInt32[] nReserved;
}
public struct MV_IO_ASSOCIATEPORT_PARAM
{
public UInt16 nInPortNum;
public UInt16 nOutPortNum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public UInt32[] nReserved;
}
public struct MV_IO_LOCAL_UPGRADE
{
public UInt32 nProcPercent;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public String strProcState;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public UInt32[] nReserved;
}
public delegate void cbOutputdelegate(IntPtr handle, ref MV_IO_INPUT_EDGE_TYPE stParam, IntPtr pUser);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_SetDebugView")]
private static extern void MV_IO_SetDebugView(Int32 nFlag);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_CreateHandle")]
private static extern Int32 MV_IO_CreateHandle(ref IntPtr handle);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_DestroyHandle")]
private static extern Int32 MV_IO_DestroyHandle(IntPtr handle);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_GetSDKVersion")]
private static extern Int32 MV_IO_GetSDKVersion(ref MV_IO_VERSION pstVersion);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_Open")]
private static extern Int32 MV_IO_Open(IntPtr handle, ref MV_IO_SERIAL pstSerial);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_Close")]
private static extern void MV_IO_Close(IntPtr handle);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_SetLightParam")]
private static extern Int32 MV_IO_SetLightParam(IntPtr handle, ref MV_IO_LIGHT_PARAM pstLightParam);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_GetInputLevel")]
private static extern Int32 MV_IO_GetInputLevel(IntPtr handle, ref MV_IO_INPUT_LEVEL pstInputLevel);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_WinIO_Init")]
private static extern Int32 MV_IO_WinIO_Init();
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_WinIO_DeInit")]
private static extern Int32 MV_IO_WinIO_DeInit();
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_MainIOInit")]
private static extern Int32 MV_IO_MainIOInit();
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_GetMainInputLevel")]
private static extern Int32 MV_IO_GetMainInputLevel(ref byte nStatus);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_SetMainGPO_NPN")]
private static extern Int32 MV_IO_SetMainGPO_NPN(Int32 nEnable);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_SetMainOutputLevel")]
private static extern Int32 MV_IO_SetMainOutputLevel(MV_IO_MAINPORT_NUMBER nPortIn, MV_GIO_LEVEL nStatus);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_SetRSConfig")]
private static extern Int32 MV_IO_SetRSConfig(ref MV_RS_CONFIG stParam);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_GetFirmwareVersion")]
private static extern Int32 MV_IO_GetFirmwareVersion(IntPtr handle, ref MV_IO_VERSION pstVersion);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_LocalUpgrade")]
private static extern Int32 MV_IO_LocalUpgrade(IntPtr handle, byte[] pData, uint nLen, ref MV_IO_LOCAL_UPGRADE pstLocalUpgrade);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_GetPortInputParam")]
private static extern Int32 MV_IO_GetPortInputParam(IntPtr handle, ref MV_IO_SET_INPUT stParam);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_GetPortOutputParam")]
private static extern Int32 MV_IO_GetPortOutputParam(IntPtr handle, ref MV_IO_SET_OUTPUT stParam);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_SetInput")]
private static extern Int32 MV_IO_SetInput(IntPtr handle, ref MV_IO_SET_INPUT stParam);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_SetOutput")]
private static extern Int32 MV_IO_SetOutput(IntPtr handle, ref MV_IO_SET_OUTPUT stParam);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_AssociatedOutPort")]
private static extern Int32 MV_IO_AssociatedOutPort(IntPtr handle, ref MV_IO_ASSOCIATEPORT_PARAM stParam);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_SetOutputEnable")]
private static extern Int32 MV_IO_SetOutputEnable(IntPtr handle, ref MV_IO_OUTPUT_ENABLE stParam);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_ExcutePNPEnable")]
private static extern Int32 MV_IO_ExcutePNPEnable(IntPtr handle, Int32 nType);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_RegisterEdgeDetectionCallBack")]
private static extern Int32 MV_IO_RegisterEdgeDetectionCallBack(IntPtr handle, cbOutputdelegate cbOutput, IntPtr pUser);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_ResetParam")]
private static extern Int32 MV_IO_ResetParam(IntPtr handle);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_Reboot")]
private static extern Int32 MV_IO_Reboot(IntPtr handle);
[DllImport("MvIOInterfaceBox.dll", EntryPoint = "MV_IO_GetLightParam")]
private static extern Int32 MV_IO_GetLightParam(IntPtr handle, ref MV_IO_LIGHT_PARAM pstLightParam);
public static void MV_IO_SetDebugView_CS(Int32 nFlag)
{
MV_IO_SetDebugView(nFlag);
}
public static Int32 MV_IO_CreateHandle_CS(ref IntPtr handle)
{
return MV_IO_CreateHandle(ref handle);
}
public static Int32 MV_IO_DestroyHandle_CS(IntPtr handle)
{
return MV_IO_DestroyHandle(handle);
}
public static Int32 MV_IO_GetSDKVersion_CS(ref MV_IO_VERSION pstVersion)
{
return MV_IO_GetSDKVersion(ref pstVersion);
}
public static Int32 MV_IO_Open_CS(IntPtr handle, ref MV_IO_SERIAL pstSerial)
{
return MV_IO_Open(handle, ref pstSerial);
}
public static Int32 MV_IO_SetLightParam_CS(IntPtr handle, ref MV_IO_LIGHT_PARAM pstLightParam)
{
return MV_IO_SetLightParam(handle, ref pstLightParam);
}
public static void MV_IO_Close_CS(IntPtr handle)
{
MV_IO_Close(handle);
}
public static Int32 MV_IO_GetInputLevel_CS(IntPtr handle, ref MV_IO_INPUT_LEVEL pstInputLevel)
{
return MV_IO_GetInputLevel(handle, ref pstInputLevel);
}
public static Int32 MV_IO_WinIO_Init_CS()
{
return MV_IO_WinIO_Init();
}
public static Int32 MV_IO_WinIO_DeInit_CS()
{
return MV_IO_WinIO_DeInit();
}
public static Int32 MV_IO_MainIOInit_CS()
{
return MV_IO_MainIOInit();
}
public static Int32 MV_IO_GetMainInputLevel_CS(ref byte nStatus)
{
return MV_IO_GetMainInputLevel(ref nStatus);
}
public static Int32 MV_IO_SetMainGPO_NPN_CS(Int32 nEnable)
{
return MV_IO_SetMainGPO_NPN(nEnable);
}
public static Int32 MV_IO_SetMainOutputLevel_CS(MV_IO_MAINPORT_NUMBER nPortIn, MV_GIO_LEVEL nStatus)
{
return MV_IO_SetMainOutputLevel( nPortIn, nStatus);
}
public static Int32 MV_IO_SetRSConfig_CS(ref MV_RS_CONFIG stParam)
{
return MV_IO_SetRSConfig(ref stParam);
}
public static Int32 MV_IO_GetFirmwareVersion_CS(IntPtr handle, ref MV_IO_VERSION pstVersion)
{
return MV_IO_GetFirmwareVersion(handle, ref pstVersion);
}
public static Int32 MV_IO_LocalUpgrade_CS(IntPtr handle, Byte[] pData, uint nLen, ref MV_IO_LOCAL_UPGRADE pstLocalUpgrade)
{
return MV_IO_LocalUpgrade(handle, pData, nLen, ref pstLocalUpgrade);
}
public static Int32 MV_IO_GetPortInputParam_CS(IntPtr handle, ref MV_IO_SET_INPUT stParam)
{
return MV_IO_GetPortInputParam(handle, ref stParam);
}
public static Int32 MV_IO_GetPortOutputParam_CS(IntPtr handle, ref MV_IO_SET_OUTPUT stParam)
{
return MV_IO_GetPortOutputParam(handle, ref stParam);
}
public static Int32 MV_IO_SetInput_CS(IntPtr handle, ref MV_IO_SET_INPUT stParam)
{
return MV_IO_SetInput(handle, ref stParam);
}
public static Int32 MV_IO_SetOutput_CS(IntPtr handle, ref MV_IO_SET_OUTPUT stParam)
{
return MV_IO_SetOutput(handle, ref stParam);
}
public static Int32 MV_IO_SetOutputEnable_CS(IntPtr handle, ref MV_IO_OUTPUT_ENABLE stParam)
{
return MV_IO_SetOutputEnable(handle, ref stParam);
}
public static Int32 MV_IO_ExcutePNPEnable_CS(IntPtr handle, Int32 nType)
{
return MV_IO_ExcutePNPEnable(handle, nType);
}
public static Int32 MV_IO_AssociatedOutPort_CS(IntPtr handle, ref MV_IO_ASSOCIATEPORT_PARAM stParam)
{
return MV_IO_AssociatedOutPort(handle, ref stParam);
}
public static Int32 MV_IO_RegisterEdgeDetectionCallBack_CS(IntPtr handle, cbOutputdelegate cbOutput, IntPtr pUser)
{
return MV_IO_RegisterEdgeDetectionCallBack(handle, cbOutput, pUser);
}
public static Int32 MV_IO_ResetParam_CS(IntPtr handle)
{
return MV_IO_ResetParam(handle);
}
public static Int32 MV_IO_Reboot_CS(IntPtr handle)
{
return MV_IO_Reboot(handle);
}
public static Int32 MV_IO_GetLightParam_CS(IntPtr handle, ref MV_IO_LIGHT_PARAM pstLightParam)
{
return MV_IO_GetLightParam(handle, ref pstLightParam);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace HRMyTool
{
static public class MyTool
{
static public int GetCtrlsCount(Form frm)
{
int count = 0;
foreach (Control ctrl in frm.Controls)
{
count++;
if (ctrl.Controls.Count > 0)
{
count += GetCtrlsCount(ctrl);
}
}
return count;
}
static public int GetCtrlsCount(Control ctrl)
{
int count = 0;
foreach (Control subctrl in ctrl.Controls)
{
count++;
if (subctrl.Controls.Count > 0)
{
count += GetCtrlsCount(subctrl);
}
}
return count;
}
static public List<Control> GetCtrls(Form frm)
{
List<Control> ctrls = new List<Control>();
foreach (Control ctrl in frm.Controls)
{
ctrls.Add(ctrl);
if (ctrl.Controls.Count > 0)
{
ctrls.AddRange(GetCtrls(ctrl));
}
}
return ctrls;
}
static public List<Control> GetCtrls(Control ctrl)
{
List<Control> ctrls = new List<Control>();
foreach (Control subctrl in ctrl.Controls)
{
ctrls.Add(subctrl);
if (subctrl.Controls.Count > 0)
{
ctrls.AddRange(GetCtrls(subctrl));
}
}
return ctrls;
}
[DllImport("winmm")]
static extern uint timeGetTime();
[DllImport("winmm")]
static extern void timeBeginPeriod(int t);
[DllImport("winmm")]
static extern void timeEndPeriod(int t);
}
}
2.主函数代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HRMyTool;
using System.Threading;
using System.Runtime.InteropServices;
using NsIOControllerSDK;
using System.Threading;
namespace IOController
{
public partial class IOController : Form
{
public delegate void Action();
// NPN/PNP State definition
public enum PNP_ENABLE_STATE
{
PNP = 0x01,
NPN = 0x02,
};
public IOController()
{
InitializeComponent();
Environment.ExitCode = 1;
int nRet = CIOControllerSDK.MV_OK;
nRet = CIOControllerSDK.MV_IO_WinIO_Init_CS();
if (nRet != CIOControllerSDK.MV_OK)
{
// LOG("win IO 初始化失败");
CIOControllerSDK.MV_IO_WinIO_DeInit_CS();
}
else
{
// LOG("Win IO 初始化成功");
}
int count = MyTool.GetCtrlsCount(this) * 2 + 2;
float[] factor = new float[count];
int i = 0;
factor[i++] = Size.Width;
factor[i++] = Size.Height;
List<Control> ctrls = MyTool.GetCtrls(this);
foreach (Control ctrl in ctrls)
{
factor[i++] = ctrl.Location.X / (float)Size.Width;
factor[i++] = ctrl.Location.Y / (float)Size.Height;
ctrl.Tag = ctrl.Size;
}
Tag = factor;
}
private void PrintMessage(string strMessage)
{
string strMsg = "[" + DateTime.Now + "] " + strMessage + "\r\n";
rtbMessage.AppendText(strMsg);
}
private void LOG(string strMessage)
{
this.Invoke(new Action(() => { PrintMessage(strMessage); }));
}
private void btnClearMessage_Click(object sender, EventArgs e)
{
rtbMessage.Clear();
rtbMessage.Refresh();
}
private void btnSaveMessage_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.RestoreDirectory = true;
sfd.Filter = "Text|*.txt|*.ALL|*.*";
sfd.FileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
foreach (string s in rtbMessage.Lines) sw.WriteLine(s);
sw.Close();
fs.Close();
}
sfd.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void VisionController_Resize(object sender, EventArgs e)
{
float[] scale = (float[])Tag;
if (null == scale)
{
return;
}
int i = 2;
List<Control> ctrls = MyTool.GetCtrls(this);
foreach (Control ctrl in ctrls)
{
ctrl.Left = (int)(Size.Width * scale[i++]);
ctrl.Top = (int)(Size.Height * scale[i++]);
ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
}
}
private void btGetGPI_Click(object sender, EventArgs e)
{
try
{
btGetGPI.Enabled = false;
GetMainGPIInputLevel();
btGetGPI.Enabled = true;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public int GetMainGPIInputLevel()
{
int nRet = CIOControllerSDK.MV_OK;
try
{
lblGPI1.BackColor = Color.White;
lblGPI2.BackColor = Color.White;
lblGPI3.BackColor = Color.White;
byte[] byteStatus = new byte[1024];
nRet = CIOControllerSDK.MV_IO_GetMainInputLevel_CS(ref byteStatus[0]); // 用字节数组接收动态库传过来的字符串
int nGPIStatus = BitConverter.ToInt32(byteStatus, 0);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Getting the electrical level status failed.");
goto MyExit;
}
if ((nGPIStatus & 0x01) == 0x01)
{
lblGPI1.BackColor = Color.Red;
}
else
{
lblGPI1.BackColor = Color.Green;
}
if ((nGPIStatus & 0x02) == 0x02)
{
lblGPI2.BackColor = Color.Red;
}
else
{
lblGPI2.BackColor = Color.Green;
}
if ((nGPIStatus & 0x04) == 0x04)
{
lblGPI3.BackColor = Color.Red;
}
else
{
lblGPI3.BackColor = Color.Green;
}
LOG("The status of the input electrical level is obtained.");
MyExit: ;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return nRet;
}
private void ckbDbgView_CheckedChanged(object sender, EventArgs e)
{
if (ckbDbgView.Checked)
{
CIOControllerSDK.MV_IO_SetDebugView_CS(1);
}
else
{
CIOControllerSDK.MV_IO_SetDebugView_CS(0);
}
}
private void btGPOLow_Click(object sender, EventArgs e)
{
int nRet = CIOControllerSDK.MV_OK;
bool bSelect = false;
try
{
if (ckbGPO1.Checked)
{
bSelect = true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_1;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO1 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO1.ForeColor = Color.Black;
}
else
{
ckbGPO1.ForeColor = Color.Green;
LOG("The command of GPO1 Low Level settings is sent.");
}
}
if (ckbGPO2.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_2;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO2 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO2.ForeColor = Color.Black;
}
else
{
ckbGPO2.ForeColor = Color.Green;
LOG("The command of GPO2 Low Level settings is sent.");
}
}
if (ckbGPO3.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_3;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO3 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO3.ForeColor = Color.Black;
}
else
{
ckbGPO3.ForeColor = Color.Green;
LOG("The command of GPO3 Low Level settings is sent.");
}
}
if (ckbGPO4.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_4;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO4 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO4.ForeColor = Color.Black;
}
else
{
ckbGPO4.ForeColor = Color.Green;
LOG("The command of GPO4 Low Level settings is sent.");
}
}
if (cbGPO5.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_5;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO5 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
cbGPO5.ForeColor = Color.Black;
}
else
{
cbGPO5.ForeColor = Color.Green;
LOG("The command of GPO5 Low Level settings is sent.");
}
}
if (ckbGPO6.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_6;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO6 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO6.ForeColor = Color.Black;
}
else
{
ckbGPO6.ForeColor = Color.Green;
LOG("The command of GPO6 Low Level settings is sent.");
}
}
if (ckbGPO7.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_7;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO7 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO7.ForeColor = Color.Black;
}
else
{
ckbGPO7.ForeColor = Color.Green;
LOG("The command of GPO7 Low Level settings is sent.");
}
}
if (ckbGPO8.Checked)
{
bSelect |= true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_8;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_LOW;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO8 Low Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO8.ForeColor = Color.Black;
}
else
{
ckbGPO8.ForeColor = Color.Green;
LOG("The command of GPO8 Low Level settings is sent.");
}
}
if (bSelect == false)
{
LOG("Disable failed.");
return;
}
}
catch (Exception)
{
LOG("Sending the command failed. Parameters or operations exception occurs.");
}
}
private void btGPOHigh_Click(object sender, EventArgs e)
{
int nRet = CIOControllerSDK.MV_OK;
bool bSelect = false;
try
{
if (ckbGPO1.Checked)
{
bSelect = true;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_1, CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO1 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO1.ForeColor = Color.Black;
}
else
{
ckbGPO1.ForeColor = Color.Red;
LOG("The command of GPO1 High Level settings is sent.");
}
}
if (ckbGPO2.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_2;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO2 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO2.ForeColor = Color.Black;
}
else
{
ckbGPO2.ForeColor = Color.Red;
LOG("The command of GPO2 High Level settings is sent.");
}
}
if (ckbGPO3.Checked)
{
bSelect = bSelect |true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_3;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO3 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO3.ForeColor = Color.Black;
}
else
{
ckbGPO3.ForeColor = Color.Red;
LOG("The command of GPO3 High Level settings is sent.");
}
}
if (ckbGPO4.Checked)
{
bSelect = bSelect |true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_4;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO4 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO4.ForeColor = Color.Black;
}
else
{
ckbGPO4.ForeColor = Color.Red;
LOG("The command of GPO4 High Level settings is sent.");
}
}
if (cbGPO5.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_5;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO5 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
cbGPO5.ForeColor = Color.Black;
}
else
{
cbGPO5.ForeColor = Color.Red;
LOG("The command of GPO5 High Level settings is sent.");
}
}
if (ckbGPO6.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_6;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO6 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO6.ForeColor = Color.Black;
}
else
{
ckbGPO6.ForeColor = Color.Red;
LOG("The command of GPO6 High Level settings is sent.");
}
}
if (ckbGPO7.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_7;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO7 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO7.ForeColor = Color.Black;
}
else
{
ckbGPO7.ForeColor = Color.Red;
LOG("The command of GPO7 High Level settings is sent.");
}
}
if (ckbGPO8.Checked)
{
bSelect = bSelect | true;
CIOControllerSDK.MV_IO_MAINPORT_NUMBER nPortIn = CIOControllerSDK.MV_IO_MAINPORT_NUMBER.MV_MAINIO_PORT_8;
CIOControllerSDK.MV_GIO_LEVEL nStatus = CIOControllerSDK.MV_GIO_LEVEL.MV_GIO_LEVEL_HIGH;
nRet = CIOControllerSDK.MV_IO_SetMainOutputLevel_CS(nPortIn, nStatus);
if (CIOControllerSDK.MV_OK != nRet)
{
LOG("Sending the command of GPO8 High Level settings failed. Error code:" + Convert.ToString(nRet, 16));
ckbGPO8.ForeColor = Color.Black;
}
else
{
ckbGPO8.ForeColor = Color.Red;
LOG("The command of GPO8 High Level settings is sent.");
}
}
if(bSelect == false)
{
LOG("Enable failed.");
return;
}
}
catch (System.Exception ex)
{
LOG("Sending the command failed. Parameters or operations exception occurs.");
}
}
}
}