PLC setting
1. Open GX work 2 to set the protocol type and port of PLC.2. Write the setting to PLC
PC setting
1.Open VS IDE2.Select C#
3.refer Socket library
using System.Net.Sockets;
4.there are two types of protocol of PLC, TCP and UDP. Here we introduce both
5. TCP connection
5.1 Create TCP object
Socket mysocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
byte[] address = { 192, 168, 3, 39 }
IPAddress addr = new IPAddress(address);
the first parameter is IPAddress object, the rear one is port number of PLC, it can be set in GX work2.
5.4 Checking
create a bool object to check the connection is successful or not.
6. UDP connection
mysocket.Connect(addr, 1001);
create a bool object to check the connection is successful or not.
bool result = mysocket.Connected;
6.1 Create UDP object
6.2 Create IP object
6.3 Connect to PLC
6.4 Checking
Socket mysocket = new Socket(SocketType.Dgram, ProtocolType.Udp);
See section 5.2
the first parameter is IPAddress object, the rear one is port number of PLC, it can be set in GX work2.
mysocket.Connect(addr, 1025);
create a bool object to check the connection is successful or not.
bool result = mysocket.Connected;
沒有留言:
張貼留言