⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cs

📁 Microsoft .net Framework Compact 下串口通讯范例
💻 CS
📖 第 1 页 / 共 2 页
字号:

                    byte[] read = new byte[32];


                    send[0] = 0x3F;

                    send[1] = 0x3F;

                    send[2] = 0x31;

                    byte[] domain = Encoding.ASCII.GetBytes(this.tbDomain.Text.Trim());

                    byte[] port = Encoding.ASCII.GetBytes(this.tbPort.Text.Trim());


                    domain.CopyTo(send, 3);

                    send[3 + domain.Length] = 0x2C;

                    port.CopyTo(send, 4 + domain.Length);

                    send[2 + domain.Length + port.Length + 2] = 0x2C;

                    send[30] = CS(send, 0, 30);

                    this.serialPort1.Write(send, 0, send.Length);

                    string response = this.serialPort1.ReadLine();

                    if (response.IndexOf("OK") >= 0)
                    {
                        MessageBox.Show("写域名成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);

                    }
                    else
                    {
                        MessageBox.Show("写域名失败,请再写一次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    }

                    this.serialPort1.Close();

                }
                catch (Exception err)
                {

                    MessageBox.Show("写域名失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);

                    this.serialPort1.Close();
                }
            }
        }

      
        /// <summary>
        /// 读域名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DomainRead_Click(object sender, EventArgs e)
        {
            this.tbDomain.Text = string.Empty;

            this.tbPort.Text = string.Empty;

            try
            {
                
                this.serialPort1.PortName = this.PortName;

                if (this.serialPort1.IsOpen) this.serialPort1.Close();

                this.serialPort1.Open();


                byte[] send = new byte[32];

                byte[] read = new byte[32];

                for (int i = 0; i < 32; i++)
                {
                    send[i] = 0x21;
                }


                send[0] = 0x3F;

                send[1] = 0x3F;

                send[2] = 0x32;

                send[30] = CS(send, 0, 30);

                this.serialPort1.DiscardInBuffer();

                this.serialPort1.Write(send, 0, send.Length);

                Thread.Sleep(1000);

                for (int i = 0; i < 32; i++)
                {
                    int value = this.serialPort1.ReadByte();

                    read[i] = (byte)value;
                }

                if (read[0] == 0x3F && read[1] == 0x3F && read[2] == 0x32)
                {

                    string data = Encoding.ASCII.GetString(read, 0, read.Length);

                    int len1 = data.IndexOf(',');

                    int len2 = data.IndexOf(',', len1 + 1);

                    this.tbDomain.Text = data.Substring(3, len1 - 3);

                    this.tbPort.Text = data.Substring(len1 + 1, len2 - len1 - 1);

                    MessageBox.Show("读域名成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);

                }
                else
                {
                    MessageBox.Show("读域名失败,请再写一次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    
                }

                this.serialPort1.Close();

            }
            catch (Exception err)
            {
                MessageBox.Show("读域名失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);

                this.serialPort1.Close();
            }
        }

        /// <summary>
        /// 写MAC地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btMacWrite_Click(object sender, EventArgs e)
        {
            if (!MacValidate())
            {
                return;
            }

            try
            {
                
                this.serialPort1.PortName = this.PortName;

                if (this.serialPort1.IsOpen) this.serialPort1.Close();

                this.serialPort1.Open();


                byte[] send = new byte[32];

                byte[] read = new byte[32];


                send[0] = 0x3F;

                send[1] = 0x3F;

                send[2] = 0x35;  //写地址

                uint macAddr = Convert.ToUInt32(this.tbMacAddr.Text.Trim(), 16);

                byte[] MacSer = ToLittleBytes(macAddr);

                MacSer.CopyTo(send, 3);

                send[30] = CS(send, 0, 30);

                this.serialPort1.Write(send, 0, send.Length);

                string response = this.serialPort1.ReadLine();

                if (response.IndexOf("OK") >= 0)
                {
                    MessageBox.Show("写地址成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    MessageBox.Show("写地址失败,请再写一次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    
                }

                this.serialPort1.Close();

            }
            catch (Exception err)
            {
                MessageBox.Show("写地址失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);

                this.serialPort1.Close();

            }

        }

        /// <summary>
        /// 读MAC地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btMacRead_Click(object sender, EventArgs e)
        {
            this.tbMacAddr.Text = string.Empty;

            try
            {
                this.serialPort1.PortName = this.PortName;

                if (this.serialPort1.IsOpen) this.serialPort1.Close();

                this.serialPort1.Open();


                byte[] send = new byte[32];

                byte[] read = new byte[32];

                for (int i = 0; i < 32; i++)
                {
                    send[i] = 0x21;
                }


                send[0] = 0x3F;

                send[1] = 0x3F;

                send[2] = 0x36;

                send[30] = CS(send, 0, 30);

                this.serialPort1.DiscardInBuffer();

                this.serialPort1.Write(send, 0, send.Length);

                Thread.Sleep(1000);

                for (int i = 0; i < 32; i++)
                {
                    int value = this.serialPort1.ReadByte();

                    read[i] = (byte)value;
                }


                if (read[0] == 0x3F && read[1] == 0x3F && read[2] == 0x36)
                {

                    byte[] MacSer = new byte[3];

                    MacSer[0] = read[3];

                    MacSer[1] = read[4];

                    MacSer[2] = read[5];

                    uint macAddr = ToLittleUint(MacSer, 0);

                    this.tbMacAddr.Text = InttoHexString(macAddr, true);

                    MessageBox.Show("读地址成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);

                }
                else
                {
                    MessageBox.Show("读地址失败,请再写一次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    
                }

                this.serialPort1.Close();

            }
            catch (Exception err)
            {
                MessageBox.Show("读地址失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);

                this.serialPort1.Close();
            }
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -