当前位置: 首页> 教育> 培训 > 2.5 C#视觉程序开发实例1----设计一个IO_Manager

2.5 C#视觉程序开发实例1----设计一个IO_Manager

时间:2025/7/11 15:10:44来源:https://blog.csdn.net/weixin_42490025/article/details/140189727 浏览次数:0次

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
 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[] 数组,用来存放输入信号和输出信号
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函数实现
public  int  Open_Server()
{return    PInvoker.Create_Server();
}
3.2.2 Close_Server函数实现
public void Close_Server()
{PInvoker.ReleaseMMF();
}
3.2.3 IO输入状态的更新读取
 /// <summary>
/// Read  IO /// </summary>/// <returns></returns>public int Read(){int nRet = 0;try{// DM8  ReadPInvoker.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输出状态更新到 共享内存服务器中
public int Write()
{int nRet = 0;try{// DM8  WritePInvoker.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(),并且引用它
/// <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()中使用它

public Form_vision()
{//以前存在的代码........//打开资源Open_Resources(); 
// 创建线程CreateThread(); timer1.Interval = 100;timer1.Start(); 
}
4.2 创建一个Colsoe_Resoures(),并且引用它
 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
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控件,用于状态显示
private void timer1_Tick(object sender, EventArgs e)
{ iO_Monitors1.update_status(true,true);
}
4.5 接下来,我们可以运行测试下效果了

先运行Form_vision.exe
在这里插入图片描述

然后打开之前课程中的IO_Clientd.exe,并且输出一些信号, 看是否有效果
在这里插入图片描述

关键字:2.5 C#视觉程序开发实例1----设计一个IO_Manager

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: