Code Monkey home page Code Monkey logo

webscale.net's Introduction

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

webscale.net's People

Contributors

aldaviva avatar dependabot[bot] avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.