简体   繁体   中英

USB-to-RS485 using Nodejs

I am trying to receive and send data from a vacuum gauge (previous Model of https://www.pfeiffer-vacuum.com/en/products/measurement/digiline/gauges/?detailPdoId=13238&request_locale=en_US ) with a computer (Linux 16.04) via an USB-to-RS485-Interface (the half-duplex USB485-STISO from http://www.hjelmslund.dk/ ). When I send a request to the gauge using a specific protocol it is supposed to answer to the request and I should be able to receive it with the interface. I managed to send data but whenever I send data, it seems that nothing comes back. I'm trying to do this with Node.js. The Code that I used so far is:

function pack(address, action, parameter, data) {
    var length = String('00' + data.length.toString()).slice(-2);
    var bufferAsString = address + action + parameter + length + data;
    var check = 0;
    for (var i = 0; i < bufferAsString.length; ++i) {
        check += bufferAsString.charCodeAt(i)
    }
    var checkSum = String('000' + String(check % 256)).slice(-3);
    var buffer = Buffer.from(bufferAsString + checkSum),
        carriageReturn = Buffer.from('\r');
    return Buffer.concat([buffer, carriageReturn]);
}

var serialPort = require('serialport');
var SerialPort = serialPort.SerialPort;

var port = new SerialPort('/dev/ttyUSB0', {
    baudrate: 9600,
    dataBits: 8,
    stopBits: 1,
    parity: 'none'
}, false);
port.open(function(err) {
   if (err) {
       return console.log('Error opening port: ', err.message);
   }
    console.log(port.isOpen());
    port.on('data', function (data) {
        console.log('Data: ' + data);
    });
    port.on('close', function() {
        console.log('port closed')
    });
    var sendBuffer = pack('001', '00', '740', '=?');
    setInterval(function() {
        port.write(sendBuffer, function(err, bytes) {
            console.log('send' + bytes)
        });
        port.drain();
    }, 1000)
});

That is supposed to send a request every second to the gauge to measure the pressure. I know that the request is being send since the TxD-Led blinks shortly every second. But I receive no answer to that request. I also tried other methods of sending data (mostly via python and the terminal) but with similar success. The green lamp for sending always flashes up but then nothing happens and no answer is received. I am at a loss as to what to try next and would really appreciate any help that you could give me.

UPDATE: Ok so I seem to have found one possible error in the whole thing. I was working with an oszilloscope to capture the signal that is going out of the interface when I send something. I started with single ascii-characters to see if the most basic signals are cominng out right. For ascii '0' the signal that is being sent is 10000011001, for ascii '1' it is 10100011001. So those are almost what I would expect, except that there seem to be 2 startbits. Normally I would expect there to be only 1 startbit. Is there a way to change the amount of startbits sent? Here are the outputs of the Oszilloscope:

ASCII '0' 信号ASCII '1' 信号

this is a communication problem:

1 check the protocol-based communications parameters like baud rate, parity, start-/stop-bits they have to be consistent ( if you use UART protocol on RS-485 other protocols like MODBUS, Profibus,... are also possible , this is a difference to normal RS-232)

If the gauge uses 9600 baud for communication you can not use 115200 baud in your command. In the nodejs code you do not set any parameter (i assume you use the UART protocol because of your nodejs). If the gauge uses any other protocol the nodejs code will also not work, despite that there are not set any parameters like baud rate, parity,... in the code https://en.wikipedia.org/wiki/RS-485

for other protocols node js serial module can not be used

http://libmodbus.org/

http://www.pbmaster.org/

2 check the proprietary commands you send to the gauge. When i want to read out the data of my multimeter i have to send an ASCII 'D' = 0100 0100 (bin) to get an answer ( endianness ?) If i send any other value the multimeter stays silent.

http://electronicdesign.com/what-s-difference-between/what-s-difference-between-rs-232-and-rs-485-serial-interfaces

Unless you have DE pulled high and R︦E︦ tied to ground, your conversation will be rather one-sided.

And if you do wire as above, you need to be able to deal with your own echo in the received data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM