怎么在c#应用程序中读取串口传送过来的数据

别人从串口给我发送的是二进制代码,我希望能在界面上点击一个按钮,然后就能把这段代码读取到一个变量中,求这段代码

//创建一个串口通讯
SerialPort CurrentPort = null;
CurrentPort = new SerialPort();
                CurrentPort.ReadBufferSize = 128;
                CurrentPort.PortName = comName;  //端口号 
                CurrentPort.BaudRate = bandRate; //比特率 
                CurrentPort.Parity =parity;//奇偶校验 
                CurrentPort.StopBits = stop;//停止位 
                CurrentPort.DataBits = databit;//数据位
                CurrentPort.ReadTimeout = 1000; //读超时,即在1000内未读到数据就引起超时异常 
                //绑定数据接收事件,因为发送是被动的,所以你无法主动去获取别人发送的代码,只能通过这个事件来处理
                CurrentPort.DataReceived += Sp_DataReceived;
                CurrentPort.Open();
                
                
                
           定义一个变量 byte[] receiveStr;          
                
         //绑定的事件处理函数
         private static void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialPort sp = sender as SerialPort;
            if (sp == null)
                return;
            byte[] readBuffer = new byte[sp.ReadBufferSize];
            sp.Read(readBuffer, 0, readBuffer.Length);
            
            //赋值
            receiveStr=readBuffer;//当然你可以通过转换将byte[]转换为字符串
        }
        
        
        //你要求的按钮事件可以这么写
         private void button1_Click(object sender, EventArgs e)
         {
                 if(receiveStr!=null)
                 {
                     变量  xxx=receiveStr;
                 }
         }

追问

有没有简单的几行代码,功能就是读取端口

追答

因为是串口,所以必须要设置串口通讯协议。这些代码基本上少不了。


以下这些就是绑定串口通讯的。任何与串口通讯的程序都需要配置以下参数。 

CurrentPort.PortName = comName;  //端口号 
                CurrentPort.BaudRate = bandRate; //比特率 
                CurrentPort.Parity =parity;//奇偶校验 
                CurrentPort.StopBits = stop;//停止位 
                CurrentPort.DataBits = databit;//数据位
                CurrentPort.ReadTimeout = 1000; //读超时,即在1000内未读到数据就引起超时异常 
                //绑定数据接收事件,因为发送是被动的,所以你无法主动去获取别人发送的代码,只能通过这个事件来处理
                CurrentPort.DataReceived += Sp_DataReceived;
                CurrentPort.Open();


基本上我以上代码是最少了,不设置通讯参数是无法与串口通讯的

追问

通讯参数这些代码应该放在哪里?

追答     

   System.IO.Ports.SerialPort CurrentPort=null;
       //创建并打开通讯端口 ,以下参数的赋值需要自己写。
        public static int OpenPort()
        {
                CurrentPort = new SerialPort();
                CurrentPort.ReadBufferSize = 128;
                CurrentPort.PortName = "COM1";  //端口号 
                CurrentPort.BaudRate = 1600; //比特率 
                CurrentPort.Parity =parity;//奇偶校验 
                CurrentPort.StopBits = stop;//停止位 
                CurrentPort.DataBits = databit;//数据位
                CurrentPort.DataReceived += Sp_DataReceived;
        }
        //接收数据事件处理
        private static void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialPort sp = sender as SerialPort;
            //readBuffer 就是接收到的数据。
            byte[] readBuffer = new byte[sp.ReadBufferSize];//扩大接收数据量
            sp.Read(readBuffer, 0, readBuffer.Length); 
        }
        
        再不明白我也没办法了

追问

不是啊 我的意思是 这段代码复制过去 应该放在什么地方 放在 private void Form1_Load(object sender, EventArgs e)里面 还是外面

追答

晕死了,我已经给你封装成了两个方法,
OpenPort 是用来启动串口通讯的

而Sp_DataReceived 是一个被动执行方法,当串口通讯启动后 有数据过来他会自动执行,因此你只需要调用OpenPort 方法即可,至于你想在什么地方执行,看你的代码逻辑,在formLoad中初始化的时候就执行也可以,在button中点击按钮后执行也可以。

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答