栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

C#与 1200PLC 走 Tcp通讯,1200做服务器,C#做客户端

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#与 1200PLC 走 Tcp通讯,1200做服务器,C#做客户端

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;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/643424.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号