C#开放式TCP/IP与S71200PLC通讯

2021/11/2 11:10:31

本文主要是介绍C#开放式TCP/IP与S71200PLC通讯,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Net;
  8 using System.Net.Sockets;
  9 using System.Text;
 10 using System.Threading;
 11 using System.Threading.Tasks;
 12 using System.Windows.Forms;
 13 
 14 namespace TCPIP测试
 15 {
 16     public partial class Form1 : Form
 17     {
 18         public Form1()
 19         {
 20             InitializeComponent();
 21         }
 22         //创建Socket连接对象
 23         private Socket socketlient = null;
 24         //运行标志位
 25         private bool isRunning = false;
 26 
 27         private Thread thrClient;
 28 
 29         private byte[] setArray = new byte[100];
 30 
 31         private void btn_Connect_Click(object sender, EventArgs e)
 32         {
 33             socketlient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
 34 
 35             IPAddress address = IPAddress.Parse(this.txt_IP.Text.Trim());
 36 
 37             IPEndPoint iPEndPoint = new IPEndPoint(address, int.Parse(this.txt_Port.Text.Trim()));
 38 
 39             try
 40             {
 41                 socketlient.Connect(iPEndPoint);
 42             }
 43             catch(Exception ex)
 44             {
 45                 MessageBox.Show("连接失败" + ex, "连接信息");
 46                 return;
 47             }
 48             MessageBox.Show("连接成功", "连接信息");
 49             isRunning = true;
 50 
 51             //开始线程接受PLC报文
 52             thrClient = new Thread(ReceiveMessage);
 53             thrClient.IsBackground = true;
 54             thrClient.Start();
 55         }
 56         /// <summary>
 57         /// 接受和处理PLC返回的报文
 58         /// </summary>
 59         private void ReceiveMessage()
 60         {
 61             while(isRunning==true)
 62             { 
 63                 //定义一个接受缓冲区
 64                 byte[] arrMsgRcv = new byte[1024];
 65                 int length = -1;
 66                 try
 67                 {
 68                     length = socketlient.Receive(arrMsgRcv);
 69                 }
 70                 catch(Exception)
 71                 {
 72                     MessageBox.Show("异常");
 73                 }
 74                 if(length==200|| length == 1024)
 75                 {
 76                     byte[] Result = GetByteArray(arrMsgRcv, 0, length);
 77                     setArray = GetByteArray(arrMsgRcv, 0, 100);
 78                     try
 79                     {
 80                         Invoke(new Action(() =>
 81                         {
 82                             this.txt_SetReadBack1.Text = BitConverter.ToSingle(GetSingleByteArray(Result, 0, 4), 0).ToString();
 83                             this.txt_SetReadBack2.Text = BitConverter.ToSingle(GetSingleByteArray(Result, 4, 4), 0).ToString();
 84 
 85                             this.txt_ActualValue1.Text = BitConverter.ToSingle(GetSingleByteArray(Result, 100, 4), 0).ToString();
 86                             this.txt_ActualValue2.Text = BitConverter.ToSingle(GetSingleByteArray(Result, 104, 4), 0).ToString();
 87                         }));
 88                     }
 89                     catch(Exception ex)
 90                     {
 91                         break;
 92                     }
 93                 }
 94              
 95             }
 96       
 97         }
 98 
 99         private void btn_DisConnect_Click(object sender, EventArgs e)
100         {
101             socketlient?.Shutdown(SocketShutdown.Both);
102             socketlient?.Close();
103         }
104         /// <summary>
105         /// 自定义截取字节数组
106         /// </summary>
107         /// <param name="byteArray"></param>
108         /// <param name="start"></param>
109         /// <param name="length"></param>
110         /// <returns></returns>
111         private byte[] GetByteArray(byte[] byteArray,int start,int length)
112         {
113             byte[] resByte =new byte[length];
114             if(byteArray!=null&&byteArray.Length>=length)
115             {
116                 for(int i=0;i<length;i++)
117                 {
118                     resByte[i] = byteArray[i + start];
119                 }
120             }
121             return resByte;
122         }
123         /// <summary>
124         /// 四字节顺序转换 1:ABCD 2:BADC 3:CDAB 4:DCBA
125         /// </summary>
126         /// <param name="byteArr"></param>
127         /// <param name="start"></param>
128         /// <param name="type"></param>
129         /// <returns></returns>
130         private byte[] GetSingleByteArray(byte[] byteArr,int start,int type)
131         {
132             byte[] resByte = new byte[4];
133             if(byteArr!=null&&byteArr.Length>4&&type>=1&&type<=4)
134             {
135                 for(int i=0;i<4;i++)
136                 {
137                     resByte[i] = byteArr[i + start];
138                 }
139                 byte[] resByte1 = new byte[4];
140                 switch(type)
141                 {
142                     case 1:
143                         resByte1 = resByte;
144                         break;
145                     case 2:
146                         resByte1[0] = resByte[1];
147                         resByte1[1] = resByte[0];
148                         resByte1[2] = resByte[3];
149                         resByte1[3] = resByte[2];
150                         break;
151                     case 3:
152                         resByte1[0] = resByte[2];
153                         resByte1[1] = resByte[3];
154                         resByte1[2] = resByte[0];
155                         resByte1[3] = resByte[1];
156                         break;
157                     case 4:
158                         resByte1[0] = resByte[3];
159                         resByte1[1] = resByte[2];
160                         resByte1[2] = resByte[1];
161                         resByte1[3] = resByte[0];
162                         break;
163                 }
164                 return resByte1;
165             }
166             return resByte;
167 
168         }
169 
170         private void groupBox2_Enter(object sender, EventArgs e)
171         {
172 
173         }
174 
175         private void btn_Set_Click(object sender, EventArgs e)
176         {
177             float f1 = Convert.ToSingle(this.txt_Set1.Text.Trim());
178             float f2 = Convert.ToSingle(this.txt_Set2.Text.Trim());
179 
180             byte[] setArr1 = BitConverter.GetBytes(f1);
181             byte[] setArr2 = BitConverter.GetBytes(f2);
182 
183             setArray[0] = setArr1[3];
184             setArray[1] = setArr1[2];
185             setArray[2] = setArr1[1];
186             setArray[3] = setArr1[0];
187 
188             setArray[4] = setArr2[3];
189             setArray[5] = setArr2[2];
190             setArray[6] = setArr2[1];
191             setArray[7] = setArr2[0];
192 
193             socketlient.Send(setArray);
194 
195         }
196     }
197    
198 }
View Code

 



这篇关于C#开放式TCP/IP与S71200PLC通讯的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程