Issue with DS1307 RTC

Hopefully someone will be able to help me. I have a Netduino plus and I am trying to read the date from a DS1307 RTC.
When I try to read it from the thread of my application that initialized the RDS1307 it works but it fails when I try to read it from another thread. It throws the following error. System.ArgumentOutOfRangeException.

The call currTime = clock.Get().ToString(“HH:mm:ss”) from controlLights causes the exception.

Can anyone help me?

Below is a sample of the code.

class LaunchController
    {
        private DS1307 clock = new DS1307();
        
        public LaunchController () {
            String displayTime;

            clock = new DS1307(); 

            clock.Set(new DateTime(2018, 1, 23, 21, 12, 0));
          
            ThreadStart lightsJob = new ThreadStart(controlLights);
            Thread lightsThread = new Thread(lightsJob);
            lightsThread.Start();

          
            while (true)
            {
                lock (this)
                {
                    displayTime = clock.Get().ToString("MMM dd yyyy HH:mm:ss");
                }
                LCD.ClearScreen();
                LCD.Write(1, 1, displayTime);
  
                Thread.Sleep(1000);
            }
        }

        private void controlLights()
        {
            String currTime;
            TimeSpan cTime;
            TimeSpan lightsOn;
            TimeSpan lightsOff;

            LightsTimer timer1 = new LightsTimer("Test");

            timer1.setPort(new OutputPort(Pins.GPIO_PIN_D2, false));
            timer1.setStartTime(21, 12, 30);
            timer1.setEndTime(21, 13, 00);


            ArrayList myEvents = new ArrayList();
            myEvents.Add(timer1);
        
            while (true)
            {
                foreach (object evnts in myEvents)
                {
                    if (evnts != null)
                    {
                        // Check to see to turn lights on of off.
                        if (evnts is LightsTimer)
                        {
                            lock (this)
                            {
                                currTime = clock.Get().ToString("HH:mm:ss");
                            }

                            cTime = new TimeSpan(int.Parse(currTime.Substring(0, 2)),
                                                int.Parse(currTime.Substring(3, 2)),
                                                int.Parse(currTime.Substring(6, 2)));
                            lightsOn = ((LightsTimer)evnts).getStartTime();
                            lightsOff = ((LightsTimer)evnts).getEndTime();

                            if ((cTime >= lightsOn) && (cTime <= lightsOff))
                            {
                                // Turn on the lights
                                if (!((LightsTimer)evnts).getPort().Read())
                                {
                                    ((LightsTimer)evnts).getPort().Write(true);
                                }
                            }
                            else
                            {
                                // Turn Lights off
                                if (((LightsTimer)evnts).getPort().Read())
                                {
                                    ((LightsTimer)evnts).getPort().Write(false);
                                }
                            }
                        }
                    }

                }
                Thread.Sleep(1000);
            }
        }
    }

The call to the DS1307 code

 public DS1307()
        {
            _slaveConfig = new I2CDevice.Configuration(DS1307_I2C_ADDRESS, DS1307_I2C_CLOCK_RATE_KHZ);
        }

and the GET code

 public DateTime Get() {
            byte[] clockData = new byte[7];

            try
            {
                I2CBus.GetInstance().ReadRegister(_slaveConfig, DS1307_RTC_START_ADDRESS, clockData, TransactionTimeout);
            }
            catch (Exception e)
            {
                throw e;
            }

            return new DateTime(
                BcdToDec(clockData[6]) + 2000, // year
                BcdToDec(clockData[5]), // month
                BcdToDec(clockData[4]), // day
                BcdToDec(clockData[2] & 0x3f), // hours over 24 hours
                BcdToDec(clockData[1]), // minutes
                BcdToDec(clockData[0] & 0x7f) // seconds
                );
        }

I would start to break down some of the concatenated calls into single statements to try and work out which component/method is generating the exception.

Threads normally have about 20ms to execute before control may be transferred to the main loop or another thread.

Regards,
Mark

Thanks for your suggestion. I did that with the same results. I then removed my LCD code and it works.

My LCD and the DS1307 are both I2C devices

LCD is using 0xC6 for its address and DS1307 is using 0x68. The following is the exception

The thread ‘’ (0x2) has exited with code 0 (0x0).
#### Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (3) ####
#### Message:
#### System.DateTime::.ctor [IP: 0000] ####
#### System.DateTime::.ctor [IP: 000e] ####
#### RealTimeClockDS1307.DS1307::Get [IP: 0053] ####
#### AquariumController.LaunchController::controlLights [IP: 0061] ####

Since they are both I2C devices do I need to do something different to address them?

If you have a look at the Netduino.Foundation you will find an I2CBus class in there. This allows multiple I2C devices to be attached to the same bus. The I2CBus class is here and there are several examples on how to use it in the Peripheral_Libs folder.

For instance, the BME280 is an I2C sensor. There is a driver and some example code showing how the I2CBus class is used.

The way we solved the problem was to have a single static I2C object and for each instance to have it’s own configuration object, The configuration is changed just before the bus is accessed. The class should also be thread safe.

Feel free to use the library. We’re alway looking for additional drivers and it would be great to see the DS1307 added to the library.

Hope this helps,
Mark