SDK 4.1 Onewire Thread.Sleep() replacement

While chasing memory frag and alloc exception problems on my Panda II, I discovered the code I had copied from GHI’s OneWire old tutorial for SDK 4.1 reading a DS18B20 thermometer has a Thread.Sleep(750); after starting the temperature conversion in order to give the Dallas time to make the conversion. This length of time is wreaking havoc on my system which feeds timer callbacks through a prioritized work item queue. [The problems seem to arise because the sleep blocks a Dequeue thread (using only one at the moment) while Enqueue is on an unblocked thread. Not sure why it leads to fragmentation.]

So I want to get rid of that nap. GHI’s tutorial is now based on SDK 4.2 and uses while (ow.ReadByte() == 0) instead of the Sleep which I understand would work with the DS18B20’s sending 0 while converting and then 1 when finished. However, when I replace the Thread.Sleep(750) with the while loop, the thermometer works fine the first time it is used, then never exits the while loop the subsequent time the temperature is requested. I notice the 4.2 code uses ow.TouchReset() instead of 4.1’s ow.Reset(), but otherwise the two examples are identical.

Anyone have any suggestions as to why this doesn’t work for me and what might be used to get rid of the Thread.Sleep(750)?

The thread.sleep is critical to allow time for the conversion to occur, but it’s only necessary when you use parasitic power. Fully power the device and you should find the results are different, the readbyte()==0 trick should be fine. I haven’t actually run a DS18B20 on a 4.2 device, but I’ll try to give it a whirl later tonight

I’m not using parasite power so that’s a plus. I was surprised that the while loop didn’t work on 4.1 except for the first time reading the DS I’m using two thermometers and they are responding in the same way–first request returns the correct temperature but they both hang on the while loop on the subsequent request, never returning anything but 0. Both thermometers work with Thread.Sleep(750) replacing the while(). I wonder about the difference between 4.1’s Reset() and 4.2’s TouchReset() which doesn’t seem to be available on 4.1.

OK, I believe this problem is resolved. Turns out to be a wiring problem. Haven’t tried the fixed system yet, but I would be surprised if the problem remains. So replacing the Thread.Sleep() with the while loop waiting for temp conversion completion works on 4.1.

The code in 4.1 was GHI exclusive, the 4.2 implementation was included in the framework and was a community contribution. That’s the reason the way things work is different. Sorry, didn’t get (and won’t have) time to look at this for 12 hrs or more. I have the Logic Analyser at the ready though :slight_smile: