c# 串口發送接收資料

來源:互聯網
上載者:User

標籤:

   /********************** 串口資料接收事件 *****************************/        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)        {            UTF8Encoding uTF8Encoding = new UTF8Encoding();            byte[] readBytes = new byte[this.SerialPort.BytesToRead];            int num = this.SerialPort.Read(readBytes, 0, readBytes.Length);            this.builder.Clear();//清空緩衝            this.received_count += (long)num;//加接收計數            this.rxtextBox.Invoke((EventHandler)delegate            {                if (!this.rxcharSetup.Checked)//判斷接收資料的格式                {                   //擷取字元長度                    for (int i = 0; i < readBytes.Length; i++)                    {                        byte b = readBytes[i];//顯示十六進位                        this.builder.Append("0x" + b.ToString("X2") + " ");                    }                }                else                {   //顯示字元格式設定                    this.builder.Append(Encoding.GetEncoding("GB2312").GetString(readBytes));                }                this.rxtextBox.SelectionStart = this.rxtextBox.TextLength;                this.rxtextBox.AppendText(this.builder.ToString());                this.rxnumdata.Text = "Rx " + this.received_count.ToString();                //更新發送計數            });        }
  /********************** 發送按鈕 *****************************/        private void senddata_Click(object sender, EventArgs e)        {            if (!this.SerialPort.IsOpen)            {                MessageBox.Show("串口未開啟!!!", "警告");            }            else            {                int num;                if (this.txdataSetup.Checked)                {                    MatchCollection matchCollection = Regex.Matches(this.txtextBox.Text, "(?i)[\\da-f]{2}");                    List<byte> list = new List<byte>();                    foreach (Match match in matchCollection)                    {                        list.Add(byte.Parse(match.Value, NumberStyles.HexNumber));                    }                    this.SerialPort.Write(list.ToArray(), 0, list.Count);                    num = list.Count;                }                else                {                    this.SerialPort.WriteLine(this.txtextBox.Text);                    num = this.txtextBox.Text.Length + 2;                }                this.send_count += (long)num;                this.txnumdata.Text = "Tx " + this.send_count.ToString();                //更新接收計數            }        }

 

c# 串口發送接收資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.