35#ifndef _UTILS_JSSERIALPORT_HXX_
36#define _UTILS_JSSERIALPORT_HXX_
40#include <emscripten.h>
41#include <emscripten/val.h>
54 std::function<
void()> cb = []() {})
56 , connectCallback_(std::move(cb))
58 string script =
"Module.serial_device = '" + device +
"';\n";
59 emscripten_run_script(script.c_str());
62 var SerialPort = require(
'serialport');
63 var portdev = Module.serial_device;
64 console.log(
'Opening ' + portdev);
65 var openerror = function(error) {
67 'Failed to open serial port ' + portdev +
': ' + error);
68 console.log(
'Known serial ports:');
69 require(
'@serialport/bindings')
71 .then(function(ports) {
72 ports.forEach(function(port) {
73 console.log(
'port "' + port.path +
74 '" pnp id: ' + port.pnpId +
75 ' manufacturer: ' + port.manufacturer);
82 Module.serial_device, {baudRate : 115200});
87 c.on(
"open", function(error) {
92 console.log(
'opened ' + c);
93 var client_port =
new Module.JSHubPort(
94 $0, function(data) { c.write(data); });
95 c.on(
'close', function() {
96 console.log(
'serial port ' + portdev +
' closed.');
97 client_port.abandon();
99 c.on(
'error', function(err) {
100 console.log(
'error on serial port ' + portdev +
': ' + err);
102 client_port.abandon();
104 c.on(
'data', function(data) { client_port.recv(data.toString()); });
108 (
unsigned long)canHub_, (
unsigned long)&connectCallback_);
111 static void list_ports() {
114 console.log(
'Known serial ports:');
115 require(
'@serialport/bindings').list().then(function(ports)
117 ports.forEach(function(port)
119 console.log(
'port "' + port.path +
120 '" pnp id: ' + port.pnpId +
121 ' manufacturer: ' + port.manufacturer);
130 std::function<void()> connectCallback_;