Coder Social home page Coder Social logo

serialscaleserver's Introduction

Serial Scale Server

Browsers don't have the ability to access devices connected via serial port, which is unfortunate for people who wish to use postal scales or other old hardware from a website. A somewhat-convoluted but actually rather simple solution is to spin up a quick flask webserver on the local device that creates an http interface between the COM port and the browser.

How To Use

First, make sure you have Python 3 installed. Then, just download the code, run pip install -r requirements.txt to get the necessary libraries (using a virtualenv is suggested but not necessary), and call python -m app from the package directory, at which point the scale server methods should be accessible at localhost's port 5000. You can optionally pass a command line argument of the COM port to use by default, for example python -m app COM2.

How It Works

The Scale

The SerialScaleReader object is essentially a wrapper around the pySerial library, applied specifically to interact with a scale. It has a few useful methods:

  • read_weight: Writes the command to the COM port to get the weight from the scale (command configurable via init kwarg), waits a moment for a response (length of wait also configurable via init kwarg), and then returns whatever the scale sends back.
  • get_settings: Gets the settings of the pyserial object that is used to communicate with the scale.
  • update_settings: Changes the settings of the pyserial object that is used to communicate with the scale. Includes:
Setting Accepted Values
port Device names on your computer, for example 'COM1', 'COM2'
baud_rate Integer
parity 'none', 'even', 'odd', 'mark', 'space'
stop_bits 1, 1.5, 2
byte_size 5, 6, 7, 8
  • update_and_get_settings: Calls update_settings and get_settings, returning both the current settings and any errors encountered when updating them. Mostly for convenience.

The Server

The server is incredibly simple, essentially just a one-to-one link between an endpoint and the SerialScaleReader object's methods. The endpoints are as follows:

  • /: The root. It just encourages you to do something useful.
  • /get_settings: Returns the serial port settings as json.
  • /update_settings: Updates the serial port settings, returning them (and any errors) as json. Values can be sent as parameters in a GET request (e.g. localhost:5000/update_settings?port=COM3&baud_rate=9600&stop_bits=1.5) in order to make it easier to bookmark and share presets, or via form data in a POST request. The values which can be sent match the SerialScaleReader's update_settings kwargs.
  • /read: Returns whatever is read from the scale (or the error received if it failed)

serialscaleserver's People

Contributors

msgoodman 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.