2 garyzhang29 GaryZhang29 于 2013.07.07 16:54 提问

读到的字节数不是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个回答

u011333582
u011333582   2013.07.07 18:16

throw new ArgumentNullException

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!