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
the sample code is as below:
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);
沒有留言:
張貼留言