C#语言上位机 西门子SMART200 通信 VS2010

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)在这里插入代码片

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值