SerialPort 收发数据不再响应DataReceived事件

.Net技术 码拜 10年前 (2015-01-05) 3905次浏览 0个评论
使用 SerialPort  ,高速收发数据一段时间后,不再响应DataReceived事件。代码如下:
pre class=”brush: csharp”>
public CommPort()
{
Com = new SerialPort();
sftpCommandFilePath = ConfigurationManager.AppSettings[“sftpCommandFilePath”];
dal = new tsomvDAL();
}
/// <summary>
/// 串口打开事件
/// </summary>
/// <returns></returns>
public bool CommOpen(string port, string baudrate)
{
try
{
Com.PortName = port;
Com.Parity = Parity.None;
Com.BaudRate = 2400; //端口默认2400
Com.ReceivedBytesThreshold = 1;
Com.Open();
Com.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);
return true;
}
catch (Exception ex)
{
LogHelper.LogError(string.Format(“打开串口出错:{0}”,ex.Message));
return false;
}
}
/// <summary>
/// 串口接受事件
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
LogHelper.LogInfo(string.Format(“{0}:进入DataReceived,Closing is {1}”, Com.PortName, Closing.ToString()));
}
/pre>
这是最后一次进入事件打印的日志
2014-11-24 15:25:50 Info COM7:进入DataReceived,Closing is False
2014-11-24 15:25:50 Info COM7接收长度:48
2014-11-24 15:25:50 Info COM7进入while,bufferLst长度48
2014-11-24 15:25:50 Info 接收 COM7 串口数据:1010030341030044
2014-11-24 15:25:50 Info COM7进入while,bufferLst长度40
2014-11-24 15:25:50 Info 接收 COM7 串口数据:10100303414F0090
2014-11-24 15:25:50 Info COM7进入while,bufferLst长度32
2014-11-24 15:25:50 Info 接收 COM7 串口数据:1010030341500091
2014-11-24 15:25:50 Info COM7进入while,bufferLst长度24
2014-11-24 15:25:50 Info 接收 COM7 串口数据:1010030341070048
2014-11-24 15:25:50 Info COM7进入while,bufferLst长度16
2014-11-24 15:25:50 Info 接收 COM7 串口数据:1010030341510092
2014-11-24 15:25:51 Info COM7进入while,bufferLst长度8
2014-11-24 15:25:51 Info 接收 COM7 串口数据:1010030341520093
2014-11-24 15:25:51 Info COM7:退出DataReceived


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SerialPort 收发数据不再响应DataReceived事件
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!