Wiki - 2.0 Serial Communication

Compatibility

List Serial Ports

qz.serial.findPorts().then(function(data) {
   var list = '';
   for(var i = 0; i < data.length; i++) {
      list += "&nbsp; " + data[i] + "<br/>";
   }

   displayMessage("<strong>Available serial ports:</strong><br/>" + list);
}).catch(displayError);

Open and Close Ports

  1. Open a serial port: openPort(port, bounds)

    var delims = {
       start: '\x02',
       end: '\x0D',
       width: null
    };
    
    qz.serial.openPort('COM1', delims).then(function() { // or '/dev/ttyUSB0', etc
       displayMessage("Serial port opened");
    }).catch(displayError);
  2. Close a serial port closePort(port)

    qz.serial.closePort('COM1').then(function() {  // or '/dev/ttyUSB0', etc
       displayMessage("Serial port closed");
    }).catch(displayError);

Send Data

  1. Send data to the open serial port via sendData(port, data, properties)

    Note: The default sendData() settings must be changed for Mettler Toledo scales

    var data = 'hi, serial port\n';
    
    var properties = {
      baudRate: 9600,
      dataBits: 8,
      stopBits: 1,
      parity: 'NONE',
      flowControl: 'NONE'
    };
    
    qz.serial.sendData('COM1', data, properties).catch(displayError);

    Mettler Toledo

    The default settings need to be changed for serial communication to work with Mettler Toledo scales.

    var data = 'W\n';   // <--- Weight command
    function sendSerialData() {
    var properties = {
      baudRate: 9600,
      dataBits: 7,     // <--- Changed from 8
      stopBits: 1,
      parity: 'EVEN',  // <--- Changed from NONE
      flowControl: 'NONE'
    };
    
    qz.serial.sendData('COM1', data, properties).catch(displayError);

Process Results

  1. Use a callback for processing the data retured from the serial port.

    qz.serial.setSerialCallbacks(function(evt) {
      if (evt.type !== 'ERROR') {
         console.log('Serial', evt.portName, 'received output', evt.output);
       } else {
          console.error(evt.exception);
       }
    });

Troubleshooting

Linux Permission Denied

If no ports will list or if you receive Permission denied in Linux (bugs.launchpad.net#949597)

Error: Port name - /dev/ttyUSB0; Method name - openPort(); Exception type - Permission denied.
  1. Run the following command:

    sudo usermod -a -G dialout "$USER"
  2. Log out of the desktop (or reboot)
  3. Log in to the desktop, try again
Edit this page