2.5 C#视觉程序开发实例1----设计一个IO_Manager
第一步目标:
1 能够实现程序切换
2 实现获取IO触发信号Trig0
3 图像处理后能够输出一个脉冲
1 IO 引脚定义
1.1 输入信号定义
1.2 输出信号定义
2 IO时序图
2.1 触发时序
2.2 切换程序时序图
3 IO_Manager.cs
我们创建一个BD_Vision_Utility的帮助类, 实现一些资源的统一管理
3.1 导入上面课程中BD_SharedIOServerd.dll
csharp
public class PInvoker
{
public const string DllExtern = "BD_SharedIOServerd.dll";
public const string Version = "400";
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
public static extern void ReleaseMMF();
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
public static extern int Create_Server();
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
// DM 区
public static extern int SetDM8(byte[] dm8, int start, int len);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
public static extern int GetDM8(byte[] dm8, int start, int len);
}
3.2 创建 IO_Manager.cs
3.2.0 创建两个bool[] 数组,用来存放输入信号和输出信号
csharp
public class IO_Manager
{
static int count_DM8 = 40;
// 输出输出
public bool[] Inputs = new bool[count_DM8];
public bool[] Outputs = new bool[count_DM8];
}
3.2.1 Open_Server函数实现
csharp
public int Open_Server()
{
return PInvoker.Create_Server();
}
3.2.2 Close_Server函数实现
csharp
public void Close_Server()
{
PInvoker.ReleaseMMF();
}
3.2.3 IO输入状态的更新读取
csharp
/// <summary>
/// Read IO
/// </summary>
/// <returns></returns>
public int Read()
{
int nRet = 0;
try
{
// DM8 Read
PInvoker.GetDM8(array_dm8_in, 0, count_DM8);
for (int i = 0; i < count_DM8; i++)
{
if (array_dm8_in[i] > 0) Inputs[i] = true;
else Inputs[i] = false;
}
GC.KeepAlive(array_dm8_in);
}
catch (Exception ex)
{
nRet = -2;
}
return nRet;
}
3.2.3 IO输出状态更新到 共享内存服务器中
csharp
public int Write()
{
int nRet = 0;
try
{
// DM8 Write
PInvoker.SetDM8(array_dm8_out, count_DM8, count_DM8);
for (int i = 0; i < count_DM8; i++)
{
if (Outputs[i]) array_dm8_out[i] = 1;
else array_dm8_out[i] = 0;
}
GC.KeepAlive(array_dm8_out);
}
catch (Exception ex)
{
nRet = -2;
}
return nRet;
}
4 我们来实现在Form_Vision中 IO状态的显示
4.0 添加BD_Vision_Utility.dll
4.1 创建一个Open_Resoures(),并且引用它
csharp
/// <summary>
/// Open_Resources
/// </summary>
private void Open_Resources()
{
ContextManager.get_IOCtx().Open_Server();
//IO_Monitor和 IO_Ctx中inputs |outputs关联
iO_Monitors1.Init(ref ContextManager.get_IOCtx().Inputs, ref ContextManager.get_IOCtx().Outputs);
}
在 Form_vision()中使用它
csharp
public Form_vision()
{
//以前存在的代码........
//打开资源
Open_Resources();
// 创建线程
CreateThread();
timer1.Interval = 100;
timer1.Start();
}
4.2 创建一个Colsoe_Resoures(),并且引用它
csharp
private void Close_Resources()
{
ContextManager.get_IOCtx().Close_Server();
}
//在程序退出时,关闭资源
private void Form_vision_FormClosing(object sender, FormClosingEventArgs e)
{
StopThread();
Task.WaitAll();
Close_Resources();
}
4.3 IO_Circle() 中添加代码,更新inputs |outputs
csharp
private void IO_Circle()
{
while (true)
{
// readInputs
// writeOutputs
System.Threading.Thread.Sleep(20);
ContextManager.get_IOCtx().Read();
ContextManager.get_IOCtx().Write();
if (StopProgramEvent.WaitOne(0, true)) break;
}//end while
}
4.4 Timer1_Ticker中 更新IO_Monitor控件,用于状态显示
csharp
private void timer1_Tick(object sender, EventArgs e)
{
iO_Monitors1.update_status(true,true);
}
4.5 接下来,我们可以运行测试下效果了
先运行Form_vision.exe
然后打开之前课程中的IO_Clientd.exe,并且输出一些信号, 看是否有效果