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

相关推荐
The Future is mine38 分钟前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
Iotfsd8 小时前
.NET写的开源工业物联网网关(IoTGateway)
物联网·c#·.net·dotnet·边缘网关·雾计算·工业物联网智能网关
先生沉默先8 小时前
c#接口_抽象类_多态学习
开发语言·学习·c#
江沉晚呤时8 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
iReachers9 小时前
使用命令行加密混淆C#程序
开发语言·c#
[太阳]889 小时前
Kafka命令行的使用/Spark-Streaming核心编程(二)
c#·linq
电商api接口开发1 天前
ASP.NET MVC 入门指南
c#·asp.net·mvc
我不是程序猿儿1 天前
[C#]反射的实战应用,实际数据模拟
开发语言·c#
爱编程的鱼1 天前
C# 结构(Struct)
开发语言·人工智能·算法·c#
是阿根1 天前
unity使用iTextSharp生成PDF文件
unity·c#·游戏引擎