Main Site Documentation

TinyCLR 0.8.0 - DataReader Load Generates ArgumentOutOfRangeException


#1

I have recently been encountering some issues with the DataReader Load member. It generates a System.ArgumentOutOfRangeException when called and there is no data in the serial input stream, this is unusual as I am use to it returning 0 when there is nothing to be loaded from the input stream.


#2

Some simple code that we can reproduce please? how often is this happened?


#3

Here’s a simple version of my code, it should reproduce the problem. It happens pretty frequently, basically every time I run it.

public void Start()
{

SerialDevice serial = SerialDevice.FromId(deviceId); //For FEZ COBRA III Board
    serial.BaudRate = 115200; 
    serial.Parity = SerialParity.None; 
    serial.StopBits = SerialStopBitCount.One; 
    serial.DataBits = 8; //Added
    serial.ReadTimeout = TimeSpan.Zero;


DataReader serialReader = new DataReader(serial.InputStream); 
DataWriter serialWriter = new DataWriter(serial.OutputStream);

_readDataThread = new Thread(ReadDataThread);
_readThreadAlive = true;
_readDataThread.Priority = ThreadPriority.Highest;
_readDataThread.Start();
}

protected void ReadDataThread()
{

while (_readThreadAlive)
    {
    	try
        {
    		if (_serialReader.Load(1) > 0)
            {
			rxBuffer = _reader.ReadByte();
		  }
            }
            catch (Exception ex)
            {
                
            }
            
}

}


#4

We need exactly example that we can reproduce. Following your code, we don’t know what port you are using. Also,

_reader.ReadByte();
_serialReader.Load(1);
serialReader = new DataReader(serial.InputStream);

I don’t see any relationship between them. Anyway, as my understand about your code, we rewrite them as below and could not reproduce issue:

static Thread _readDataThread;
        static bool _readThreadAlive = true;
        static DataReader _serialReader;
        static DataWriter _serialWriter;
        
        static void Main()
        {
            SerialDevice serial = SerialDevice.FromId("GHIElectronics.TinyCLR.NativeApis.STM32F4.UartProvider\\0"); //For FEZ COBRA III Board
            serial.BaudRate = 115200;
            serial.Parity = SerialParity.None;
            serial.StopBits = SerialStopBitCount.One;
            serial.DataBits = 8; //Added
            serial.ReadTimeout = TimeSpan.Zero;

            _serialReader = new DataReader(serial.InputStream);
            _serialWriter = new DataWriter(serial.OutputStream);

            _readDataThread = new Thread(ReadDataThread);
            _readThreadAlive = true;
            _readDataThread.Priority = ThreadPriority.Highest;
            _readDataThread.Start();

            Thread.Sleep(-1);

        }
        protected static void ReadDataThread()
        {
            while (_readThreadAlive)
            {
                try
                {
                    if (_serialReader.Load(1) > 0)
                    {
                        int rxBuffer = _serialReader.ReadByte();
                    }
                }
                catch (Exception ex)
                {

                }

            }
        }