编程语言
首页 > 编程语言> > C#串口连接的读取和发送详解

C#串口连接的读取和发送详解

作者:互联网

C#串口连接的读取和发送详解
一、串口连接的打开与关闭
串口,即COM口,在.NET中使用 SerialPort 类进行操作。串口开启与关闭,是涉及慢速硬件的IO操作,频繁打开或关闭会影响整体处理速度,甚至导致打开或关闭串口失败。非特殊情况,串口一次性打开后,在退出程序时关闭串口即可。在打开串口前,可以设置一些常用的参数。常用的参数如下:

(1)串口的接受/发送超时时间:ReadTimeout/WriteTimeout。

(2) 串口的接受/发送缓存区大小:ReadBufferSize/WriteBufferSize。

具体代码如下:

// Open Com
   _serialPort = new SerialPort(com, baud);
   if (_serialPort.IsOpen) _serialPort.Close();
 
   // Set the read / write timeouts
   _serialPort.ReadTimeout = 500;
   _serialPort.WriteTimeout = 500;
 
   // Set read / write buffer Size,the default of value is 1MB
   _serialPort.ReadBufferSize = 1024 * 1024;
   _serialPort.WriteBufferSize = 1024 * 1024;
 
   _serialPort.Open();
 
   // Discard Buffer
   _serialPort.DiscardInBuffer();
   _serialPort.DiscardOutBuffer();

二、串口发送
SerialPort 类发送支持二进制发送与文本发送,需要注意的是文本发送时,需要知道转换的规则,一般常用的是ASCII、UTF7、UTF-8、UNICODE、UTF32。具体代码如下
需要注意的是超出缓冲区的部分会被直接丢弃。因此,如果需要使用串口传送大文件,那接收方和发送方都需要将各自的缓冲区域设置的足够大,以便能够一次性存储下大文件的二进制数组。若条件限制,缓冲区域不能设置过大,那就需要在发送大文件的时候按照发送缓冲区大小分包去发送,接收方按顺序把该数组组合起来形成接受文件的二进制数组。

#region Send
    /// <summary>
    /// 发送消息(byte数组)
    /// </summary>
    /// <param name="buffer"></param>
    /// <param name="offset"></param>
    /// <param name="count"></param>
    public void Send(byte[] buffer, int offset, int count)
    {
      lock (_mux)
      {
        _serialPort.Write(buffer, offset, count);
        _sendCount += (count - offset);
      }
    }
 
    /// <summary>
    /// 发送消息(字符串)
    /// </summary>
    /// <param name="encoding">字符串编码方式,具体方式见<see cref="Encoding"/></param>
    /// <param name="message"></param>
    public void Send(Encoding encoding , string message)
    {
      lock (_mux)
      {
        var buffer = encoding.GetBytes(message);
        _serialPort.Write(buffer, 0, buffer.Length);
        _sendCount += buffer.Length;
      }
    }
    #endregion

三、串口接收
串口接收需要注意,消息接受与消息处理要代码分离。不能把流程处理的代码放入信息接受处,因为消息处理或多或少会有耗时,这会造成当发送方发送过快时,接受方的接受缓冲区会缓存多条消息。我们可以把接受到的消息放入队列中,然后在外部线程中,尝试去拿出该条消息进行消费。采用 “生产-消费”模式。具体代码如下:

#region Receive
    private void PushMessage()
    {
      _serialPort.DataReceived += (sender, e) =>
      {
        lock (_mux)
        {
          if (_serialPort.IsOpen == false) return;
          int length = _serialPort.BytesToRead;
          byte[] buffer = new byte[length];
          _serialPort.Read(buffer, 0, length);
          _receiveCount += length;
          _messageQueue.Enqueue(buffer);
          _messageWaitHandle.Set();
        }
      };
    }
 
    /// <summary>
    /// 获取串口接受到的内容
    /// </summary>
    /// <param name="millisecondsToTimeout">取消息的超时时间</param>
    /// <returns>返回byte数组</returns>
    public byte[] TryMessage(int millisecondsToTimeout = -1)
    {
      if (_messageQueue.TryDequeue(out var message))
      {
        return message;
      }
 
      if (_messageWaitHandle.WaitOne(millisecondsToTimeout))
      {
        if (_messageQueue.TryDequeue(out message))
        {
          return message;
        }
      }
      return default;
    }
    #endregion

四、完整代码与测试结果

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace SerialportDemo
{
  public class SSerialPort
  {
    private SerialPort _serialPort;
    private readonly ConcurrentQueue<byte[]> _messageQueue;
    private readonly EventWaitHandle _messageWaitHandle;
    private int _receiveCount, _sendCount;
    private readonly object _mux;
    public int ReceiveCount
    {
      get => _receiveCount;
    }
    public  int SendCount
    {
      get => _sendCount;
    }
    public SSerialPort(string com, int baud )
    {
      // initialized
      _mux=new object();
      _receiveCount = 0;
      _sendCount = 0;
      _messageQueue = new ConcurrentQueue<byte[]>();
      _messageWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
 
      // Open Com
      OpenCom(com.ToUpper(),baud);
 
      // Receive byte
      PushMessage();
    }
 
    private void OpenCom(string com, int baud)
    {
      // Open Com
      _serialPort = new SerialPort(com, baud);
      if (_serialPort.IsOpen) _serialPort.Close();
 
      // Set the read / write timeouts
      _serialPort.ReadTimeout = 500;
      _serialPort.WriteTimeout = 500;
 
      // Set read / write buffer Size,the default of value is 1MB
      _serialPort.ReadBufferSize = 1024 * 1024;
      _serialPort.WriteBufferSize = 1024 * 1024;
 
      _serialPort.Open();
 
      // Discard Buffer
      _serialPort.DiscardInBuffer();
      _serialPort.DiscardOutBuffer();
    }
 
 
    #region Static
    /// <summary>
    /// 获取当前计算机的串行端口名的数组
    /// </summary>
    /// <returns></returns>
    public static string[] GetPortNames()
    {
      return SerialPort.GetPortNames();
    }
    #endregion
 
    #region Receive
    private void PushMessage()
    {
      _serialPort.DataReceived += (sender, e) =>
      {
        lock (_mux)
        {
          if (_serialPort.IsOpen == false) return;
          int length = _serialPort.BytesToRead;
          byte[] buffer = new byte[length];
          _serialPort.Read(buffer, 0, length);
          _receiveCount += length;
          _messageQueue.Enqueue(buffer);
          _messageWaitHandle.Set();
        }
      };
    }
 
    /// <summary>
    /// 获取串口接受到的内容
    /// </summary>
    /// <param name="millisecondsToTimeout">取消息的超时时间</param>
    /// <returns>返回byte数组</returns>
    public byte[] TryMessage(int millisecondsToTimeout = -1)
    {
      if (_messageQueue.TryDequeue(out var message))
      {
        return message;
      }
 
      if (_messageWaitHandle.WaitOne(millisecondsToTimeout))
      {
        if (_messageQueue.TryDequeue(out message))
        {
          return message;
        }
      }
      return default;
    }
    #endregion
 
 
    #region Send
    /// <summary>
    /// 发送消息(byte数组)
    /// </summary>
    /// <param name="buffer"></param>
    /// <param name="offset"></param>
    /// <param name="count"></param>
    public void Send(byte[] buffer, int offset, int count)
    {
      lock (_mux)
      {
        _serialPort.Write(buffer, offset, count);
        _sendCount += (count - offset);
      }
    }
 
    /// <summary>
    /// 发送消息(字符串)
    /// </summary>
    /// <param name="encoding">字符串编码方式,具体方式见<see cref="Encoding"/></param>
    /// <param name="message"></param>
    public void Send(Encoding encoding , string message)
    {
      lock (_mux)
      {
        var buffer = encoding.GetBytes(message);
        _serialPort.Write(buffer, 0, buffer.Length);
        _sendCount += buffer.Length;
      }
    }
    #endregion
 
    /// <summary>
    /// 清空接受/发送总数统计
    /// </summary>
    public void ClearCount()
    {
      lock (_mux)
      {
        _sendCount = 0;
        _receiveCount = 0;
      }
    }
 
    /// <summary>
    /// 关闭串口
    /// </summary>
    public void Close()
    {
      _serialPort.Close();
    }
  }
}

测试代码如下:

class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine($"该计算机可使用的串口列表:{string.Join(",", SSerialPort.GetPortNames())}");
 
      Console.Write("请输入需要打开的串口:");
      string port = Console.ReadLine();
      SSerialPort com = new SSerialPort(port, 57600);
      Console.WriteLine($"串口 {port} 打开成功...");
 
      Console.Write("请输入需要打开的串口发送的消息:");
      string text = Console.ReadLine();
 
      while (true)
      {
        com.Send(Encoding.Default, text);
        Console.WriteLine($"总共发送 {com.SendCount}");
        var message = com.TryMessage();
        if (message != null)
        {
          Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss fff")} {Encoding.Default.GetString(message)}");
 
           TEST:从添加延时可以测试到,接受消息和处理消息必须分不同线程处理。因为对于消息的处理或多或少都需要耗时,这样容易造成消息处理不及时。而添加到队列后,我们可以随时取出处理
          //System.Threading.Thread.Sleep(100*1);
        }
        Console.WriteLine($"总共接受 {com.ReceiveCount}");
      }
 
 
      Console.ReadKey();
    }
  }

使用串口工具测试如下,对于串口的接受如丝般顺滑。当我们在消息中增加测试延时后,就会发现当串口工具继续快速发送一段时间后关闭发送,发现使用队列后,依然没有丢失一条来自发送方的消息。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO.Ports;
namespace PacketsTest
{
    
   class ComSR
  {
    private SerialPort serialPort;
    private bool status = false;//接受结束状态,true为成功接收到数据.
    private byte[] reciveData = null;//接受到的数据存储到这里.
    public bool Status
    {
      set { status = value; }
      get { return status; }
        
    }
    public byte[] RecivedData
    {
      get { return reciveData; }
    }
    public SerialPort _SerialPort
    {
      get { return serialPort; }
    }
  
    public ComSR(string portName,int baudRate,int dataBits,Parity parity,StopBits stopbits)
    {
      serialPort = new SerialPort();
      serialPort.PortName = portName;//设置端口
      serialPort.BaudRate = baudRate;//设置波特率
      serialPort.DataBits = dataBits;//设置数据长度
      serialPort.Parity = parity;//设置奇偶校验协议
      serialPort.StopBits = stopbits;//停止位
      serialPort.ReceivedBytesThreshold = 1;//这里是读事件触发条件,1代表一个字节
      serialPort.DtrEnable = true;//启用数据终端就绪信号
      serialPort.RtsEnable = true;//请求发送就绪信号
      serialPort.DataReceived += new SerialDataReceivedEventHandler(Data_Recive);//绑定处理读事件的函数.
    }
  
    public void SendBuff(byte[] data)
    {
      try
      {
          
        serialPort.Write(data, 0, data.Length);//这里是写      
      }
      catch (Exception e)
      {
        if (serialPort != null && serialPort.IsOpen)
        {
          status = false; //这个status是我在上层类中判断端口读写状态的属性,可以去掉
          serialPort.Close();
        }
      }
    }
  
    void Data_Recive(object o, EventArgs e)//这个是读端口,事件处理函数
    {
      try
      {
        Thread.Sleep(3);
        int length = serialPort.BytesToRead;
        reciveData = new byte[length];
        serialPort.Read(reciveData, 0, length);
        while (serialPort.BytesToRead != 0)//由于数据长度读写不一,而且时间很难确定,所以在这里做了这样的处理
        {
          Thread.Sleep(1);
        }
        if (serialPort.IsOpen)
        {
          status = true;
          //serialPort.Close();//这里不应该关闭,应当在更高层次的程序上关闭端口.
        }
      }
      catch (Exception ex)
      {
        if (serialPort != null && serialPort.IsOpen)
        {
          status = false;
          serialPort.Close();
        }
      }
    }
  
  
  
    public void PortOpen()
    {
      if (!serialPort.IsOpen)
      {
        serialPort.Open();
      }
      else
      {
        serialPort.Close();
        serialPort.Open();
      }
    }
    /// <summary>
    /// 关闭端口
    /// </summary>
    public void PortClose()
    {
      if (serialPort != null && serialPort.IsOpen)
      {
        serialPort.Close();
      }
    }
  }
}

标签:C#,System,serialPort,buffer,详解,串口,byte,public
来源: https://blog.csdn.net/kalvin_y_liu/article/details/117396839