| New file |
| | |
| | | // 欧姆龙 HBP-9020 2400 7 无校验 |
| | | import { ElMessage, ElMessageBox, ElNotification } from 'element-plus' |
| | | import { ipcRenderer } from 'electron' |
| | | const { SerialPort } = require('serialport') |
| | | const { DelimiterParser } = require('@serialport/parser-delimiter') |
| | | const { ByteLengthParser } = require('@serialport/parser-byte-length') |
| | | import {sockteStore} from '@/stores/sockteInfo' |
| | | const { InterByteTimeoutParser } = require('@serialport/parser-inter-byte-timeout') |
| | | |
| | | // 设置重连间隔和最大重试次数 |
| | | const RECONNECT_INTERVAL = 10000; // 重连间隔10秒 |
| | | const MAX_RECONNECT_ATTEMPTS = 10; // 最大重试次数10次 |
| | | |
| | | let reconnectAttempts = 0; // 当前重试次数 |
| | | let serialPort:any; // 串口实例 |
| | | |
| | | const initPort=(path:String,baudRate:Number)=>{ |
| | | console.log('初始化打开oumulong-HBP-9020端口',reconnectAttempts) |
| | | if(reconnectAttempts!==0){ |
| | | console.log('ssss',reconnectAttempts) |
| | | ipcRenderer.invoke('logger', '串口重连第${reconnectAttempts}次') |
| | | ElMessage.warning({ |
| | | message: `串口重连第${reconnectAttempts}次启动`, |
| | | type: 'success', |
| | | }) |
| | | } |
| | | if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) { |
| | | console.error('重试次数达到上限,不再尝试重连。'); |
| | | ipcRenderer.invoke('logger', '串口已经重连了${reconnectAttempts}次了,点击确认关闭程序,重启电脑后再试') |
| | | ElMessageBox.confirm( |
| | | `串口已经重连了${reconnectAttempts}次了,点击确认关闭程序,重启电脑后再试`, |
| | | 'Warning', |
| | | { |
| | | confirmButtonText: '确认', |
| | | cancelButtonText: '取消', |
| | | type: 'warning', |
| | | } |
| | | ) |
| | | .then(() => { |
| | | ipcRenderer.send('winClose') |
| | | ipcRenderer.invoke('logger', '确认了关闭程序') |
| | | }) |
| | | .catch(() => { |
| | | ElMessage({ |
| | | type: 'info', |
| | | message: '取消操作', |
| | | }) |
| | | }) |
| | | return; |
| | | } |
| | | |
| | | if (serialPort && serialPort.isOpen) { |
| | | reconnectAttempts=0 |
| | | console.log('串口已打开,不再重复打开。'); |
| | | return; |
| | | } |
| | | try { |
| | | serialPort = new SerialPort({ path, baudRate, dataBits:7 }, (err: any) => { |
| | | reconnectAttempts++ |
| | | if (err) { |
| | | console.log(err) |
| | | ipcRenderer.invoke('logger', '血压计端口打开失败!') |
| | | ElNotification.warning({ |
| | | title: '警告', |
| | | message: '血压计端口打开失败!', |
| | | showClose: false, |
| | | duration:10000 |
| | | }) |
| | | |
| | | setTimeout(()=>{ |
| | | initPort(path,baudRate) |
| | | }, RECONNECT_INTERVAL); |
| | | }else{ |
| | | reconnectAttempts=0 |
| | | ipcRenderer.invoke('logger', '血压计端口打开成功') |
| | | ElMessage.success({ |
| | | message: '血压计端口打开成功', |
| | | type: 'success', |
| | | }) |
| | | } |
| | | }) |
| | | serialPort.on("close",(err: any)=>{ |
| | | ipcRenderer.invoke('logger', 'HBP9030血压计端口异常端口链接关闭') |
| | | console.log('mbp9020血压计端口异常端口链接断开') |
| | | reconnectAttempts=0 |
| | | setTimeout(()=>{ |
| | | initPort(path,baudRate) |
| | | }, RECONNECT_INTERVAL); |
| | | console.log(err) |
| | | }) |
| | | // 解析分割数据流 |
| | | //02 49 44 39 39 39 39 39 39 39 39 42 32 35 2F 3034 2F 31 31 2F 31 32 3A 35 32 20 31 31 36 20 3036 33 20 30 37 36 20 03 |
| | | //ID99999999B25/04/11/12:52 116 063 076 |
| | | const parser = serialPort.pipe(new InterByteTimeoutParser({ interval: 500 ,maxBufferSize:40})) |
| | | parser.on('data', (value: string | any[])=>{ |
| | | const str=value.toString() |
| | | console.log(str) |
| | | ipcRenderer.invoke('logger', '串口消息') |
| | | ipcRenderer.invoke('logger', value) |
| | | ipcRenderer.invoke('logger', value.toString()) |
| | | if(str.length>=38){ |
| | | const gy=str.substring(27,30) |
| | | const dy=str.substring(31,34) |
| | | const mb=str.substring(35,38) |
| | | console.log(gy+','+dy+','+mb,'得到的数据') |
| | | sockteStore().setxyjSockte( |
| | | { |
| | | deviceName:'HBP-9030', |
| | | type:"血压计", |
| | | result:gy+','+dy+','+mb, |
| | | resultTime:new Date().toString(), |
| | | state:2 |
| | | } |
| | | ) |
| | | } |
| | | |
| | | }) |
| | | } |
| | | catch (error:any) { |
| | | console.error('无法创建串口实例:', error.message); |
| | | reconnectAttempts=0 |
| | | setTimeout(()=>{ |
| | | initPort(path,baudRate) |
| | | }, RECONNECT_INTERVAL); |
| | | } |
| | | } |
| | | |
| | | export { |
| | | initPort, |
| | | } |