Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node-serialport does not work when run above NW.js #5035

Closed
sadr0b0t opened this issue Jun 27, 2016 · 7 comments
Closed

Node-serialport does not work when run above NW.js #5035

sadr0b0t opened this issue Jun 27, 2016 · 7 comments
Assignees
Labels

Comments

@sadr0b0t
Copy link

Operating System and Hardware: Ubuntu Linux 14.04 64 bit
NW.js version: v0.15.4, v0.16.0-beta2
Serialport version: 4.0.0-rc1, 3.1.2

Hello, Node-serialport fails to open serial port from inside application running above NW.js platform .

<!DOCTYPE html>
<html>
  <head>
    <title>Rraptor</title>
    <meta charset="UTF-8"/>

    <script type="text/javascript">
      var port;


      function writeLog(msg) {
        var logElement = document.getElementById("serial_read_data");
        logElement.innerHTML += msg + "<br>";
        logElement.scrollTop = logElement.scrollHeight;
      }

      /**
       * Обновить список последовательных портов
       */
      function updateSerialPortList() {
        var SerialPort = require('serialport');

        var combo = document.getElementById("serial_ports");
        combo.options.length = 0;

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

            var option = document.createElement("option");
            option.text = port.comName;
            option.value = port.comName;
            combo.add(option, null);
          });
        });
      }

      /**
       * Подключаемся к устройству на последовательном порте
       */
      function connectDevice(portName) {
        var SerialPort = require('serialport');

        port = new SerialPort(portName, {
          // скорость
          baudRate: 9600,
          // получать данные по одной строке
          parser: SerialPort.parsers.readline('\n'),
          // не открывать порт сразу здесь
          autoOpen: false,
          lock: true
        });

        // 
        // события
        // 

        // порт открылся
        port.on('open', function () {
          writeLog('Opened port: ' + portName);

          // пишем данные отсюда, результат получаем дальше 
          // в колбэке на событие data
          port.write('help');
        });

        // пришли данные
        port.on('data', function (data) {

          document.getElementById('serial_read_data').innerHTML += data;
          writeLog(data);
        });

        // 
        // Действия
        // 

        // открываем порт
        port.open(function(err) {
          if (err) {
            writeLog('Error opening serial connection ' + portName + ': ' + err);
          }
        });      
      }

      function sendCmd(cmd) {
        // пишем данные отсюда, результат получаем в колбэке на событие data
          port.write(cmd);
      }
    </script>
  </head>
  <body>
    <h1>Рраптор: панель управления</h1>
    <div>
      Устройство: <select name="serial_ports" id="serial_ports"></select> 
      <a href="javascript:updateSerialPortList()">обновить</a>

      <a href="javascript:connectDevice(document.getElementById('serial_ports').value)">Подключиться</a>
    </div>

    <a href="javascript:sendCmd('help')">Попросить о помощи</a>

    <div id="serial_read_data">
    </div>
  </body>
</html>

copy above code to index.html, then run application from same dir:

~/apps/nwjs-sdk-v0.16.0-beta2-linux-x64/nw .
The app would show window with dropdown list, click on "Update serial port list" link to fill the list of available serial ports (this would work), then select "/dev/ttyUSB0" (or other valid device connected to serial port), then click on "Connect device" link to connect to serial port.

On the first connection attempt it would return error event with "true" as err variable value (and it has boolean datatype), the second and all further connection attempt would return error event with err with "unable to lock device" message.

Same code runs just fine when run from command line with regular nodeJS: I am able to communicate device (Arduino), send commands, receive result etc. Getting list of serial ports also works just fine with NW.js.

Tried both with NW.js stable v0.15.4 and with latest v0.16.0-beta2 (sdk download option allows to use built-in debugger when run app).
http://nwjs.io/downloads/

Serialport version: 4.0.0-rc1, 3.1.2 - tried both, both fail

Same report for Node-serialport project
serialport/node-serialport#861

and same problem with Node-serialport+Electron
serialport/node-serialport#838

@Rainforce15
Copy link

confirming for OS X El Capitan as well - works in v0.14, but no longer in v0.15

@fanchou
Copy link

fanchou commented Jul 6, 2016

@rogerwang may be we should encapsulate chrome serial api,then we can use printer without driver and get more control of printer,and control hardware

@Mike-Dax
Copy link

Mike-Dax commented Jul 7, 2016

I'm getting the same issue, same circumstances on OSX Yosemite, 10.10.4.

@Pumpernickel61
Copy link

As a workaround you could use chrome serial api as described in https://developer.chrome.com/apps/serial, samples can be found here https://github.com/GoogleChrome/chrome-app-samples. Or is there any major difference between node serial and chrome serial?

@kailniris
Copy link

I am using chrome serial api from a long time it works like a charm.

@hujiajie
Copy link
Contributor

hujiajie commented Jul 3, 2017

@Christywl, I cannot reproduce the issue with a recent revision of NW.js. Would you please help to verify it? I'd like to close the issue if it also works from you side.

@Christywl
Copy link
Contributor

I can connect to the Arduino device on Linux with nwjs-sdk-v0.22.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants