当前位置:首页 > C# > 正文内容

c# 串口发送接收数据

admin9年前 (2015-12-02)C#4875
/********************** 串口数据接收事件 *****************************/
        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();
                //更新接收计数
            }
        }


扫描二维码推送至手机访问。

版权声明:本文由视觉博客发布,如需转载请注明出处。

本文链接:https://feelsight.cn/post/9.html

标签: c#串口

“c# 串口发送接收数据” 的相关文章

UDP打洞原理及代码

UDP"打洞"原理 1.       NAT分类根据Stun协议(RFC3489),NAT大致分为下面四类1)      Full Cone这种NAT内部的机器...

 C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果)

C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果)

一、各种旋转、改变大小 注意:先要添加画图相关的using引用。 //向右旋转图像90°代码如下: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {...

C#图片处理示例(裁剪,缩放,清晰度,水印)

using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; usin...

安装包制作工具 SetupFactory使用1 详解

安装包制作工具 SetupFactory使用1 详解

Setup Factory 是一个强大的安装程序制作工具。提供了安装制作向导界面,即使你对安装制作不了解,也可以生成专业性质的安装程序。可建立快捷方式,也可直接在 Windows 系统的注册表加入内容,还能在 Win.ini 和 System.ini 内加入设定值,更可以建立反安装选...

c# 全局鼠标事件

1.Win32Api public class Win32Api { [StructLayout(LayoutKind.Sequential)] public class POINT { publi...

C#全局监听Windows键盘事件

1.工具类代码 using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。