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

相关推荐
我是唐青枫15 分钟前
C#.NET Consul + Steeltoe 深入解析:服务注册发现、健康检查与微服务接入
c#·.net·consul
波波00728 分钟前
用微软AutoGen+ 通义千问实现 AI 成语接龙
人工智能·microsoft·c#
csdn_aspnet11 小时前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
武藤一雄16 小时前
C# 设计模式大全(第一弹|7种)
microsoft·设计模式·微软·c#·.net·.netcore
格林威17 小时前
Baumer相机锂电池极片裁切毛刺检测:防止内部短路的 5 个核心方法,附 OpenCV+Halcon 实战代码!
开发语言·人工智能·数码相机·opencv·计算机视觉·c#·视觉检测
向上的车轮17 小时前
熟悉C#如何转TypeScript——SDK与包引用
开发语言·typescript·c#
CSharp精选营19 小时前
Dispose 不释放?C# 资源泄漏的 3 种隐蔽场景排查
c#·资源泄漏
unicrom_深圳市由你创科技20 小时前
LabVIEW和C#在工业控制中的应用差异是什么?
fpga开发·c#·labview
唐青枫21 小时前
C#.NET Consul + Steeltoe 深入解析:服务注册发现、健康检查与微服务接入
c#·.net
DowneyJoy1 天前
【Unity3D补充知识点】常用数据结构分析-集合(List<T>)
数据结构·unity·c#·list