Changing time affects USB ReadTimeout

Changing time has a strange effect on the read timeout and causes a read to block for infinite.

Following example changes the time while reading from a USB serial device.
Device used is a FEZ Domino.

I expected the read timeout would have a timeout that is independant from the Domino’s time.


using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.USBHost;

namespace Test
{
    public class Program
    {
        public static USBH_SerialUSB _serialUsb;

        public static void Main()
        {
            USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;

            while (true)
            {
                Thread.Sleep(5000);
                Debug.Print("Changing time");
                Microsoft.SPOT.Time.TimeService.SetUtcTime(new DateTime(2012,3,30,17,58,00).Ticks);
                Debug.Print("Changed time");
            }
        }

        public static void SerialWorker()
        {
            byte[] buffer = new byte[128];
            int bytesRead;
            while (true)
            {
                Debug.Print("Reading");
                long d0 = DateTime.Now.Ticks;
                bytesRead = _serialUsb.Read(buffer, 0, buffer.Length);
                long d1 = DateTime.Now.Ticks;
                Debug.Print("Read " + bytesRead.ToString() + " bytes, blocked for " + ((d1-d0)/TimeSpan.TicksPerMillisecond).ToString() + " ms");
            }
        }

        public static void DeviceConnectedEvent(USBH_Device device)
        {
            Debug.Print("USB connected");

            if (device.TYPE == USBH_DeviceType.Serial_FTDI)
            {
                switch (device.TYPE)
                {
                    case USBH_DeviceType.Serial_FTDI:
                        _serialUsb = new USBH_SerialUSB(device, 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                        break;
                }
                //_serialUsb.ReadTimeout = 500;
                _serialUsb.Open();
                Thread serialUsbThread = new Thread(SerialWorker);
                serialUsbThread.Start();
            }
        }
    }
}

Output is


USB connected
Reading
Read 0 bytes, blocked for 501 ms
Reading
Read 0 bytes, blocked for 500 ms
Reading
Read 0 bytes, blocked for 501 ms
Reading
Read 0 bytes, blocked for 500 ms
Reading
Read 0 bytes, blocked for 500 ms
Reading
Changing time
Changed time
Changing time
Changed time
Changing time
Changed time

We will investigate but this will have to wait till after 4.2

Ok, I understand.

I work around it now by using a long to store the UtcOffsetTicks in global scope.
Where UTC time is needed the offset is added to the DateTime.Now.Ticks.

That way the internal clock is never changed.