using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
//using MSCommLib;
using System.Net.Sockets;
using System.Net;
using System.IO.Ports;
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket sock;
IPAddress ipaddress;
IPEndPoint ipendpoint;
private void Form1_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
byte[] byt = new byte[60];// s7 200 or SMART 200 与C#以太网 2019 1107 3.24
// byte[] BY1 = { 0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0xC1, 0x02, 0x01, 0x00, 0xC2, 0x02, 0x01, 0x01, 0xC0, 0x01, 0x09 };// 如果是 smart200 选这个
byte[] BY1 = { 0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0xC1, 0x02, 0x4D, 0x57, 0xC2, 0x02, 0x4D, 0x57, 0xC0, 0x01, 0x09 }; // 如果是 s7 200 选这个
byte[] BY2 = { 0x03, 0x00, 0x00, 0x19, 0x02, 0xF0, 0x80, 0x32, 0x01, 0x00, 0x00, 0xCC, 0xC1, 0x00, 0x08, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x01, 0x00, 0x01, 0x03, 0xC0 };//二次握手确认 协议码 s7 200 or smart
ipaddress = IPAddress.Parse(textBox1.Text);//IP地址
ipendpoint = new IPEndPoint(ipaddress, 102);//IP地址
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ipendpoint);//连接创建到指定设备 IP 与端口号
byt = LockThread(BY1);//首次握手确认根据不同的PLC 选择不同的握手协议码
Thread.Sleep(80);
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/4d53f53429fa47cb85bdc22a2034e66a.png#pic_center)
```csharp
在这里插入代码片
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/da2f2ef6e912499ebf314014b8cf1cf6.png)在这里插入代码片