asmrobot
2019-11-13 576b92fd82f568572bc4beb125fa0ba0191a602f
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
 
namespace RichCreator.Utility.InputControl
{
    /// <summary>
    /// 串口连接类
    /// </summary>
    public class SerialPortConnect:IDisposable
    {
        private SerialPort serialPort;
        public Action<byte[]> DataReceiveCallback;
        private string comName;
        //private ManualResetEvent manualLock;
 
        public SerialPortConnect(string comName)
        {
            this.comName = comName;
            //this.dataReceiveCallback = dataReceiveCallback;
            //manualLock = new ManualResetEvent(false);
        }
 
 
        // Methods
        public Boolean Connect()
        {
            try
            {
                if (this.serialPort != null)
                {
                    if (this.serialPort.IsOpen)
                    {
                        return true;
                    }
                }
                else
                {
                    this.serialPort = new SerialPort();
                }
                this.serialPort.WriteTimeout = 1000;
                this.serialPort.PortName = comName;
                this.serialPort.BaudRate = 115200;
                this.serialPort.Parity = Parity.None;
                this.serialPort.StopBits = StopBits.One;
                this.serialPort.Open();
                this.serialPort.DataReceived += new SerialDataReceivedEventHandler(this.SerialPortDataReceived);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
 
 
        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <returns></returns>
        public Boolean Disconnect()
        {
            if (this.serialPort == null)
            {
                return false;
            }
            if (this.serialPort.IsOpen)
            {
                this.serialPort.Close();
            }
            return true;
        }
 
 
        /// <summary>
        /// 串口状态
        /// </summary>
        public bool SerialPortStatus
        {
            get
            {
                if (this.serialPort == null)
                {
                    return false;
                }
                if (this.serialPort.IsOpen)
                {
                    return true;
                }
                return false;
            }
        }
 
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public Boolean Send(byte[] buf, int len)
        {
            if (this.serialPort == null)
            {
                //throw new Exception("串口不存在");
                return false;
            }
            if (!this.serialPort.IsOpen)
            {
                //throw new Exception("串口没打开");
                return false;
            }
            try
            {
                
                this.serialPort.Write(buf, 0, len);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
 
        }
 
 
 
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            
            if (this.serialPort.IsOpen)
            {
                try
                {
                    if (this.serialPort.BytesToRead <= 0)
                    {
                        return;
                    }
 
                    byte[] buffer = new byte[this.serialPort.BytesToRead];
                    int num = this.serialPort.Read(buffer, 0, buffer.Length);
 
                    if (this.DataReceiveCallback != null)
                    {
                        this.DataReceiveCallback(buffer);
                    }
                }
                catch
                { }
            }
 
        }
 
        private bool isFinialize = false;
        public void Dispose()
        {
            Close();
        }
 
        public void Close()
        {
            if (!isFinialize)
            {
                if (serialPort != null)
                {
                    if (serialPort.IsOpen)
                    {
                        serialPort.Close();
                    }
                    serialPort = null;
                }
 
                //释放锁
                //this.manualLock.Close();
                GC.SuppressFinalize(this);
            }
        }
    }
}