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

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,并且输出一些信号, 看是否有效果

相关推荐
新手unity自用笔记1 小时前
项目-坦克大战学习-子弹的移动与销毁
笔记·学习·c#
qinzechen2 小时前
分享几个做题网站------学习网------工具网;
java·c语言·c++·python·c#
yufei-coder6 小时前
C# Windows 窗体开发基础
vscode·microsoft·c#·visual studio
dangoxiba6 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
AitTech6 小时前
深入理解C#中的TimeSpan结构体:创建、访问、计算与格式化
开发语言·数据库·c#
hiyo58510 小时前
C#中虚函数和抽象函数的概念
开发语言·c#
开心工作室_kaic12 小时前
基于微信小程序的校园失物招领系统的设计与实现(论文+源码)_kaic
c语言·javascript·数据库·vue.js·c#·旅游·actionscript
时光追逐者16 小时前
WaterCloud:一套基于.NET 8.0 + LayUI的快速开发框架,完全开源免费!
前端·microsoft·开源·c#·.net·layui·.netcore
friklogff17 小时前
【C#生态园】打造现代化跨平台应用:深度解析.NET桌面应用工具
开发语言·c#·.net
hiyo5851 天前
C#的面向对象
开发语言·c#