C # 제어 네트워크 릴레이 코드 예제

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

C # 제어 네트워크 릴레이 코드 예제
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public static Socket ClientSocket;
        public static string IP = "192.168.0.200";
        public static int port = 8899;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void SocketSendMessage(string CMD)
        {
            IPAddress ip = IPAddress.Parse(IP);  //将IPアドレス文字列変換成IPAddress实例
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//使用指定的アドレス簇协议、套接字タイプ和通信プロトコル
            IPEndPoint endPoint = new IPEndPoint(ip, port); // 用指定的ip和ポート番号初期化IPEndPoint实例
            ClientSocket.Connect(endPoint);  //与远程マスター建立连接

            byte[] message = HexStrTobyte(CMD);
            ClientSocket.Send(message);
            byte[] receive = new byte[message.Length];
            int length = ClientSocket.Receive(receive);  // length 受信Bytesの配列長さ
            richTextBox1.Text = DateTime.Now.ToString("HH:mm:ss") + ":" + byteToHexStr(receive)+"\n" + richTextBox1.Text;
            ClientSocket.Close();  //接続を閉じる
        }

        private static byte[] HexStrTobyte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
            return returnBytes;
        }

        // Bytesの配列转hexadecimalストリング·ストリング   
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr = returnStr+" "+ bytes[i].ToString("X2");//ToString("X2") 为C#中的ストリング·ストリング格式控制符
                }
            }
            return returnStr;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SocketSendMessage("01 06 00 00 00 01 48 0A");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SocketSendMessage("01 06 00 00 00 00 89 CA");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SocketSendMessage("01 06 00 01 00 01 19 CA");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SocketSendMessage("01 06 00 01 00 00 D8 0A");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            SocketSendMessage("01 06 00 02 00 01 E9 CA");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            SocketSendMessage("01 06 00 02 00 00 28 0A");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            SocketSendMessage("01 06 00 03 00 01 B8 0A");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            SocketSendMessage("01 06 00 03 00 00 79 CA");
        }

        private void button9_Click(object sender, EventArgs e)
        {
        }

        private void button10_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click_1(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }
    }
}

Related Tags
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.