READFILE API读串口数据前面几十条正常后面就乱了
网上找了很多 就是没看到 C#的readfile 异步读取,自己搞的又出问题,
自己写的读取的方法
public byte[] Read(int NumBytes, ref int BytesRead)
{
byte[] BufBytes;
byte[] BufOutBytes=null;
BufBytes = new byte[NumBytes];
if (HandleReadFile != -1)
{
KERNEL32.OVERLAPPED ovlCommPort = new KERNEL32.OVERLAPPED();
KERNEL32.PurgeComm(HandleReadFile, PURGE_TXCLEAR);
if (!KERNEL32.ReadFile(HandleReadFile, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort))
{
if (!KERNEL32.GetOverlappedResult(HandleReadFile, ref ovlCommPort, out BytesRead, false))
{
}
Array.Copy(BufBytes, BufOutBytes, BufBytes.Length);
UpdateEpcView();
}
}
return BufOutBytes;
}