关于serialport的属性ReceivedBytesThreshold 我设置了值为272可是有时接收的长度为16这是怎么回事,有没有人遇到这种问题谢谢回复 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我是这样读取的SerialPort p = sender as SerialPort; if (!p.IsOpen) { return; } int bytes2 = serialPort.BytesToRead; byte[] buffer2 = new byte[bytes2]; if (p.BytesToRead > 0) { p.Read(buffer2, 0, bytes2); }请问怎么继续读取 p.Read(buffer2, 0, bytes2);读取没有问题.你再把问题描述详细一点.是有时读到16位,那么再读到到16字节的前一次读取是多少呢?还是一直都只读到16位. 代码很多,不好贴刚开始读取是272位,过了几十秒,就读到一次不是272位的数据以后读取到的还是272位,这样数据就错位了,不能解析表达能力不好,不知道你看明白了吗我的QQ 402514969 可否加QQ详谈,谢谢 ReceivedBytesThreshold只是触发DataReceived事件的一个阀值,由于串口通讯的实时性,很难说你每次读取的数据长度就是阀值的长度。请参考下面的方法。技术要点: (1).首先,SerialPort的ReceivedBytesThreshold先设置成1,表示只要有1个字符送达端口时便触发DataReceived事件 (2).当DataReceived触发时,先把ReceivedBytesThreshold设置成一个比较大的值,达到读取本次端口数据时,不再触发DataReceived. (3).循环读取端口中的数据,直至读完。 (4).移除读取数据中的非法字符。 (5).触发一个后台线程处理收到的数据。 (6).在finally中把ReceivedBytesThreshold重置回1 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { if (sender.GetType() != typeof(System.IO.Ports.SerialPort)) { return; } string strReceive = string.Empty; string strCollect = string.Empty; System.IO.Ports.SerialPort comPort = (System.IO.Ports.SerialPort)sender; try { comPort.ReceivedBytesThreshold = comPort.ReadBufferSize; while (true) { strReceive = comPort.ReadExisting(); if (string.Equals(strReceive, string.Empty)) { break; } else { strCollect += strReceive; Application.DoEvents(); Thread.Sleep(100); } } strCollect = strCollect.Replace("\0", string.Empty); strCollect = strCollect.Replace("\r\n", string.Empty); strCollect = strCollect.Replace("\r", string.Empty); strCollect = strCollect.Replace("\n", string.Empty); if (!this.bIsHandleCom) { this.bIsHandleCom = true; mReceiveData = strCollect; if (ReceiveDataParserEvent != null) ReceiveDataParserEvent(mReceiveData); if (ThreadReceiveParser != null && !ThreadReceiveParser.IsAlive) { ThreadReceiveParser.Start(); } } } catch (Exception ex) { MessageBox.Show(this, ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { comPort.ReceivedBytesThreshold = 1; } } C# ini文件如何读取一个section中的key 外加一读取问题 求个sql语句 菜鸟求助各位大师 谁有c#写的xml解析器阿 安装程序问题 infopath2007附件权限问题 C#怎么给datatable按某行排序 C#中设置DataGrid的headerStyle的Backcolor如何写 关于按下键无效的问题,问了好几次了没人回,希望知道的人帮帮忙 郁闷 在.cs文件下插入js代码的方法是什么? 用Timer控件刷新listview,怎样可以不闪?
SerialPort p = sender as SerialPort;
if (!p.IsOpen)
{
return;
}
int bytes2 = serialPort.BytesToRead;
byte[] buffer2 = new byte[bytes2];
if (p.BytesToRead > 0)
{
p.Read(buffer2, 0, bytes2);
}
请问怎么继续读取
你再把问题描述详细一点.是有时读到16位,那么再读到到16字节的前一次读取是多少呢?
还是一直都只读到16位.
刚开始读取是272位,过了几十秒,就读到一次不是272位的数据
以后读取到的还是272位,这样数据就错位了,不能解析
表达能力不好,不知道你看明白了吗
我的QQ 402514969 可否加QQ详谈,谢谢
(1).首先,SerialPort的ReceivedBytesThreshold先设置成1,表示只要有1个字符送达端口时便触发DataReceived事件
(2).当DataReceived触发时,先把ReceivedBytesThreshold设置成一个比较大的值,达到读取本次端口数据时,不再触发DataReceived.
(3).循环读取端口中的数据,直至读完。
(4).移除读取数据中的非法字符。
(5).触发一个后台线程处理收到的数据。
(6).在finally中把ReceivedBytesThreshold重置回1 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (sender.GetType() != typeof(System.IO.Ports.SerialPort))
{
return;
}
string strReceive = string.Empty;
string strCollect = string.Empty;
System.IO.Ports.SerialPort comPort = (System.IO.Ports.SerialPort)sender; try
{
comPort.ReceivedBytesThreshold = comPort.ReadBufferSize;
while (true)
{
strReceive = comPort.ReadExisting();
if (string.Equals(strReceive, string.Empty))
{
break;
}
else
{
strCollect += strReceive;
Application.DoEvents();
Thread.Sleep(100);
}
}
strCollect = strCollect.Replace("\0", string.Empty);
strCollect = strCollect.Replace("\r\n", string.Empty);
strCollect = strCollect.Replace("\r", string.Empty);
strCollect = strCollect.Replace("\n", string.Empty); if (!this.bIsHandleCom)
{
this.bIsHandleCom = true;
mReceiveData = strCollect;
if (ReceiveDataParserEvent != null)
ReceiveDataParserEvent(mReceiveData);
if (ThreadReceiveParser != null && !ThreadReceiveParser.IsAlive)
{
ThreadReceiveParser.Start();
}
} }
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
comPort.ReceivedBytesThreshold = 1;
}
}