3

Closed

DS1307 crash

description

The GetTime() method of DS1307 doesn't convert the values with a minimum string length, leading to an exception from Int.Parse due to the empty string, the code should probably be something like this:

return new DateTime(
            second: int.Parse(Tools.Dec2Hex(ReadBuffer[0], 1)),
            minute: int.Parse(Tools.Dec2Hex(ReadBuffer[1], 1)),
            hour: int.Parse(Tools.Dec2Hex(ReadBuffer[2], 1)),
            day: int.Parse(Tools.Dec2Hex(ReadBuffer[4], 1)),
            month: int.Parse(Tools.Dec2Hex(ReadBuffer[5], 1)),
            year: int.Parse(Tools.Dec2Hex(ReadBuffer[6], 1)) + 2000
        );
Closed Jun 21, 2014 at 4:11 PM by Garrcomm

comments

keeweezee wrote Jul 21, 2013 at 4:38 PM

Netduino Plus 2 required this change

FarisRRW wrote Oct 2, 2013 at 11:41 AM

chuckhays: Your proposed code above does fix the problem. I've tried it.

keeweezee: I think it's a more general problem. I'm using a GHI FEZ Hydra board with NETMF 4.2. The exception occurs with that too.

To add, with the existing code, if any of the Second (ReadBuffer[0]), Minute (ReadBuffer[1]) or Hour (ReadBuffer[2]) values are ZERO, then Dec2Hex returns an empty string (ie. "") which causes int.Parse to throw an IndexOutOfRange exception. Calling Tools.Dec2Hex with MinLength = 1 whenever the Input is ZERO fixes the problem (Dec2Hex will return a "0" string as output).

Garrcomm wrote Jun 21, 2014 at 4:11 PM

Hi guys,

Thanks for solving this issue!
I modified the code, so it's now also fixed in the toolbox.