How to processing the data retured from the serial port?

116 views
Skip to first unread message

junwei...@gmail.com

unread,
Feb 9, 2017, 11:16:38 PM2/9/17
to qz-print
I want to test the serial communication from the tutorial here (https://qz.io/wiki/2.0-serial-communication#process-results), but I don't know how to display the data retured from the serial port on the web page. Could you please give me some guidance? THS!!

Tres Finocchiaro

unread,
Feb 10, 2017, 12:49:42 AM2/10/17
to junwei...@gmail.com, qz-print
If it's logging to the console properly, you simply have to update the webpage with more JavaScript and would make this is more of a JS/HTML question than a QZ Tray question... 

  qz.serial.setSerialCallbacks(function(evt) {
    if (evt.type !== 'ERROR') {
       console.log('Serial', evt.portName, 'received output', evt.output);
+      document.getElementById("field").value="whatever you want here";
    } else {
       console.error(evt.exception);
    }
  });
  // ...
+ </script>
+ <body>
+ <input type="text" id="field">
+ <!-- etc... 


On Thu, Feb 9, 2017 at 11:16 PM, <junwei...@gmail.com> wrote:
I want to test the serial communication from the tutorial here (https://qz.io/wiki/2.0-serial-communication#process-results), but I don't know how to display the data retured from the serial port on the web page. Could you please give me some guidance? THS!!

--
You received this message because you are subscribed to the Google Groups "qz-print" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qz-print+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bryan martinez

unread,
Nov 20, 2023, 3:39:57 PM11/20/23
to qz-print
Hi, I am trying to return the callback information by using qz.serial.sendData, but the responses give me undefained
function enviarHexa(cmdHexa, puerto, rx) {

        let properties = {};
        if (!rx) {
            rx = {
                encoding: 'ISO-8859-1',
                start: null,
                end: null,
                width: null,
                lengthBytes: null,
                crcBytes: null,
                untilNewline: false
            };
        }

        if (puerto == 'COM6') {
            properties = {
                baudRate: 9600,
                dataBits: 8,
                stopBits: 1,
                parity: 'NONE',
                flowControl: 'NONE',
                rx: rx
            };
        }

        if (puerto == 'COM3') {
            properties = {
                baudRate: 9600,
                dataBits: 7,
                stopBits: 1,
                parity: 'EVEN',
                flowControl: 'NONE',
                rx: rx
            };
        }

        let serialData = {
            type: 'hex',
            data: cmdHexa
        };

        qz.serial.sendData(puerto, serialData, properties)
            .then(response => {
                console.log('Datos enviados con éxito:', response);
             
            })
            .catch(error => {
                console.error('Error al enviar datos serial:', error);
                reject(error);
            });

}

qz.serial.setSerialCallbacks(function (streamEvent) {
    if (streamEvent.type !== 'ERROR') {
        console.log('Serial', streamEvent.portName, 'received output', streamEvent.output);
        if (streamEvent.portName == 'COM6') {
            datosHex = stringToHex(streamEvent.output);
            console.log('Datos en hexadecimal:', datosHex);
        } else if (streamEvent.portName == 'COM3') {
            datosHex = stringToHex(streamEvent.output);
            console.log('Datos en hexadecimal:', datosHex);
   
        }

        return streamEvent.portName

    } else {
        console.log(streamEvent.exception);

    }
});

In the callback I actually have the answer but I don't know how I can return that answer from my sendHexa function, can you help me?. thank you
To unsubscribe from this group and stop receiving emails from it, send an email to qz-print+u...@googlegroups.com.

Tres Finocchiaro

unread,
Nov 20, 2023, 5:11:40 PM11/20/23
to bryan martinez, qz-print
I don't know how I can return that answer from my sendHexa function

I do not see your sendHexa function, but I believe this is more of a question about asynchronous programming, right?

I believe one of the rx parameters start/end, width, untilNewline are required otherwise the callback won't know how often to fire.

Tres Finocchiaro

unread,
Nov 22, 2023, 10:32:21 AM11/22/23
to bryan martinez, qz-print
 
I wanted to know if that response can be consulted when I invoke the sinHexa() function

Not easily, it would require some type of promise resolver or global callback.
 


On Wed, Nov 22, 2023 at 9:57 AM bryan martinez <braya.na...@gmail.com> wrote:
Hello, thank you very much for answering me. this is my sendHexa function function sendHexa(cmdHexa, port, rx) {


      let properties = {};
      if (!rx) {
          rx = {
              encoding: 'ISO-8859-1',
              start: null,
              end: null,
              width: null,
              lengthBytes: null,
              crcBytes: null,
              until new line: false
          };
      }

      if (port == 'COM6') {
          properties = {
              Baud rate: 9600,
              data bits: 8.
              stop bits: 1,
              parity: 'NONE',
              Flow control: 'NONE',
              rx: rx
          };
      }

      if (port == 'COM3') {
          properties = {
              Baud rate: 9600,
              data bits: 7.
              stop bits: 1,
              parity: 'EVEN',
              Flow control: 'NONE',

              rx: rx
          };
      }

      let serialData = {
          type: 'hexadecimal',
          data: cmdHexa
      };

      constant data = qz.serial.sendData(port, serialData, properties)
          .then(answer => {
              console.log('Data sent successfully:', response);

          })
          .catch(error => {
              console.error('Error sending serial data:', error);
              reject(error);
          });
      
}

in the "rx" parameter I am sending the rx object with the desired response length, if I consult the response from qz.serial.setSerialCallbacks(function (streamEvent) {

   
     if (streamEvent.type !== 'ERROR') {
         console.log(streamEvent.output);
           
     } else {
         console.log(streamEvent.exception);

     }
});

In streamEvent.output the response from my device arrives, however I wanted to know if that response can be consulted when I invoke the sinHexa() function

thank you

Reply all
Reply to author
Forward
0 new messages