C#串口编程实例代码

时间:2022-03-19 04:21:29

由于工作需要,第一次接触串口编程.所以不得不在网上查阅各种编程实例.最后结合自己的理解与实践,最终有了如下代码.
本代码只经过了简单的软件测试,与简单的硬件测试.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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();
      }
    }
  }
}

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。