如何使用NodeJ将命令写入串行端口? [英] How to write commands to a serial port using NodeJs?

查看:90
本文介绍了如何使用NodeJ将命令写入串行端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在将命令写入到连接到USB控制器的锁上时遇到问题,该USB控制器使用NodeJ连接到笔记本电脑端口.我已经在python中成功实现了这一点,但是在nodejs中遇到了问题.

我之所以转换为nodejs的原因是因为整个应用程序都是用nodejs编写的,并且通过电子方式运行有一个桌面应用程序,而打开/关闭端口只是其中的一小部分.

等效的python并能正常工作

  ser = serial.Serial()ser.baudrate = 38400 #Southco文档中建议的速率,锁和程序的速率必须相同//COMPORT是一个存储整数(例如6)的变量ser.port ="COM {}".format(COMPORT)ser.timeout = 10ser.open()命令="open1"#call serial_connection()函数ser.write((%s \ r \ n"%command).encode('ascii'))#Southco锁以ASCII格式接收和发送命令 

现在在NodeJs中,我正在使用库串行端口,并且试图通过在电子应用程序下运行的nodejs来实现这一点.

  var SerialPort = require('serialport');var port = new SerialPort("COM6",{波特率:38400});port.on('open',function(){port.write(Buffer.from('open1','ascii'),function(err){如果(错误)返回sendData(500,err.message);console.log('写的消息');});}); 

我知道nodejs能够与端口交互,因为当我在命令提示符下运行 serialport-list 或在代码内运行它时

  SerialPort.list(function(err,ports){ports.forEach(function(port){console.log(port.comName,port.pnpId,port.manufacturer);//或console.log(port)});}); 

它显示端口信息.因此,nodejs能够读取端口,并发出有关特定端口的信息,但我似乎无法成功向其发出命令.由于未引发任何错误消息,并且它确实显示了在控制台中写入的消息,因此似乎得到了执行,但是该锁没有反应,而使用python代码,该锁确实通过打开或关闭对所写命令的要求来进行反应.

任何帮助将不胜感激.

解决方案

如果查看SerialPort文档,将会看到实例化新实例时立即打开该端口,除非您传递了选项AutoOn = false.因此,在您的情况下,在您设置侦听器.on('open')时,该端口已经打开,因此它永远不会收到事件.删除侦听器,然后立即调用.write().

Good day everyone,

I am having issues writing commands to a lock connected to a USB controller which is connected to the laptop port using NodeJs. I have successfully achieved this in python, but is having issues in nodejs.

The reason why I am converting to nodejs is because the entire application is written in nodejs and runs has a desktop app via electron, and opening/closing port is just a small component of it.

The python equivalent and which works fine

ser = serial.Serial()
        ser.baudrate = 38400 #Suggested rate in Southco documentation, both locks and program MUST be at same rate
        // COMPORT  is a variable that stores an integer such as 6
        ser.port = "COM{}".format(COMPORT) 
        ser.timeout = 10
        ser.open()
        command = "open1"
        #call the serial_connection() function
        ser.write(("%s\r\n"%command).encode('ascii')) #Southco locks receives and sends commands in ASCII

Now in NodeJs, I am using the library serialport and I am trying to achieve this via nodejs that runs under the electron app.

var SerialPort = require('serialport');

   var port = new SerialPort("COM6", {
        baudRate: 38400
    });

    port.on('open', function() {
        port.write(Buffer.from('open1', 'ascii'), function(err) {
            if (err) 
                return sendData(500, err.message);

            console.log('message written');
        });
    });

I know nodejs is able to interact with the port because when I run serialport-list in command prompt or when i run it within the code

SerialPort.list(function (err, ports) {
    ports.forEach(function(port) {
        console.log(port.comName, port.pnpId, port.manufacturer); // or console.log(port)
    });
});

it shows the port information. Hence, nodejs is able to read the port, and issue information about the specific port, but I cant seem to issue command succesfully to it. It seems to get execute since no error message is thrown and it does display message written in console, but the lock does not react, whereas with the python code the lock does react by opening or closing demanding on the command that is written.

Any help would be appreciated.

解决方案

If you look in the SerialPort docs you'll see that the port is immediately opened when a new instance is instantiated, unless you pass the option AutoOn=false. So, in your case, the port is already open by the time you set the listener .on('open'), so it never receives an event. Remove the listener and just call .write() immediately.

这篇关于如何使用NodeJ将命令写入串行端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆