forked from jperson2000/GPS.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialPort.cs
229 lines (201 loc) · 6.33 KB
/
SerialPort.cs
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.IO;
using System.IO.Ports;
using System.Text;
using GeoFramework.Gps.Nmea;
namespace GeoFramework.Gps.IO
{
/// <summary>
/// Wraps the .Net <see cref="System.IO.Ports.SerialPort"/> class and implements workarounds for some
/// known bugs and limitations.
/// </summary>
internal class SerialPort
#if PocketPC
: IDisposable
#else
: System.IO.Ports.SerialPort
#endif
{
#if PocketPC
/* Older devices run into problems when trying to use .NET's SerialPort class.
* For example, it will barf trying to open the GPS Intermediate Driver because
* the driver may *temporarily* report error #21 until the underlying GPS port
* is opened. So, we need a custom SerialStream class for this case.
*/
private SerialStream _BaseStream;
private string _Port;
private int _BaudRate;
#endif
private StreamReader _Reader;
#region Constructors
public SerialPort()
{
}
#if PocketPC
public SerialPort(string portName, int baudRate)
{
_Port = portName;
_BaudRate = baudRate;
}
#else
public SerialPort(string portName, int baudRate)
: this(portName, baudRate, Parity.None, 8, StopBits.One)
{
}
#endif
#if !PocketPC
public SerialPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
: base(portName, baudRate, parity, dataBits, stopBits)
{
ReadTimeout = (int)Device.DefaultReadTimeout.TotalMilliseconds;
WriteTimeout = (int)Device.DefaultWriteTimeout.TotalMilliseconds;
NewLine = "\r\n";
WriteBufferSize = NmeaReader.IdealNmeaBufferSize;
ReadBufferSize = NmeaReader.IdealNmeaBufferSize;
ReceivedBytesThreshold = 65535; // We don't need this event, so max out the threshold
Encoding = Encoding.ASCII;
}
#endif
#endregion
#region Public Properties
#if PocketPC
public string PortName
{
get
{
return _Port;
}
set
{
_Port = value;
}
}
public int BaudRate
{
get
{
return _BaudRate;
}
set
{
_BaudRate = value;
}
}
public SerialStream BaseStream
{
get
{
return _BaseStream;
}
}
public bool IsOpen
{
get
{
return _BaseStream != null;
}
}
public int ReadTimeout
{
get
{
return _BaseStream.ReadTimeout;
}
set
{
_BaseStream.ReadTimeout = value;
}
}
#endif
#endregion
#region Public Methods
#if PocketPC
public void Open(FileAccess access, FileShare sharing)
{
_BaseStream = new SerialStream(_Port, _BaudRate, access, sharing);
}
#else
public new void Open()
{
base.Open();
/* The .Net SerialStream class has a bug that causes its finalizer to crash when working
* with virtual COM ports (e.g. FTDI, Prolific, etc.) See the following page for details:
* http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/8a1825d2-c84b-4620-91e7-3934a4d47330
* To work around this bug, we suppress the finalizer for the BaseStream and close it ourselves instead.
* See the Dispose method for the other half of this workaround.
*/
GC.SuppressFinalize(BaseStream);
}
#endif
#if PocketPC
public void Close()
{
if (_BaseStream != null)
{
_BaseStream.Close();
_BaseStream = null;
}
}
#endif
#if PocketPC
public void DiscardInBuffer()
{
_BaseStream.DiscardInBuffer();
}
#endif
public new string ReadLine()
{
/* On the HTC P3300, an OutOfMemoryException occurs when using SerialPort.ReadLine().
* However, a StreamReader.ReadLine() works just fine. This suggests that SerialPort.ReadLine()
* is buggy! Use a StreamReader to get the job done.
*/
if (_Reader == null)
{
_Reader = new StreamReader(BaseStream, Encoding.ASCII, false, NmeaReader.IdealNmeaBufferSize);
}
return _Reader.ReadLine();
}
#endregion
#region Implementation of IDisposable
#if PocketPC
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endif
#if PocketPC
protected void Dispose(bool disposing)
{
if (disposing)
Close();
}
#else
protected override void Dispose(bool disposing)
{
if (disposing)
{
try
{
/* The .Net SerialStream class has a bug that causes its finalizer to crash when working
* with virtual COM ports (e.g. FTDI, Prolific, etc.) See the following page for details:
* http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/8a1825d2-c84b-4620-91e7-3934a4d47330
* To work around this bug, we suppress the finalizer for the BaseStream and close it ourselves instead.
* See the Open method for the other half of this workaround.
*/
if (IsOpen)
{
BaseStream.Close();
}
}
catch
{
// The BaseStream is already closed, disposed, or in an invalid state. Ignore and continue disposing.
}
}
base.Dispose(disposing);
}
#endif
#endregion
}
}