自学内容网 自学内容网

2.5 C#视觉程序开发实例1----IO_Manager实现切换程序

2.5 C#视觉程序开发实例1----IO_Manager实现切换程序

1 IO_Manager中输入实现

1.0 IO_Manager中输入部分引脚定义
// 设定index 目的是为了今后可以配置这些参数、
// 输入引脚定义
private int index_trig0 = 0; // trig index
private int index_cst = 7; //cst index
private int[] index_ProNO = new int[8];// 程序号index
 for (int i = 0; i < 8; i++)
                index_ProNO[i] = 8 + i;
1.1 IO_Manager中获取Trig0 信号
public  bool get_Trig0()
{
    return Inputs[index_trig0];
}
1.2 IO_Manager中获取Pro_NO
public ushort get_ProgramNO()
{
    ushort Pro_no = 0;
    if (Inputs[index_ProNO[0]]) Pro_no += 1;
    if (Inputs[index_ProNO[1]]) Pro_no += 2;
    if (Inputs[index_ProNO[2]]) Pro_no += 4;
    if (Inputs[index_ProNO[3]]) Pro_no += 8;
    if (Inputs[index_ProNO[4]]) Pro_no += 16;
    if (Inputs[index_ProNO[5]]) Pro_no += 32;
    if (Inputs[index_ProNO[5]]) Pro_no += 64;
    if (Inputs[index_ProNO[6]]) Pro_no += 128;
    return Pro_no;
}
1.3 IO_Manager中获取Pro_CST程序切换信号
public bool get_CST()
{
    return Inputs[index_cst];
}

2 IO_Manager中输出实现

2.0 IO_Manager中输出部分引脚定义
// 输出引脚定义
 private int index_online = 0;// 联机信号
 private int index_sto = 1;// STO 输出
 private int index_ok = 2;// OK输出
 private int index_ng = 3;// NG输出
 private int index_CST_Acomm = 7;// 程序切换成功
2.1 IO_Manager中输出CST_AComm
public void Out_CST_AComm(bool state)
{
    lock (_mutex)
    {
        Outputs[index_CST_Acomm] = state;
    }
}
2.2 IO_Manager中输出Online信号
public void Out_Onlie(bool state)
{
    lock(_mutex)
    {
        Outputs[index_online] = state;
    }
}

3 Form_vision 主程序中添加代码实现程序号切换和相机触发

3.0 添加program_no 变量
/// <summary>
 /// 当前程序号
 /// </summary>
 public int program_No = 0
3.1 IO_Circle中实现程序号的切换和CST_Acom的返回
 private void IO_Circle()
{
  bool pro_cst_cur = false;
  bool pro_cst_last = false;
  while (true)
  { 
     // readInputs
     // writeOutputs 
      System.Threading.Thread.Sleep(20);
      ContextManager.get_IOCtx().Read();
      ContextManager.get_IOCtx().Write();
      //更新程序号
      // CST  Accom 信号  
      pro_cst_cur = ContextManager.get_IOCtx().get_CST();
      if (pro_cst_cur && !pro_cst_last)  //判断上升沿触发
      {
          program_No = ContextManager.get_IOCtx().get_ProgramNO();
          //toDO 代码,程序切换
          ContextManager.get_IOCtx().Out_CST_AComm(true); 
      } 
      pro_cst_last = pro_cst_cur;

      // CST_AComm 复位
      if(!pro_cst_cur)
          ContextManager.get_IOCtx().Out_CST_AComm(false);

      if (StopProgramEvent.WaitOne(0, true)) break;
  }//end while  
}
3.2 Timer_Ticker中实现当前程序号的更新显示
private void timer1_Tick(object sender, EventArgs e)
{
    /// 更新程序号到控件
    Util_Helper.Set_NumberControl_Value(num_programNO, program_No);

    iO_Monitors1.update_status(true,true);
}

4 效果展示

4.1 IO_Client.exe先准备好程序号31到端口上

在这里插入图片描述

4.2 IO_Client.exe 输出CST 信号

Vision 接受到CST上升沿后,立刻执行程序切换,并且输出CST_Acomm信号给IO_Client
在这里插入图片描述

4.2 IO_Client.exe 接收到CST_AComm信号后, 复位 CST信号 ,Vision也会复位CST_Acomm

在这里插入图片描述

5 下一章节 我们会重点讲获取一次相机采集信号和输出一个STO脉冲


原文地址:https://blog.csdn.net/weixin_42490025/article/details/140216112

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!