Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createSerial(com) {
// eslint-disable-next-line
const Readline = SerialPort.parsers.Readline;
this.port = new SerialPort(
com,
{
lock: false
},
async e => {
if (e instanceof Error) {
console.error(e);
this.setState({
fatalError: true
});
return;
}
await this.getData();
console.log("Connected!");
}
function connect(comName){
logger.log("conncting " + comName);
console.log("conncting " + comName);
const options: any = { baudRate: 115200 };
if(os.type() == "Windows_NT"){
options.hupcl = true;
options.rtscts = true;
}
context.com = new SerialPort(comName, options);
context.com.on('data', (data) => context.data(data));
}
export async function connectEBB(path: string | undefined): Promise {
if (path) {
return new EBB(new SerialPort(path));
} else {
const ebbs = await EBB.list();
if (ebbs.length) {
return new EBB(new SerialPort(ebbs[0]));
} else {
return null;
}
}
}
return new Promise((resolve, reject) => {
const port = new SerialPort(path);
port.on("open", () => {
port.removeAllListeners();
resolve(port);
});
port.on("error", (e) => {
port.removeAllListeners();
reject(e);
});
});
}
stopBits: parseInt(setting.stopbit, 10),
parity: setting.parity,
};
switch(setting.flowcontrol) {
case 'RTS/CTS':
options.rtscts = true;
break;
case 'XON/XOFF':
options.xon = true;
options.xoff = true;
break;
default:
options.xon = false;
options.xoff = false;
}
serial = new SerialPort(setting.device, options);
store.dispatch(serialOpening());
serial.open( err => {
if (err) {
serial = null;
store.dispatch(serialClosed());
store.dispatch(refreshDeviceAction());
}
});
serial.on('open', () => store.dispatch(serialOpened()));
serial.on('close', () => { store.dispatch(serialClosed); serial = null; })
serial.on('error', () => {})
serial.on('data', (buf) => display(buf));
}
}
open(callback) {
if (this.port) {
const err = new Error(`Cannot open serial port "${this.settings.path}"`);
callback(err);
return;
}
const { path, ...rest } = this.settings;
this.port = new SerialPort(path, {
...rest,
autoOpen: false
});
this.port.on('open', this.eventListener.open);
this.port.on('close', this.eventListener.close);
this.port.on('error', this.eventListener.error);
this.parser = this.port.pipe(new Readline({ delimiter: '\n' }));
this.parser.on('data', this.eventListener.data);
this.port.open(callback);
}