2018年5月5日 星期六

Control Beckhoff I/O module by C#

This method can directly control I/O module without PLC.
1. Install TwinCAD3
2. Refer the Library of Beckhoff
using TwinCAT.Ads;
3.declare the client object and output array
private TwinCAT.Ads.TcAdsClient client; UInt32[][] OUTPUTs = new UInt32[16][];

4. Using connect method of client object
client.Connect(11);

5. the data structure of IO array: assume there are 16 output ports, then the length of array is 16. Each port has 2 elements, both data type of elements are Uint 32. The first element is the practical address of IO module, the second element is the virtual address of the port. For instance, if the virtual address of output port 1 is 0xC10000D0, then output port 2 is 0xC10000D0+(Uint32)1=0xC10000D1.

6.As we acquire the practical address and virtual address of IO module and its ports. we can use
client.WriteAny(master_address,IO_address,bool);
to control output ports directly.

Or
client.ReadAny(master_address,IO_address, typeof(var));
to read the current state of ports.

the sample code is as below:
for (int i = 0; i < OUTPUTs.Length; i++) { OUTPUTs[i] = new UInt32[2]; OUTPUTs[i][0] = 0x3040030; OUTPUTs[i][1] = 0xC10000D0 + (UInt32)i; object c = client.ReadAny(OUTPUTs[i][0], OUTPUTs[i][1], typeof(byte)); if ((byte)c == 1) { client.WriteAny(OUTPUTs[i][0], OUTPUTs[i][1], (bool)false); rds[i].Checked = false;//radio button object cbs[i].Checked = false;//check box object } else { client.WriteAny(OUTPUTs[i][0], OUTPUTs[i][1], (bool)true); rds[i].Checked = true; cbs[i].Checked = true; } listBox1.Items.Add(c);

沒有留言:

張貼留言

同軸度 Coaxiality

同軸度和同心度是兩個非常容易搞混的幾何公差. 同軸度的符號如下: 跟正位度的符號是一樣的! 有這種設定,主要是它的定義和正位度的使用方法很像,反而跟同心度沒這麼相似. 首先來個範例: 由於這個不是繪圖軟體做的,只是示意用,不合工程圖規範的部分還請包涵. 這個是...