title: How to write commands to a serial port using NodeJs?
body: <p>Good day everyone,</p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>The python equivalent and which works fine</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Now in NodeJs, I am using the library serialport and I am trying to achieve this via nodejs that runs under the electron app.</p>
  
  <pre><code>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');
          });
      });
  </code></pre>
  
  <p>I know nodejs is able to interact with the port because when I run <code>serialport-list</code> in command prompt or when i run it within the code</p>
  
  <pre><code>SerialPort.list(function (err, ports) {
      ports.forEach(function(port) {
          console.log(port.comName, port.pnpId, port.manufacturer); // or console.log(port)
      });
  });
  </code></pre>
  
  <p>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.</p>
  
  <p>Any help would be appreciated.</p>