Skip to content

⚖ Measure weight using a Stamps.com digital USB postage scale

License

Notifications You must be signed in to change notification settings

Aldaviva/WebScale.net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

WebScale

Nuget GitHub Workflow Status Testspace Coveralls

Measure weight using a Stamps.com digital USB postage scale

Stamps.com 5-pound digital postage scale

  1. Quick Start
  2. Prerequisites
  3. Installation
  4. Usage
  5. Sample
  6. See also

Quick Start

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

Prerequisites

Installation

This package is available as WebScale on NuGet Gallery.

dotnet add package WebScale
NuGet\Install-Package WebScale

Usage

Connect to device

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.

Read weight

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.

Listen for weight change events

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.

Tare

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.

Dispose

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
    }
}

Sample

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.

See also