npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jspos2

v0.2.0

Published

Simple client for Shtrikh-M POS2 scales

Downloads

9

Readme

Project is in the early stage of development

The main goal of this project is to create simple JavaScript client to work with Shtrikh-M (Russian scales manufacturer http://www.shtrih-m.ru/) scales which use POS2 protocol. This project is in the very early stage of development and tested only with Shtrikh-Slim scales. For now, the client is able to connect to scales and receive current weight channel state. Also it allows to easily send raw commands (thus allowing to send any other command that is not natively supported by this library yet), taking care of LRC calculation and other boring stuff.

Installation

yarn add jspos2

Also this module depends on serialport module, which in turn uses native bindings. It means that you may need to rebuild it with node-gyp, especially if you are going to use it with electron (it that case you may find it usefull to use electron-rebuild for that purpose). For more info on this subject you may look at the serialport docs.

Basic usage

To communicate with the scales you need to create the client first. The first way to create it requires you to provide the instance of SerialPort which should be already opened. The constructor method should be used in that case, it takes port and optional second argument with options. For example, it may look like this:

import SerialPort from 'serialport';
import { Client } from 'jspos2';

const port = new SerialPort(portName, {}, err => {
  if (err != null) { // do something }
  
  // Starting from here we can be sure that the port is ready and opened
  const client = new Client(port, {
    // Optional. May be omitted when scales has the default 0030 password 
    password: List([0x00, 0x00, 0x03, 0x00]),
  });
});

Or, alternatively, you can find it easier to make use of the factory method. To use it you need to know your scales vendor id and product id. For example, for one of the Shtrikh-Slim scales it is 1FC9 and 80A3 accordingly (library is clever enough to determine that in linux this ID will look like '0x1fc9' and in windows '1FC9', you may specify it in either form, in either case). This factory method takes vendor id, product id, and optional third argument with options, and returns Promise.

Client.fromDeviceId('1FC9', '80A3', {
  // Optional. May be omitted when scales has the default 0030 password 
  password: List([0x00, 0x00, 0x03, 0x00]),
  // Optional. If set to false, library does not make any attempts to adjust IDs to
  // the concrete OS and seeks for the strict equality
  normalize: true
}).then(client => {
  // Do something with the client
}, err => {
  // Handle the error somehow
});

Also, according to the protocol, you should initialize client before it can be used (ENQ byte will be sent and NAK byte is expected as a response)

client.init().then(() => {
  // Now we can be sure that the client is initialized
}, err => {
  // Handle the initialization problem 
})

Now, when we have our client created and initialized, we are ready to go and to receive the current weight channel state:

client.requestScalesState().then(s => {
  // Do something with s 
}, err => {
  // Handle the error somehow
})

Scales state has the following shape:

type ScalesStateFields = {
  isFixed: boolean,
  autoZero: boolean,
  enabled: boolean,
  tare: boolean,
  stable: boolean,
  autoZeroError: boolean,
  overweight: boolean,
  measurementError: boolean,
  underweight: boolean,
  noAnswerFromADC: boolean,
  weight: number,
  tareWeight: number
}

Sending RAW requests

To utilize the scales functionality which is not yet fully supported by this library, you has the ability to send raw requests. Under the hood, both the requests and the responses are represented by the RawMessage instances. For example, here is how you can request the scales state using raw command.

import { RawMessage, Client } from 'jspos2';
import { List } from 'immutable';

const type = 0x3A;
// Pure data, without STX, length, type and LRC bytes
const data = List([0x00, 0x00, 0x03, 0x00]);

const requestMessage = new RawMessage(type, data);

// Perform it on the previously created and initialized client
client.sendRawCommand(requestMessage).then(response => {
  // Response is also the RawMessage, so you can retrieve its content from its data property.
}, error => {
  // Handle the error somehow
})