# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  [VB6] TEMPer Gold USB HID Thermometer Class

## dilettante

*Note*

Be sure to see the newest version posted in a later reply, post #12 or so!


*Some Background*

TEMPer is a series of products by PCsensor for measuring temperature (and in some cases humidity).  There are many models on the market with varying capabilities.  While some of the early units appear as USB Serial devices (COMx: ports) the newer ones are USB HID devices that use the standard Windows driver and don't require driver installation.

_TEMPerGold.cls_ is a Visual Basic 6.0 Class that works only with the TEMPer Gold product.  It is a wrapper around the RDingUSB.dll that comes with the unit's software package.  The software is a basic .Net application you may or may not find useful.  There is basically ZERO documentation available on the companion mini-CD _or online._

TEMPer Gold runs from about $9 to $20 USD (depending on where you shop) if you are curious.  I've never seen the red LED light up as described by PCsensor.  But perhaps mine has a dummy LED where the LED would go, having been dropped to save costs?


*Basic Operation*

If you plug in the TEMPer Gold without installing any software it "installs" in Windows as two HID devices: one a keyboard and the other a special function device.

As a "keyboard" you can make basic use of the TEMPer by opening Excel or even Notepad and then holding Caps Lock or Num Lock for 3 seconds, which makes it start "typing" readings until you hold down Caps Lock or Num Lock 3 seconds again.

Details on using the sample .Net applet can be found on the PCsensor web site (see link above).  I didn't install it because I have too much .Net clutter here right now anyway, and did not want to risk the cruft a poorly constructed installer might leave behind even after uninstalling it.


*Extracting RDingUSB.dll*

Since _[version 1 of]_ my code requires this DLL you'll have to extract it in order to use TEMPerGold.cls yourself.

The easiest way is to locate the MSI package containing the PCsensor demo applet and do an "administrative install" to extract the installation files to a folder.  Example:



```
msiexec /a "d:\work\temper\thepackage.msi" /qb TARGETDIR="d:\work\temper\packagefiles"
```


*My Demo*

The attached demo Project archive contains all of the files needed to build and run the VB6 demo.  However for it to run you'll need to either put RDingUSB.dll in the Project folder yourself, or else place it where a normal system DLL Search will find it.

The demo as written manages the state of its UI to guide you through the right steps, but basically:
First you Open the device.Then you can Get/Set the Calibration.Then you can start taking sample readings.Finally you Close the device.
Calibration is an offset ranging from -8 to +7°C, and it seems to be non-volatile.  You only need to set it when you want to adjust it.


*Oof!*

This was a messy thing to track down and I had lots of false starts.

While written only for the TEMPer Gold you can probably modify it to work for other TEMPer devices that are USB HID devices.  The earlier models that present themselves as a USB COM-port might be easier to use but most of the logic in this class will not apply.

From a DUMPBIN of the file the RDingUSB.dll is really a renamed SonixUSB.dll.


*Taking it Further*

You could easily write a data logger, charting application, etc. if you want one.

I'm thinking of adding it as optional functionality to my GossCam webcam server posted here in another thread.

----------


## dilettante

Hmm, the HTTP webcam server suggests other possibilities.

In addition to just displaying the temperature on the images, it could also log both temperatures and a "brightness" score for the image at the time of each sample.  That might be interesting to use for graphing, one data point being somewhat boring by itself.

----------


## dilettante

BTW:

You can find _GossCam_ as part of this thread: AVICap32 Cam Streaming w/o Clipboard.

----------


## dilettante

Hmm...

Even though PCsensor suggests these work on "NT" (I suspect they meant NT 4.0) it looks like this sort of USB HID device requires Windows 2000 or later, and may not work at all for Win9x.

I haven't had a chance to confirm this though.  Who actually has a machine with Windows 95 running on it these days?  I sure don't, and do most of my Win9x testing in a VM.  Even using a VM product that does USB pass-through from the host might not prove anything.

----------


## dilettante

Well with a little more work I have produced another version.

_Also see version 2.4 posted below!_

TEMPerGold.cls version 2 has the following improvements:
No longer requires RDingUSB.dll, it does the job in pure VB6 and system API calls.Should be able to handle multiple TEMPer Gold devices attached to the same system.
The multiple-device functionality has not been tested, since I have only a single unit here.

Do not be fooled by the attachment size, much of the increase consists of improved documentation.  I have left the revision history comments in and added a section on Usage.


I use an 8.x version of LINK.EXE so if you are still using the 6.0 version that ships with VB6 your binaries will be larger.  But the version 1 demo consisted of the EXE (40KB here) and RDingUSB.dll (44KB).

My version 2 demo doesn't need the extra DLL and compiles and links to a feathery 48KB for me.  So not only is there one less dependency, there is a 36KB net savings!  Not too shabby, if I do say so myself.

----------


## dilettante

Keep in mind that these demos (and both versions of the Class) aren't especially useful if you don't have a TEMPer Gold to play with.  :Wink: 


And while I am thinking about it:

You should be able to use this Class from within VBA 6.x hosts too!

----------


## dilettante

*Update:*

As I expected, initial testing shows that this code also works with the external-probe TEMPer1 device.  The TEMPer1 is packaged somewhat differently and has an external sensor connected by a cable that is about 3 feet long.

----------


## dilettante

I should have posted this very minor update a while back, but here it is now.

This is version 2.4 and it has one significant change with a few minor changes:
Added Name(Dev) property.Added range check on the IsOpen(Dev) property.Added a few comments and corrected some typos.
The significant change is the first one, which lets the client program retrieve the "names" of the devices found.  This can be useful for programs that need to service multiple sensors since their sequence in the list cannot be guaranteed from run to run.

To make a long story short, I've confirmed my class can handle multiple devices properly.


*The Long Story*

Right now the class can handle TEMPer1 and TEMPer Gold devices but these look identical in terms of USB product and vendor codes, etc.  Even the firmware version string can be identical!

In fact the only way they vary sems to be by where they are plugged into the PC.  So these USB device names cannot absolutely tell you which is which.  If you have two devices and swap them later your program will never "know" it.  Examples:


```
\\?\hid#vid_0c45&pid_7401&mi_01#7&8fe1afd&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
\\?\hid#vid_0c45&pid_7401&mi_01#7&1602e383&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
```

This isn't as bad as it sounds though.  Your program can map these names to a shorthand or descriptive name and the association should "stick" until you move devices around.


*How I Handle Mappings*

For example in one of my data logger programs I use an INI file to hold the mappings:


```
[Devices]
\\?\hid#vid_0c45&pid_7401&mi_01#7&8fe1afd&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}=
\\?\hid#vid_0c45&pid_7401&mi_01#7&1602e383&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}=

[\\?\hid#vid_0c45&pid_7401&mi_01#7&8fe1afd&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}]
SourceNameT1=Freezer1
RecordT1=True
SourceNameT2=RED7#9762T2
RecordT2=False
SourceNameH=RED7#9762H
RecordH=False

[\\?\hid#vid_0c45&pid_7401&mi_01#7&1602e383&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}]
SourceNameT1=Freezer2
RecordT1=True
SourceNameT2=RED7#3487T2
RecordT2=False
SourceNameH=RED7#3487H
RecordH=False
```

Here the T1 items are the TEMPer Gold's single temperature measurement.  The T2 and H items reflect possible future support for dual-sensor TEMPer devices that have either a 2nd temperature sensor or a humidity sensor.

The SourceNameXX items in the INI file are manually configured once, after I do an initial run of my logger to scan for devices and build the INI file.  Then I edit the file to set the individual sensor names I want and turn on recording for each.

The RED7#9999XX type names are random unique names the INI builder inserts (RED7 here being the computer name).  "Freezer1" and "Freezer2" are what I renamed the two active sensors my two TEMPer units have.  Those names are carried in my data logs and can be used as charting curve series labels, etc.

----------


## dilettante

The class and demo do not provide any charting or logging themselves.  You'd have to write your own programs to do that, or log the data for import into Excel, etc.

This isn't too bad though, and we always have the MSChart control in VB6.

The example image here isn't the best (the greenhouse heater is designed to gently keep the temperature up in cold weather, not warm weather).  It gives you the general notion though.

----------


## tayyab_ch

Hi, 
Excellent work and information. I was wondering if you can create a DLL for C# or tell how to use your code in Visual Studio 2010 to access and read data from Temper USB. Much obliged.
I bought the device last week. I want to read data from it using Visual Studio 2010. C# is the requirement. I am not very experienced programmer. Just wanted to do experiments for knowledge seeking  :Smilie: 

Tayyab

----------


## dilettante

Sorry, I missed your post until just now.

If you had the VB6 development system you could compile my class as a DLL and use it via COM Interop but since most C# users wouldn't normally have any need for VB6 they wouldn't have it to play with.  I can't really do this for you myself for a couple of reasons:
We can't provide binary in attachments here.The code is actually still pretty rough even though working and you'd almost certainly want to improve upon it yourself.  So you really want source code and not a compiled DLL.

Another option might be to acquire a version of VB.Net (which you may already have on hand depending on the Edition of Visual Studio .Net you use) and attempt an automated conversion and then polish that up.  But the odds of that saving you any effort are probably pretty minimal.  The conversion process VS provides for VB6-to-VB.Net is rough at best, and in this case there'd be a lot of PInvoke stuff which most likely makes things even rougher (mostly a ton of "TODO:" comments in the result).

Your best bet would probably be a bunch of work, yet a lot less than some of the alternatives.  You'd need to go through the source of one of the later versions of my TEMPer interface class pretty much line by line and convert it to C#, and then carefully go through and rewrite it as cleaner C#.


If I were you here's how I'd tackle this:
Download my "version 1" attachment from very early in this thread.  It has the shortest amount of code to translate because it uses the RDingUSB.DLL for the low-level operations.Take the CD that came with your TEMPer or go to PCSensor's web site and get the software they provide for the device.The software comes as an MSI installer package (or a ZIP archive with an MSI inside).  Extract the MSI without installing it.Dig into the extracted folder structure and grab the RDingUSB.DLLCarefuly work through my TEMPerGold version 1 VB6 class, opening it in Notepad if nothing else.  Translate what you find into a C# class.
To extract the MSI I'd do something like this:
Create a junk folder someplace, the root of a D:\, etc. drive may be easiest.  Let's say D:\RDing as an example.Copy the MSI file into it, as in D:\RDing\TEMPer V23.4.msiOpen a command prompt and change the current drive and directory to D:\RDingExtract by typing msiexec /a "D:\RDing\TEMPer V23.4.msi" /qb TARGETDIR="D:\RDing\Extracted"Ok any UAC prompt, it thinks it is doing an administrative install but it doesn't install anything.Then dig through the Extracted subfolder looking for the RDingUSB.DLL and copy that into your Project folder to use it.Delete all of the junk folder and its contents.
This involves the barest minimum of conversion effort for you.  You can always go back and wade through my later versions to look at adding more features or bypassing the need for the RDingUSB.DLL after you get comfortable with things.

About the only bizarre looking thing in the different versions of my classes above is probably where I build some Byte arrays of USB commands during the Class_Initialize event.  There are trivial ways to do the same thing in C# as part of declarations.

----------


## dilettante

It looks like I never did get around to posting any updates.  Later the same month I obtained a TEMPerHUM Gold that meaures both temperature and humidity.

I'm posting a TEMPerMulti.cls demo here that has the newest version of my class.  It handles several of hese devices.  I pretty much finished this last August but it is working so I'm not sure why I hadn't posted it here back then.

I'm using the class as part of a weather station project.  Basically I have a webcam, a TEMPerHUM, and a small USB hub inside a small birdhouse with an "extended USB cable" running back to a Windows Home Server 2011 machine indoors.  It has been running well for a while (since early September or so).

----------


## LaurieY

Hi,
Thanks for the work you have done on this.  I have used your VB6 class to create a DLL which I have been able to call from C#.  
I thought you might like to know of a minor problem I encountered that you are unlikely to have hit. 
It worked instantly using an XP VM but kept failing in W8, both with my dll and with your Multi Demo application, obviously I suspected W8 or X64 problems. 
I tracked the problem down to my Dell touch screen monitor.  It connects the touch via a HID device and that kept reporting "the process cannot access the file because it is being used by another process" in response to the CreateFile.
I suppose that could happen with any other device that is in use.  I patched around it accepting an error only if it was the VID of the Temper.

I am using the TemperHum as part of my telescope focuser.

Laurie

----------


## dilettante

Hi Laurie,

Glad this was useful to you.

Sorry that you ran into that problem but at least you have a workaround/fix.  Should be helpful information to others running into similar HID device collisions.

One of the things I really don't care for is that sometimes the TEMPer devices can "go nuts" and automagically go into keyboard mode and start sending "types" data to the PC.  So far I haven't found a solution, and it can be a serious problem if you do not have a program with keyboard focus active that can "eat" those keystrokes.  Even when you have a key-eater it can lose focus and then the Desktop/Shell gets thoem and things can get really crazy!

If there is a way to disable that function of these TEMPer devices I have not found it yet.

----------


## snovotill

> It looks like I never did get around to posting any updates.  Later the same month I obtained a TEMPerHUM Gold that meaures both temperature and humidity.
> 
> I'm posting a TEMPerMulti.cls demo here that has the newest version of my class.  It handles several of hese devices.  I pretty much finished this last August but it is working so I'm not sure why I hadn't posted it here back then.


Attached is info on how to get the TEMPerNTC working. See attached example in C# and a fully debugged working executable, with comments in the code. TEMPerNTC provides 0.01 degree Celsius resolution and fast updates on the external sensor. It can be used with any NTC thermistor having a resistance of 10Kohm @ 25C and a Beta Value of 3950; these are available on eBay in many shapes and sizes for well under $1 each.

_Attachment removed by moderator._

----------


## Joacim Andersson

@snovotill: Welcome to the VBForums.  :wave: 

I've removed your attachment since it contained both the compiled DLL as well as an EXE file. Binary files are not allowed to be used as attachments in this forum, only source code files are allowed. Also your code example is in C# while this is the CodeBank forum for VB6.

----------


## vwwv

Sorry to resurrect this thread after 4 years of inactivity... but I'm trying to convert the code from  "Dilettante" from VB 6.0 to VB.net 2017 (and share here after that).

I have one TEMPer1F H1 version 1.4.

I already can request the temperature and recieve one answer. My question is how to convert that answer to a temperature and Humidity. Example of one answer: Hexadecimal:  80 4 1C 4F 5 DD 35 46 / Decimal: 128 4 28 79 5 221 53 70.

I already tried lots of things but unsuccessfully.

Could please somebody help me please.

Best Regards,
Bruno Luz.

----------


## vwwv

The original code (version 2 of Dilettante) that convert the temperature:



```
   Dim Buffer(8) As Byte
        Dim NumberOfBytes As Decimal
        Dim Reading As Short

        If IsOpen Then
            If WriteUSB(hTEMPer, CommandReadTemper(0), 9, NumberOfBytes) Then
                If ReadUSB(hTEMPer, Buffer(0), 9, NumberOfBytes) Then

                    'Reading = CShort(Buffer(3)) * 16 Or CShort(Buffer(4)) \ 16

                    Reading = Buffer(3) * 16 Or Buffer(4) \ 16

                    If Reading And &H800 Then Reading = Reading Or &HF000
                    Celsius = Reading * 0.0625
                    GetCelsius = True
                    mError = ""
                    Exit Function
                End If
            End If
            SetError()
        End If
```

----------


## vwwv

I can't understand... I get tree different temperatures.. From the official program "TEMPer V25.6", from the "TXT" button on the device, and from this code...



```
Reading = Buffer(3) * 16 Or Buffer(4) \ 16
Celsius = Reading * 0.0625
```

----------


## dilettante

I found TEMPer1F H1 not working which suggests that this newer device probably returns data in another format, one that my old code doesn't handle properly.

Maybe there is information in that discussion that could help, or maybe new code in that C library that might give you more clues.

----------


## vwwv

Thank you for the information Dilettante

My version of the sensor is: TEMPer1F H1 V1.4.
In attachment the conversion of the first version of the Dilettante code in VB.net 2017. I can retrive data from the sensor but i can't interpret it yet for now. I don't have much time now but in the future i will try to work on it and post here. 

InTEMPerGold Demo versão 0 VB2017 .zip

----------


## jcvamp

I've managed to create a program in VB.net 2010 that can read from multiple TEMPer2 USB dongles, but I don't own any other models to test it with. I was wondering if anyone is still developing this idea, and if so, would they like to collaborate in some way?

----------


## xxdoc123

I have a problem, if hid usb does not return data, readfile keeps blocking the process, causing the program to freeze！

How to solve

----------


## dilettante

I haven't looked at this for a long time.  I found that the devices often fall back to keyboard mode causing lots of trouble.

The alternative to blocking is to read in a separate thread or process, or do non-blocking reads.  This can get very messy in VB6, which normally does everything on its UI thread.  You'd probably do best by creating a C++ DLL to work with your device(s).

Of course the TEMPer devices still have the keyboard-mode issues.

----------


## dilettante

To be honest things have changed and today I would probably roll my own device from a microcontroller and a sensor.  Then I would have more control, and ultimately it might be simpler to have the device look like a USB Serial device rather than a HID device.

----------


## xxdoc123

Thank you for your reply, it is estimated to be a difficult problem

----------


## DllHell

> Thank you for your reply, it is estimated to be a difficult problem


You can get a wifi enabled temp and humidity sensor up and going pretty easily these days:
https://www.amazon.com/ThingPulse-Wi.../dp/B01KE7BA3O

----------

