1:PLC的地址是:192.168.1.1:2000;
第一步:编辑PLC程序
第二步:写C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace _1200PLC走Tcp通讯
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket sk;
public void skr()
{
while (true)
{
byte[] by = new byte[1024];
int a = sk.Receive(by);
textBox1.Text = System.Text.Encoding.Default.GetString(by, 0, a);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
sk = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress Ip = IPAddress.Parse(“192.168.1.1”);
int port = int.Parse(“2000”);
sk.Connect(new IPEndPoint(Ip, port));
Thread th = new Thread(skr);
th.IsBackground = true;
th.Start();
} }
}
第三步:运行调试:获得结果,textbox 显示abcde;



