V
V
Vitaly2018-02-27 15:54:41
linux
Vitaly, 2018-02-27 15:54:41

How to send command on COM/Serial port using nodejs?

All the best! So I decided to play around with hardware using nodejs.
I found the popular node-serialport module , and now I’m trying to send something to the hardware with it, but it’s all useless...
The protocol description says:
5a955303f04a6003239980.png
BCC of command protocol is determined by “XOR” for the values ​​from EOT to ETX.
Assignment of Value SOH - 01H
ETX - 03H ACK - 06H ID - 50H
STX - 02H EOT - 04H NCK - 15H
and my own sample command (test) that I'm trying to send:
Test Command (0x76H)
5a9554533849b389557663.png
and my code:

var SerialPort = require('serialport');

var port = new SerialPort('/dev/ttyS0', {
    baudRate: 9600,
    autoOpen: false
});



port.open(function (err) {
    if (err) {
        return console.log('Error opening port: ', err.message);
    }

    var data = Buffer.from("04H50H02H76H03HXXH", "hex")
    

    console.log("will write", data)

    port.write(data);
});

port.on('open', function() {
    console.log("on open event")
});

after starting it nothing happens

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2018-02-27
@Scorpiored88

The 'H' at the end is just a hexadecimal value, you don't need to pass it to Buffer.from.
The BCC value must first be calculated by taking the XOR of all the bytes included in the command. XX - simply indicates the calculated value (in this example it should be 23H).
Accordingly, the line should look like this:
Well, if you fully read the description of the protocol, you will see that after sending the command, you need to receive an acknowledgment (ACK), a response from the dispenser, and issue an ACK confirming the receipt of the response.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question