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

作者:用户 来源:互联网 浏览:374 次 时间:2016-05-27 10:33:15

c#串口apireadfilewaitforsingleobject

串口api-读到的字节数不是0,但是传入ReadFile的参数buffer有时候会没数据? - 摘要: 读到的字节数不是0,但是传入ReadFile的参数buffer有时候会没数据?publicBooleanReadDatas(Byte[]buffer,outInt32numberOfBytesRead

问题描述

读到的字节数不是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, "串口未打开");

}

解决方案

throw new ArgumentNullException

【云栖快讯】诚邀你用自己的技术能力来用心回答每一个问题,通过回答传承技术知识、经验、心得,问答专家期待你加入!  详情请点击
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

40+云计算产品,6个月免费体验

现在注册,免费体验40+云产品,及域名优惠!

云服务器9.9元/月,大学必备

热点导航