2017-06-22 44 views
3

我想使用TwinCat 3控制閥門和Visual Studio C#來處理想要在噴泉上顯示的圖像的自動圖形噴泉。通過ADS.Net從C#發送數組到TwinCat 3通過ADS.Net

圖像處理程序的最終形式是二進制陣列圖像(附件): Image Processing Result 1; Image Processing Result 2;

我想用圖像處理的最終形式來控制機器上的閥門(閥門在1時打開,當閥門爲0時閥門將關閉)。我對TwinCat 3非常新穎特別是與ADS庫。

來自infosys beckhoff的示例對我沒有什麼幫助,有人可以幫助我嗎?

謝謝

+0

你嘗試過什麼到目前爲止送呢?在infosys上的示例是有幫助的... –

+0

實際上,我正在嘗試將數組寫入TwinCat 3.在infosys上,有一個示例從twincat 3讀取數組,但沒有示例如何從twincat 3寫入數組。 –

+0

我發現示例9對我非常有幫助,現在我試圖弄清楚如何在我的項目中實現此示例。 –

回答

1

我做了在端口851連接到本地PLC和寫入100個布爾變量命名爲 「boolArray」 在MAIN TC3的陣列(3的TwinCAT)樣本控制檯程序:

using System; 
using TwinCAT.Ads; 
using System.Threading; 

namespace WriteArrayToPLC 
{ 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      TcAdsClient adsClient = new TcAdsClient(); 
      byte[] boolArray = new byte[100]; 
      // Fill array with 010101010... 
      for (int i = 0; i < 100; i++) 
      { 
       boolArray[i] = (i % 2 != 0) ? (byte)1 : (byte)0; 
      } 
      // Connect to PLC 
      try 
      { 

       if (adsClient != null) 
       { 
        Console.WriteLine("Connecting to PC"); 
        adsClient.Connect(851); 
       } 
      } 
      catch (Exception err) 
      { 
       Console.WriteLine(err.Message); 
       adsClient = null; 
      } 

      if (adsClient != null) 
      { 
       try 
       { 
        // Get the handle for the array 
        int handle_array = adsClient.CreateVariableHandle("MAIN.boolArray"); 
        // Write the array to PLC 
        Console.WriteLine("Writing the array at handle: " + handle_array.ToString()); 
        adsClient.WriteAny(handle_array, boolArray); 
       } 
       catch (Exception err) 
       { 
        Console.WriteLine(err.Message); 
       } 
       // The end 
       Console.WriteLine("Done"); 
       Thread.Sleep(3000); 
      } 
     } 
    } 
} 

該代碼很好地代表了向TC3寫入數組。

1

我以前System.Runtime.InteropServices.MarshalAs使用TwinCAT 3.1.4022.0

數組聲明:

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] 
public byte[] data; 

,然後我可以通過

TcAdsClient.WriteAny(ixGroup, ixOffset, data)