c# source code

I’m looking for some examples of the c# source code used to access the usb port data for the watchport/H sensor. The digi.com website says it is available but I can’t seem to find it.


Sorry, we don’t provide such a thing. I’m curious, where did you see on our website that we do?

We provide no API or SDK, per se. Instead, the Watchport USB Sensor responds to standard ASCII commands, which are listed in the “Using the Watchport Sensor Interface” section of the Watchport Installation Guide.

For example, to test this, open a HyperTerminal session in Windows and connect to the COM port that’s associated with the Watchport USB Sensor, then issue one of the available commands to display a reading.

The exception is the Watchport/P (or the Watchport/D in Proximity mode). This Watchport USB Sensor raises and lowers the DSR (Data Send/Set Ready, pin 6 in RS-232) signal when the beam is reflected back. Your software will have to monitor the status of the DSR signal to see if it’s asserted (set to 0, which means “nothing in proximity”) or not.

The specifics of communicating with the Watchport USB Sensor depends on the programming language you are using. Basic serial port programming knowledge is required.

You can download the Watchport Installation Guide here:


Hello, the C# code to talk to these sensors is just like jeremym explains. However, I can give you a few more tips that may help. Note, that if this is a WatchportH, you can get Temp and Humid. For humidity, simply do something like this:
// write H
sp.Write(new byte[] { 0x48, 0x0D }, 0, 2);

To get Temp in C or F from this sensor, you can send something like this:

// write TC
sp.Write(new byte[] { 0x54, 0x43, 0x0D }, 0, 3);


// write TF
sp.Write(new byte[] { 0x54, 0x46, 0x0D }, 0, 3);

If you want asynchronous behavior, then you will have to use a delegate in C# to read the serial data reply and parse the results.

Otherwise, if synch behavior is good enough, after the write to the sp, you can then do something like this:

// give it time for the bytes to make it back up USB, etc.
if (sp.BytesToRead > 0)
bytes = new byte[sp.BytesToRead];
sp.Read(bytes, 0, sp.BytesToRead);
strFromSensor = ASCIIEncoding.ASCII.GetString(bytes);

If no bytes are read, you may want to try sending one byte at a time, with some delays in between bytes. The first method should work for you. If not, you can write an “else” statement that would try the “1 byte, sleep” method.

Hope this helps.