使用FINS/ TCP与欧姆龙PLC沟通
可参考下列教学
using System.Net;using System.Net.Sockets;
上面必须使用;
IPAddress ipAddr = IPAddress.Parse("192.168.250.1"); IPEndPoint ipep = new IPEndPoint(ipAddr, 9600); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Connect(ipep);
先宣告地址与埠号,之后连线。
if (server.Connected){ byte[] data = { 0x46, 0x49, 0x4E, 0x53, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03 }; server.Send(data);byte[] rece = new byte[40]; server.Receive(rece);.........
必须先传一段握手命令,否则之后都无法接收PLC的任何回应。如上
.........byte[] data1 = { 0x46, 0x49, 0x4E, 0x53, 0x00, 0x00, 0x00, 0x1A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x03, 0x00, 0x01, 0x01, 0x01, 0x82, 0x03, 0xE8, 0x00, 0x00, 0x01 }; server.Send(data1); server.Receive(rece);}