C#制御ネットワークリレーのコード例

freeFree Technical Resource

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

C#制御ネットワークリレーのコード例
システムの使用;
システム、コレクション、一般的な
システムのコンポーネントモデル;
システムデータの使用;
システムを使用します。
システムを使用します。
System.Netの利用
System.Net.Socketsの使用
システムを使用する.テキスト;
システム.スレッド.タスクを使用します。
System.Windows.Formsの使用

namespace WinFormsApp 1
{
    public paral class 1 Form
    {
        静的なクライアントソケット;
        public static string IP = "192.168.0.200";
        パブリック静的intポート= 8899;
        public 1
        {
            ize
        }

        private void Form1_Load object sender、EventArgs e
        {
            
        }

        ate
        {
            IPAddress ip = IPAddress.Parse IP; //IPアドレス文字列をIPAddressインスタンスに変換する
            ClientSocket = new Socket AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp;//指定されたアドレスクラスタプロトコル、ソケットタイプ、およびCommunicationプロトコルを使用する
            IPEndPoint endPoint = new IPEndPoint ip port; //IPとポート番号でIPEndPointインスタンスを初期化する
            ClientSocket.Connect endPoint; //リモートホストへの接続を確立する

            byte]= HexStryte;
            ClientSocket.Send message;
            byte] receive = new byte message.Length];
            int length = ClientSocket.Receive(receive); // length受信バイト配列長
            Text
            ClientSocket。Close Close nClose nClose nClose; //コネクションを閉じるこねくしょんを閉じる
        }

        ate static byte] HexStr
        {
            hexString = hexString.Replace """;
            if hexString. % 2!= 0の
                hexString += " ";
            byte] returnBytes = new byte hexString.Length / 2];
            for int i = 0; i retB.; i++
                returnBytes i] = Convert.ToByte hexString.Substring i * 2 2.Trim 16;
            ReturnBytesの意味
        }

        //バイト配列を16進文字列に変換   
        public static string byteToHexStr byte] bytes
        {
            string retStr = "";
            if b!= null
            {
                for int i = 0; i b.; i++
                {
                    retStr = retStr +" "+ bi].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 *.