|
1 | 1 | import { SWORTTSource } from './common';
|
2 | 2 | import { EventEmitter } from 'events';
|
3 |
| -import * as fs from 'fs'; |
4 |
| -import * as os from 'os'; |
| 3 | +import type { SerialPort } from 'serialport'; |
5 | 4 | import * as vscode from 'vscode';
|
6 |
| -import * as path from 'path'; |
7 |
| - |
8 |
| -export function findSerialPortModuleHelp(extensionPath: string) { |
9 |
| - return 'Node/npm module "serialport" not found. You can install this in one of two ways\n' + |
10 |
| - '1. Install "Serial Monitor" VSCode extension. https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-serial-monitor\n' + |
11 |
| - '2. or, you can compile the serialport module locally on your computer. Follow these instructions on a shell prompt\n' + |
12 |
| - ` cd ${extensionPath}/binary_modules\n` + |
13 |
| - ` bash ./build.sh ${process.versions['electron']}\n` + |
14 |
| - 'If you chose to compile locally, make sure NodeJS is installed on your system. Visit https://nodejs.org/en/download/'; |
15 |
| -} |
16 |
| - |
17 |
| -export function findSerialPortModule(extensionPath: string, useModule) { |
18 |
| - const paths = []; |
19 |
| - const p = path.normalize(path.join(extensionPath, 'binary_modules', 'electron-' + process.versions['electron'], 'node_modules')); |
20 |
| - if (fs.existsSync(p) && fs.existsSync(path.join(p, 'serialport'))) { |
21 |
| - paths.push(p); |
22 |
| - } else { |
23 |
| - const serMonitorExt = 'ms-vscode.vscode-serial-monitor'; |
24 |
| - const serialMonitor: vscode.Extension<any> = vscode.extensions.getExtension(serMonitorExt); |
25 |
| - if (serialMonitor) { |
26 |
| - paths.push(path.join(serialMonitor.extensionPath, 'dist', 'node_modules')); |
27 |
| - paths.push(path.join(serialMonitor.extensionPath, 'node_modules')); |
28 |
| - } |
29 |
| - } |
30 |
| - |
31 |
| - let added = false; |
32 |
| - for (const p of paths) { |
33 |
| - if (fs.existsSync(path.join(p, 'serialport'))) { |
34 |
| - if (useModule.paths.indexOf(p) === -1) { |
35 |
| - console.log(`Adding ${p} to module search path`); |
36 |
| - useModule.paths.push(p); |
37 |
| - } |
38 |
| - added = true; |
39 |
| - } |
40 |
| - } |
41 |
| - return added; |
42 |
| -} |
43 |
| - |
44 |
| -declare function __webpack_require__(name: string): any; |
45 |
| -declare function __non_webpack_require__(name: string): any; |
46 | 5 |
|
47 | 6 | export class SerialSWOSource extends EventEmitter implements SWORTTSource {
|
48 |
| - private serialPort: any = null; |
| 7 | + private serialPort: SerialPort = null; |
49 | 8 | public connected: boolean = false;
|
50 | 9 |
|
51 |
| - constructor(private device: string, private baudRate: number, extensionPath: string) { |
| 10 | + constructor(private device: string, private baudRate: number) { |
52 | 11 | super();
|
53 | 12 |
|
54 |
| - /* While this is a bit ugly - it works around WebPack's mangling of the require statements. eval('require.main') gets us |
55 |
| - the main module in a non-mangled form (instead of the current module - but that is not important for our purposes here) |
56 |
| - and allows us to modify the paths and load in the serial port from there. We have to wrap it in an eval statement to avoid |
57 |
| - webpack mangling */ |
58 |
| - // tslint:disable-next-line: no-eval |
59 |
| - const mainModule = eval('require.main'); |
60 |
| - const added = findSerialPortModule(extensionPath, mainModule); |
61 |
| - if (!added) { |
62 |
| - vscode.window.showErrorMessage(findSerialPortModuleHelp(extensionPath)); |
63 |
| - return; |
64 |
| - } |
65 |
| - |
66 |
| - let SerialPort; |
67 |
| - try { |
68 |
| - SerialPort = mainModule.require('serialport').SerialPort; |
69 |
| - if (!SerialPort) { |
70 |
| - vscode.window.showErrorMessage(findSerialPortModuleHelp(extensionPath)); |
71 |
| - return; |
72 |
| - } |
73 |
| - } |
74 |
| - catch (e) { |
75 |
| - vscode.window.showErrorMessage(findSerialPortModuleHelp(extensionPath)); |
76 |
| - return; |
77 |
| - } |
78 |
| - |
79 |
| - this.serialPort = new SerialPort(device, { baudRate: baudRate, autoOpen: false }); |
| 13 | + const { SerialPort } = require('serialport'); |
| 14 | + this.serialPort = new SerialPort({ path: device, baudRate: baudRate, autoOpen: false }); |
80 | 15 | this.serialPort.on('data', (buffer) => {
|
81 | 16 | this.emit('data', buffer);
|
82 | 17 | });
|
|
0 commit comments