GaryZhang29 2013-07-07 08:54 采纳率: 0%
浏览 3634

读到的字节数不是0,但是传入ReadFile的参数buffer有时候会没数据?

public Boolean ReadDatas(Byte[] buffer, out Int32 numberOfBytesRead, Int32 timeOut)
{
if (buffer == null)
throw new ArgumentNullException("buffer");

numberOfBytesRead = 0;
Int32 err;
Boolean status = false;
int frameLen = UInt16.MaxValue;
bool isFrameLenGeted = false;

if (this.isOpen)
{
    int totalReadCount = 0;
    bool reading = true;
    while (reading)
    {
        byte[] readBuffer = new byte[56];
        ClearCommError(this.port, ref this.errorFlags, ref this.comStat);
        if (!ReadFile(this.port, readBuffer, readBuffer.Length, out numberOfBytesRead, ref this.overLapped))
        {
            //GetLastError()函数返回ERROR_IO_PENDING,表明串口正在进行读操作
            if ((err = Marshal.GetLastWin32Error()) != ERROR_IO_PENDING)
                throw new SerialPortException(this.PortName, "读串口错误,错误码:" + err);

            //使用WaitForSingleObject函数等待,直到读操作完成或延时已达到timeOut毫秒  
            //当串口读操作进行完毕后,m_osRead的hEvent事件会变为有信号
            switch (WaitForSingleObject(this.overLapped.hEvent, timeOut))
            {
                case WAIT_TIMEOUT://超时
                    reading = false;
                    CancelIo(this.port);
                    status = false;
                    break;
                case WAIT_OBJECT_0://读取完成正确返回
                    numberOfBytesRead = this.overLapped.InternalHigh;
                    //不是每次都出现的问题
                    //问题:这里numberOfBytesRead是正确的,但是readBuffer里面没有任何数据为什么
                    //哪位能找出问题出在哪里?

                    Array.Copy(readBuffer, 0, buffer, totalReadCount, numberOfBytesRead);

                    totalReadCount += numberOfBytesRead;
                    status = true;
                    if (!isFrameLenGeted && totalReadCount >= 3)
                    {
                        frameLen = (int)buffer[1] + ((int)buffer[2] << 8);
                        isFrameLenGeted = true;
                    }

                    if (totalReadCount < frameLen)
                        reading = this.hasPortData();
                    else
                        reading = false;
                    break;
                case WAIT_FAILED://失败
                    {
                        err = Marshal.GetLastWin32Error();
                        reading = false;
                        CancelIo(this.port);
                        throw new SerialPortException(this.PortName, "读取串口失败,错误码:" + err);
                    }
                default:
                    reading = false;
                    CancelIo(this.port);
                    throw new SerialPortException(this.PortName, "读取串口失败,未知错误");
            }
        }
        else
        {
            Array.Copy(readBuffer, 0, buffer, totalReadCount, numberOfBytesRead);
            totalReadCount += numberOfBytesRead;
            status = true;
            if (!isFrameLenGeted && totalReadCount >= 3)
            {
                frameLen = (int)buffer[1] + (int)buffer[2] >> 8;
                isFrameLenGeted = true;
            }

            if (totalReadCount < frameLen)
                reading = this.hasPortData();
            else
                reading = false;
        }
    }

    numberOfBytesRead = totalReadCount;
    if (numberOfBytesRead == 0)
        status = false;
    return status;
}
else
    throw new SerialPortException(this.PortName, "串口未打开");

}

  • 写回答

1条回答

  • 鱼头鱼头 2013-07-07 10:16
    关注

    throw new ArgumentNullException

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!