重新回到这个问题上,项目升级后要在.NetCF 3.5下工作,但是这个Monitor无论如何也不能工作了,无法读取,总是返回超时错误或者OutOfMenoryException,做了几个实验发现,在2.0下可以工作,不知道是不是netcf3.5的问题。顺便做了点小改动。
GpsMonitor.cs
----
近期开发一个基于多普达P800的GPS应用,使用Microsoft的例子不能读取其中的具体数据,于是找了点资料,写了一个串口读取的类。在多普达p800,VS2005下调试“可用”,以前也没接触过,摸着石头过河吧。
已知问题:
1.波特率设置过高偶尔出现死机情况,也不一定是死机,但是失去反应了。不爱等,直接拔电池复位。
2.读取过程中经常出现OutOfMemoryException,分步调试未见异常,但速度哦过慢则读取不全。
3.除了端口不能打开,应用时不应该处理任何错误,否则容易引起程序停止响应或退出。原因不明。
不知哪位高手遇到过类似问题。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
GpsMonitor
1
using System;
2
using System.IO.Ports;
3
using System.Collections;
4
using System.Collections.Generic;
5
using System.Windows.Forms;
6
using System.Runtime.InteropServices;
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
namespace Lordeo.AssetMan.PPC.App
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
10
public class GpsMonitor
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
private SerialPort m_serialPort;
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
private System.Windows.Forms.Timer m_timer;
15
private const int defaultTimerInterval = 800; //越快越容易出错
16
//读取到事件
17
public event EventHandler OnDataRecieved;
18
//临时存储读取的信息
19
private string m_dataReaded = string.Empty;
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
22
/// 构造
23
/// </summary>
24
public GpsMonitor()
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
//timer
27
m_timer = new System.Windows.Forms.Timer();
28
m_timer.Interval = defaultTimerInterval;
29
m_timer.Tick += new EventHandler(Timer_Tick);
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
//设置端口的默认值
32
m_serialPort = new SerialPort();
33
m_serialPort.ReadBufferSize = 2048;
34
m_serialPort.BaudRate = 4800; //P800默认的速率,过高的速率容易产生误码
35
m_serialPort.DataBits = 8;
36
m_serialPort.Parity = Parity.None;
37
m_serialPort.StopBits = StopBits.One;
38
m_serialPort.ReadTimeout = 2000;//超时
39
m_serialPort.PortName = "COM4"; //多数GPS都用COM4
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
m_serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(SerialPort_ErrorReceived);
42
}
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
private void SerialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
this.Message = e.ToString();
47
}
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
timer#region timer
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
51
/// 计时器工作
52
/// </summary>
53
/// <param name="sender"></param>
54
/// <param name="e"></param>
55
private void Timer_Tick(object sender, EventArgs e)
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
try
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
if (!string.IsNullOrEmpty(m_serialPort.ReadLine()))
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
this.Message = m_serialPort.ReadLine();
62
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
}
65
}
66
catch (Exception ex)
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
if (ex.GetType() == typeof(System.TimeoutException))
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
//this.StopWatch();
71
this.Message = "操作超时,请确认设备是否已经打开或尝试其他端口。";
72
}
73
}
74
}
75
#endregion
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
OpenSerialPort#region OpenSerialPort
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
79
/// 打开端口
80
/// </summary>
81
private void OpenSerialPort()
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
if (!m_serialPort.IsOpen)
84![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85
try
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
m_serialPort.Open();
88
}
89
catch (Exception x)
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
this.Message = x.Message;
92
this.StopWatch();
93
}
94
}
95
}
96
#endregion
97![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
CloseSerialPort#region CloseSerialPort
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
100
/// 关闭端口
101
/// </summary>
102
private void CloseSerialPort()
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
m_serialPort.Close();
105
}
106
#endregion
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
properties#region properties
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
110
///设置读取的时间间隔,单位毫秒
111
/// </summary>
112
public int Interval
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114
set
115![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
116
this.m_timer.Interval = value;
117
}
118
}
119
120![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
122
/// 执行读取
123
/// </summary>
124
public void BeginWatch()
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
126
OpenSerialPort();
127
m_timer.Enabled = true;
128
}
129![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
130
/// 停止读取
131
/// </summary>
132
public void StopWatch()
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134
m_timer.Enabled = false;
135
CloseSerialPort();
136
}
137![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
139
/// 读取到的信息
140
/// </summary>
141
public string Message
142![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
143
set
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
m_dataReaded = value;
146
OnDataRecieved(this, null);
147
}
148
get
149![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150
return m_dataReaded;
151
}
152
}
153![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
154![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
155
/// 设置波特率
156
/// </summary>
157
public int BaudRate
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_serialPort.BaudRate; }
160![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_serialPort.BaudRate = value; }
161
}
162![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
163![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
164
/// 数据位
165
/// </summary>
166
public int DataBits
167![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
168![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_serialPort.DataBits; }
169![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_serialPort.DataBits = value; }
170
}
171![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
172
/// 校验协议
173
/// </summary>
174
public Parity ParityType
175![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
176![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_serialPort.Parity; }
177![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_serialPort.Parity = value; }
178
}
179![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
180
/// 停止位
181
/// </summary>
182
public StopBits StopBitsType
183![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
184![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_serialPort.StopBits; }
185![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_serialPort.StopBits = value; }
186
}
187![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
188
/// 端口号
189
/// </summary>
190
public string PortName
191![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
192![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_serialPort.PortName; }
193![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_serialPort.PortName = value; }
194
}
195
#endregion
196![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
197![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
获取可用端口#region 获取可用端口
198![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
199
/// 获取本机可用端口名称
200
/// </summary>
201
/// <returns></returns>
202
public string[] GetPortNames()
203![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
204
ArrayList ports = new ArrayList();
205![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
206
uint hPort = INVALID_FILE_HANDLE;
207![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
208
string port = string.Empty;
209![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
210
for (int i = 0; i < 10; i++)
211![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
212
port = "COM" + i.ToString() + ":";
213![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
214
hPort = CreateFile(port, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, IntPtr.Zero);
215
if (hPort != INVALID_FILE_HANDLE)
216![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
217
ports.Add(port);
218
Console.WriteLine(port);
219
CloseHandle(hPort);
220
}
221
}
222![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
223
return (string[])ports.ToArray(typeof(string));
224
}
225![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
226![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
wince defination#region wince defination
227
private const uint GENERIC_READ = 0x80000000;
228
private const uint OPEN_EXISTING = 3;
229
private const uint INVALID_FILE_HANDLE = 0xFFFFFFFF;
230![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
231
[DllImport("coredll.dll")]
232
static extern uint CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, uint lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr notUsedPassZero);
233![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
234
[DllImport("coredll.dll")]
235
static extern int CloseHandle(uint hObject);
236
#endregion
237![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
238
#endregion
239
}
240
}
241![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)