English 中文(简体)
C# Serial Communications - received Data loss
原标题:C# Serial Communications - Received Data Lost

我的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即为超写。 在某些制度中,这并不是一个问题,因为数据迅速出现。 然而,关于接受缓冲的其他系统数据则不完整。 如何接收数据<代码>p UartData_c ,而不是超标准。 我正在使用Microsoft Vision C# 2008快版。 感谢。

最佳回答
private SerialPort sp;
private List<byte> byte_buffer = new List<byte>();    

private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
   byte_buffer.Clear();

   while (sp.BytesToRead > 0)
   {
      byte_buffer.Add((byte)sp.ReadByte());
   }
}
问题回答

如果pUartDataList<char>,你可在名单上添加到的果园。 AddRange(

在我知道这只是“Aray”在c#中的问题之前,我不得不阅读这个问题2-3次。 (从一开始,我认为这是一个严重的序列通信错误......lol)

Ok,

You could use a List<> or ArrayList class to maintain dynamic data. i.e add remove etc. so each time you recieve data from serial, just add it to the list.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签