C# 串行通信 - 接收数据丢失
由于收集器数组被覆盖而不是附加,我的 C# 应用程序中接收到的数据正在丢失。
char[] pUartData_c;
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
pUartData_c = serialPort1.ReadExisting().ToCharArray();
bUartDataReady_c = true;
}
catch ( System.Exception ex )
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
在此示例中,每次收到新数据时都会覆盖 pUartData_c
。在某些系统上这不是问题,因为数据输入得足够快。然而,在其他系统上,接收缓冲区中的数据并不完整。如何将接收到的数据附加到 pUartData_c
,而不是覆盖它。我正在使用 Microsoft Visual C# 2008 Express Edition。谢谢。
Received data in my C# application is getting lost due to the collector array being over-written, rather than appended.
char[] pUartData_c;
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
pUartData_c = serialPort1.ReadExisting().ToCharArray();
bUartDataReady_c = true;
}
catch ( System.Exception ex )
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
In this example pUartData_c
is over-written every time new data is received. On some systems this is not a problem because the data comes in quickly enough. However, on other systems data in the receive buffer is not complete. How can I append received data to pUartData_c
, rather than over-write it. I am using Microsoft Visual C# 2008 Express Edition. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果
pUartData
是List
,您可以使用.AddRange()
将传入的字符添加到列表中。If
pUartData
was aList<char>
you could add the incoming chars to the list with.AddRange()
.我必须读这个问题 2-3 遍才知道这只是 C# 中的数组追加问题。 (最初我以为这是一个严重的串行通信错误...哈哈)
好的,
您可以使用
List
或ArrayList
类来维护动态数据。即添加删除等。因此,每次从串行接收数据时,只需将其添加到列表中即可。
I had to read this question 2-3 times before I knew that this is just Array append problem in c#. (Initially I thought this was a serious serial communication error...lol)
Ok,
You could use a
List<>
orArrayList
class to maintain dynamic data. i.e add remove etc.so each time you recieve data from serial, just add it to the list.