【C#】初始化三菱运动卡

  1. 添加三菱dll引用

  2. WPF实现
    2.1、方法一:引用AxInterop.ActUtlTypeLib.dll实现
    添加WindowsFormsIntegration 引用

    初始化代码

//解决因为第三方控件报错,将实例化的对象添加到控件合集中
System.Windows.Controls.Grid CTSGrid = new System.Windows.Controls.Grid();
System.Windows.Forms.Integration.WindowsFormsHost host = null;
host = new System.Windows.Forms.Integration.WindowsFormsHost();
host.Child = actUtlTypeLib.AxActUtlType;
((ISupportInitialize)actUtlTypeLib.AxActUtlType).BeginInit();
CTSGrid.Children.Add(host);
((ISupportInitialize)actUtlTypeLib.AxActUtlType).EndInit();


2.2、方法二:引用Interop.ActUtlTypeLib.dll实现
初始化

ActUtlType AxActUtlType= new ActUtlType();
/// 打开线路连接
/// </summary>
/// <param name="stationNumber"></param>
/// <returns></returns>
public bool Connect(int stationNumber)
{
 AxActUtlType.ActLogicalStationNumber = stationNumber;
 if (0 == AxActUtlType.Connect())
 {
     IsConnected = true;
     return true;
 }
 return false;
}

/// <summary>
/// 关闭线路连接
/// </summary>
/// <returns></returns>
public int Disconnect()
{
 IsConnected = false;
 return AxActUtlType.Disconnect();
}
/// <summary>
/// 读设备数据
/// </summary>
/// <param name="deviceName"></param>
/// <param name="size"></param>
/// <param name="oInt"></param>
/// <returns></returns>
public bool Reader(string deviceName, int size, out int[] oInt)
{
   oInt = new int[size];
   if (Reader(deviceName, size * 2, out short[] oInt2))
   {
       for (int i = 0; i < size; i++)
       {
           oInt[i] = (oInt2[i * 2 + 1] << 16) + oInt2[i * 2];
       }

       return true;
   }

   return false;
}
  1. WINFROM实现
    初始化代码
//解决因为第三方控件报错,将实例化的对象添加到控件合集中
 ((ISupportInitialize)(this.axActUtlType1)).BeginInit();
 this.Controls.Add(axActUtlType1);
 ((ISupportInitialize)(this.axActUtlType1)).EndInit();

参考文献
https://www.cnblogs.com/ldl-david/p/5311848.html

posted @ 2024-07-06 09:36  qiutian-hao  阅读(39)  评论(0编辑  收藏  举报