数据读取器不会从串行通信中的输入流返回数据



>我已经成功连接了我的RFID阅读器并打开了端口并且它正在工作,我还使用DataWriter类向其写入了字节。但是现在,当尝试从RFID阅读器读取字节时,它不起作用。PS:我正在尝试使用 MVVM 模式在 UWP 应用程序上实现这一点。它在经典应用程序中工作,因此所有配置都是正确的。

这是我的VMClass

        DataWriter dataWriter;
        //readobjects
        private CancellationTokenSource ReadCancellationTokenSource;
        private Object ReadCancelLock = new Object();
        //writeObjects
        private CancellationTokenSource WriteCancellationTokenSource;
        private Object WriteCancelLock = new Object();
        DataReader DataReaderObject;
        int id1;
        public viewModel()
        {
            // listOfDevices = new ObservableCollection<DeviceListEntry>();
            findAndOpenAsync();
        }
        public async Task findAndOpenAsync()
        {
            string selector = SerialDevice.GetDeviceSelector("COM5");
            DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
            if (devices.Count > 0)
            {
                DeviceInformation deviceInfo = devices[0];
                serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
                serialDevice.BaudRate = 9600;
                serialDevice.DataBits = 8;
                serialDevice.StopBits = SerialStopBitCount.One;
                serialDevice.Parity = SerialParity.None;
                WriteCancellationTokenSource = new CancellationTokenSource();
                ReadCancellationTokenSource = new CancellationTokenSource();
                serialDevice.WriteTimeout = new TimeSpan(0, 0, 10);
                    await Write(WriteCancellationTokenSource.Token);
                int id2 = id1;

            }
            else
            {
                MessageDialog popup = new MessageDialog("Sorry, no device found.");
                await popup.ShowAsync();
            }
        }
        public async Task Write(CancellationToken cancellationToken)
        {
            try
            {
                while (!Recived)
                {
                    Task<UInt32> storeAsyncTask;
                    dataWriter = new DataWriter(serialDevice.OutputStream);
                    byte[] buffer = new byte[9];
                    buffer[0] = 0x7e; //head fixed 7eh
                    buffer[1] = 0x04; //length 4
                    buffer[2] = 0x01; //destination 
                    buffer[3] = 0x13; //function code
                    buffer[4] = 0xed; // destination xor function 
                    buffer[5] = 0x01; // sum (destination+function+xor)modulo256 
                    dataWriter.WriteBytes(buffer);

                    // uint a= await dataWriter.StoreAsync();
                    lock (WriteCancelLock)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        storeAsyncTask = dataWriter.StoreAsync().AsTask(cancellationToken);
                    }
                    UInt32 bytesWritten = await storeAsyncTask;
                    dataWriter.DetachStream();
                    dataWriter.Dispose();
                    if (bytesWritten > 0)
                    {
                        DataReaderObject = new DataReader(serialDevice.InputStream);
                        await Read(ReadCancellationTokenSource.Token);

                    }
                }
            }
            catch (Exception ex) {
            }
        }
        private async Task Read(CancellationToken cancellationToken)
        {
            byte[] bufferRead = new byte[12];
                Task<UInt32> loadAsyncTask;
                uint ReadBufferLength = 1024;
                // Don't start any IO if we canceled the task

                lock (ReadCancelLock)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    DataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
                    loadAsyncTask = DataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
                }

                UInt32 bytesRead = await loadAsyncTask;
                if (bytesRead > 0)
                {
                try
                {
                    DataReaderObject.ReadBytes(bufferRead);
                    byte[] idBytes = new byte[4];
                    idBytes[0] = bufferRead[8];
                    idBytes[1] = bufferRead[7];
                    idBytes[2] = bufferRead[6];
                    idBytes[3] = bufferRead[5];
                    id1 = BitConverter.ToUInt16(idBytes, 0);
                    Recived = true;
                }
                catch (Exception ec)
                {
                }
                finally
                {
                    DataReaderObject.DetachStream();
                    DataReaderObject.DetachBuffer();
                    DataReaderObject.Dispose();
                }
            }

        }
        }

我测试了您的代码并在此行出现以下异常:DataReaderObject.ReadBytes(bufferRead);

WinRT information: The operation attempted to access data outside the valid range

请使用这个:

        if (bytesRead > 0)
        {
            try
            {
                byte[] bufferRead = new byte[bytesRead];
                DataReaderObject.ReadBytes(bufferRead);

而不是

byte[] bufferRead = new byte[12];

最新更新