简体   繁体   English

使用 Nodejs 的 USB 转 RS485

[英]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/ ).我正在尝试使用计算机(Linux)从真空计( https://www.pfeiffer-vacuum.com/en/products/measurement/digiline/gauges/?detailPdoId=13238&request_locale=en_US )接收和发送数据16.04) 通过 USB 到 RS485 接口(来自http://www.hjelmslund.dk/的半双工 USB485-STISO)。 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.我正在尝试使用 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.我知道请求正在发送,因为 TxD-Led 每秒闪烁一次。 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.我还尝试了其他发送数据的方法(主要是通过 python 和终端),但取得了类似的成功。 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.我从单个 ascii 字符开始,看看最基本的信号是否正确。 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.对于 ascii '0',发送的信号是 10000011001,对于 ascii '1',它是 10100011001。所以这些几乎是我所期望的,除了似乎有 2 个起始位。 Normally I would expect there to be only 1 startbit.通常我希望只有 1 个 startbit。 Is there a way to change the amount of startbits sent?有没有办法改变发送的起始位的数量? Here are the outputs of the Oszilloscope:以下是 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) 1检查基于协议的通信参数,如波特率、奇偶校验、起始/停止位,它们必须保持一致(如果您在 RS-485 上使用 UART 协议,其他协议如 MODBUS、Profibus... 也是可能的,这与普通 RS-232 不同)

If the gauge uses 9600 baud for communication you can not use 115200 baud in your command.如果仪表使用 9600 波特进行通信,则不能在命令中使用 115200 波特。 In the nodejs code you do not set any parameter (i assume you use the UART protocol because of your nodejs).在 nodejs 代码中,您没有设置任何参数(我假设您因为 nodejs 而使用 UART 协议)。 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如果仪表使用任何其他协议,nodejs 代码也将无法工作,尽管在代码https://en.wikipedia.org/wiki/RS-485中没有设置任何参数,如波特率、奇偶校验等

for other protocols node js serial module can not be used对于其他协议 node js 串口模块不能使用

http://libmodbus.org/ http://libmodbus.org/

http://www.pbmaster.org/ http://www.pbmaster.org/

2 check the proprietary commands you send to the gauge. 2检查您发送到仪表的专有命令 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.当我想读出万用表的数据时,我必须发送一个 ASCII 'D' = 0100 0100 (bin) 以获得答案(字节序?)如果我发送任何其他值,万用表将保持静音。

http://electronicdesign.com/what-s-difference-between/what-s-difference-between-rs-232-and-rs-485-serial-interfaces 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.除非您将 DE 拉高并将R︦E︦ 接地,否则您的谈话将是片面的。

And if you do wire as above, you need to be able to deal with your own echo in the received data.如果你按照上面的方式进行连线,你需要能够在接收到的数据中处理你自己的回声。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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