添加串口处理
parent
1f596c6ed3
commit
811389a1f0
|
@ -16,6 +16,8 @@
|
|||
"echarts-china-cities-js": "^0.1.1",
|
||||
"echarts-gl": "^2.0.8",
|
||||
"echarts-liquidfill": "^3.1.0",
|
||||
"electron-store": "^8.0.1",
|
||||
"serialport": "^9.2.5",
|
||||
"vue": "^3.0.5",
|
||||
"vue-echarts": "^6.0.0"
|
||||
},
|
||||
|
@ -24,8 +26,9 @@
|
|||
"@vue/compiler-sfc": "^3.0.5",
|
||||
"autoprefixer": "^10.2.5",
|
||||
"cross-env": "^7.0.3",
|
||||
"electron": "^12.0.0",
|
||||
"electron": "^13.5.2",
|
||||
"electron-builder": "^22.10.5",
|
||||
"electron-rebuild": "^2.3.4",
|
||||
"postcss": "^8.2.10",
|
||||
"rimraf": "^3.0.2",
|
||||
"tailwindcss": "^2.1.1",
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { app, BrowserWindow } from 'electron'
|
||||
import {join} from "path";
|
||||
import './load-serialport'
|
||||
|
||||
function createWindow() {
|
||||
const win = new BrowserWindow({
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
const Store = require('electron-store')
|
||||
|
||||
const store = new Store()
|
||||
const set = (key, val) => store.set(key, val)
|
||||
|
||||
const get = (key) => store.get(key)
|
||||
|
||||
module.exports = { set, get }
|
|
@ -0,0 +1,46 @@
|
|||
const { ipcMain } = require('electron')
|
||||
const SerialPort = require('serialport')
|
||||
const Readline = require('@serialport/parser-readline')
|
||||
const config = require('./config')
|
||||
|
||||
const DEFAULT_BAUDRATE = 9600
|
||||
const getSerialPortConstructorArguments = (ports) => {
|
||||
let serialport = config.get('serialport')
|
||||
const path = ( serialport && serialport.path ) ? serialport.path : ports[0].path
|
||||
const baudRate = ( serialport && serialport.baudRate ) ? serialport.baudRate : DEFAULT_BAUDRATE
|
||||
return { path, baudRate }
|
||||
}
|
||||
|
||||
let sender = null
|
||||
ipcMain.on("APP_MOUNTED", (event) => {
|
||||
sender = event.sender
|
||||
})
|
||||
|
||||
const sendMsg = (channel, msg) => {
|
||||
if (sender) sender.send(channel, msg)
|
||||
}
|
||||
|
||||
SerialPort.list().then((ports, err) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
return
|
||||
}
|
||||
|
||||
console.log(ports, '=====================')
|
||||
|
||||
// 过滤到COM1口,很多电脑自带COM1接口
|
||||
ports.filter((port) => port.path != 'COM1')
|
||||
if (ports.length == 0) {
|
||||
console.error('设备为连接')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const { path, baudRate } = getSerialPortConstructorArguments(ports)
|
||||
const serialPort = new SerialPort(path, { baudRate })
|
||||
const parser = serialPort.pipe(new Readline({ delimiter: '\r\n' }))
|
||||
parser.on('data', (data) => { sendMsg('NMEA', data) })
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue