Measure weight using a Stamps.com digital USB postage scale
dotnet new console
dotnet add package WebScale
// Program.cs
using Aldaviva.WebScale;
using IWebScale webScale = new WebScale();
webScale.WeightChanged += (sender, weight) => Console.WriteLine($"{weight.OunceForce,4:N1} oz.");
// Keep console program running while waiting for WeightChanged events
CancellationTokenSource cancellationTokenSource = new();
Console.CancelKeyPress += (sender, eventArgs) => {
eventArgs.Cancel = true;
cancellationTokenSource.Cancel();
};
Console.WriteLine("Press Ctrl+C to exit");
cancellationTokenSource.Token.WaitHandle.WaitOne();
dotnet run
-
Stamps.com Stainless Steel 5 lb. Digital Scale
- Sign up for free trial at Stamps.com.
- During signup, request a complimentary 5-pound digital postage scale. Pay $9.99 USD shipping and handling.
- Before the four-week trial is up, cancel your account and avoid the monthly fee.
I successfully performed these steps in September 2014. Today, it may work differently, or not at all.
-
Any .NET runtime that supports .NET Standard 2.0 or later:
-
Operating systems:
- Windows
- MacOS
- Linux (may need superuser permissions to access
/dev/hidraw0
)
This package is available as WebScale
on NuGet Gallery.
dotnet add package WebScale
NuGet\Install-Package WebScale
Construct a new WebScale
instance.
using Aldaviva.WebScale;
using IWebScale webScale = new WebScale();
This connects to the first Stamps.com Stainless Steel 5 lb. Digital Scale found connected to the computer.
If there is no suitable device connected, this will wait asynchronously and connect when one appears. If the device disconnects, this will wait for it to reconnect and then automatically reestablish a connection.
The IsConnected
property shows whether or not the instance is currently connected to a scale. Updates to this property are indicated by the IsConnectedChanged
event.
Get how much weight is currently on the scale. The precision is 0.1 ounces. Can be negative if the scale was tared with weight on it which was subsequently reduced.
Force weight = webScale.Weight;
The Force
type is from the UnitsNet library. You can get specific units of force using properties on it like OunceForce
or KilogramsForce
.
This property automatically updates every 400 ± 15 milliseconds whenever the scale is connected.
Receive a notification when the weight on the scale changes. Not fired while taring.
webScale.WeightChanged += (sender, weight) => Console.WriteLine($"{weight.OunceForce,4:N1} oz.");
The event argument is the amount of weight currently on the scale.
If you need event callbacks to run on the UI thread so you can update a Windows Forms or WPF UI, you can set the IWebScale.EventSynchronizationContext
property.
The scale automatically resets itself to zero when it powers on. You can also manually tare it by calling Tare()
.
await webScale.Tare();
After this task completes, the scale's weight will read zero.
When you are done with this instance, call Dispose()
to disconnect from the device. You can also use a using
statement or declaration.
public void ExplicitlyDispose() {
IWebScale webScale = new WebScale();
// use webScale here
webScale.Dispose();
}
public void ImplicitlyDisposeWithUsingDeclaration() {
using IWebScale webScale = new WebScale();
// use webScale here
// when control exits the ImplicitlyDisposeWithUsingDeclaration method, webScale will be disposed
}
public void ImplicitlyDisposeWithUsingStatement() {
using (IWebScale webScale = new WebScale()) {
// use webScale here
// when control exits the using block, webScale will be disposed
}
}
The sample application demonstrates usage of this library.
You may clone this repository and run the sample with dotnet run
in the Sample
project directory.
You may also download prebuilt executables of the sample app for Windows, Linux, and MacOS, each on x64 and ARM. Remember that the Linux app needs to be run with sudo
, and the MacOS and Linux apps need the chmod +x
executable bit.
- Aldaviva/webscale - Node.js version of this library