From a7fb2f5eab3e548f040004c1019f1c994eb91ad4 Mon Sep 17 00:00:00 2001
From: chenyc <501753378@qq.com>
Date: 星期二, 09 十二月 2025 14:44:36 +0800
Subject: [PATCH] 日常更新

---
 src/samples/deviceApi/RBP-9000c.ts |  196 +++++++++++++++++++++++++++++++++++-------------
 1 files changed, 142 insertions(+), 54 deletions(-)

diff --git a/src/samples/deviceApi/RBP-9000c.ts b/src/samples/deviceApi/RBP-9000c.ts
index f6dd278..91cd604 100644
--- a/src/samples/deviceApi/RBP-9000c.ts
+++ b/src/samples/deviceApi/RBP-9000c.ts
@@ -1,68 +1,156 @@
 // 脉搏波 rbp-9000c 115200 
-import { ElMessage } from 'element-plus'
+import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'
 const { SerialPort } = require('serialport')
 const { DelimiterParser } = require('@serialport/parser-delimiter')
 const { ByteLengthParser } = require('@serialport/parser-byte-length')
 import {sockteStore} from '@/stores/sockteInfo'
+import { ipcRenderer } from 'electron'
 
-
+// 设置重连间隔和最大重试次数
+const RECONNECT_INTERVAL = 10000; // 重连间隔10秒
+const MAX_RECONNECT_ATTEMPTS = 10; // 最大重试次数10次
+ 
+let reconnectAttempts = 0; // 当前重试次数
+let serialPort:any; // 串口实例
 const initPort=(path:String,baudRate:Number)=>{
-    const serialport  = new SerialPort({ path, baudRate }, (err: any) => {
-        if (err) {
-            console.log(err)
-            ElMessage({
-                message: '端口打开失败!',
-                type: 'error',
+    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', '确认了关闭程序')
             })
-            console.log(err)
-        }else{
-            ElMessage({
-                message: 'rbp-9000c端口打开成功',
-                type: 'success',
+            .catch(() => {
+              ElMessage({
+                type: 'info',
+                message: '取消操作',
+              })
             })
-        } 
-    })
-    serialport.on("close",(err: any)=>{
-        console.log('端口异常端口链接断开')
-        console.log(err)
-    })
-    //                 AA E0 03 28 64 02 71 06 44 51 A2 01 84 40 40 C0 5D D0
-    // 解析分割数据流   AA 80 09 0F 01 06 00 17 05 0E 10 11 0F 00 8D 00 44 00 56 8C
-    //AA 80 09 0F 01 06 00 17 05 0E 0F 37 37 00 74 00 46 00 50 70
-    const parser = serialport.pipe(new ByteLengthParser({ length: 20 }))
-    // parser.on('data', (value: string | any[])=>{
-    //     console.log(value.toString())
-    //     const str=value.toString()
-    //     const list=str.split(',')
-    //     console.log(list)
-    //     if(list.length===11){
-    //         sockteStore().setxyjSockte(
-    //           {
-    //               deviceName:'HBP-9030',
-    //               type:"血压计",
-    //               result:list[14]+','+list[16]+','+list[18],
-    //               resultTime:new Date().toString(),
-    //               state:2
-    //           }
-    //       )
-    //     }
+        return;
+    }
+    
+    if (serialPort && serialPort.isOpen) {
+        reconnectAttempts=0
+        console.log('串口已打开,不再重复打开。');
+        return;
+    }
+    try {
+        serialPort  = new SerialPort({ path, baudRate }, (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', ' 血压计端口异常端口链接关闭')
+            console.log(' 血压计端口异常端口链接断开')
+            reconnectAttempts=0
+            setTimeout(()=>{
+                initPort(path,baudRate)
+            }, RECONNECT_INTERVAL);
+            console.log(err)
+        })
+        //                 AA E0 03 28 64 02 71 06 44 51 A2 01 84 40 40 C0 5D D0
+        // 解析分割数据流   AA 80 09 0F 01 06 00 17 05 0E 10 11 0F 00 8D 00 44 00 56 8C
+        //AA 80 09 0F 01 06 00 17 05 0E 0F 37 37 00 74 00 46 00 50 70
+        // 9600
+        const parser = serialPort.pipe(new ByteLengthParser({ length: 20 }))
+        parser.on('data',(value: string | any[]) =>{
+            if(value.length>=20){
+                sockteStore().setxyjSockte(
+                    {
+                        deviceName:'RBP-9000c',
+                        type:"血压计",
+                        result:value[14]+','+value[16]+','+value[18],
+                        resultTime:new Date().toString(),
+                        state:2
+                    }
+                )
+            }
+            console.log(value,'血压计数据')
+        }) 
         
+    }
+    catch (error:any) {
+        console.error('无法创建串口实例:', error.message);
+        reconnectAttempts=0
+        setTimeout(()=>{
+            initPort(path,baudRate)
+        }, RECONNECT_INTERVAL);
+    }
+    // const serialport  = new SerialPort({ path, baudRate }, (err: any) => {
+    //     if (err) {
+    //         console.log(err)
+    //         ElMessage({
+    //             message: '端口打开失败!',
+    //             type: 'error',
+    //         })
+    //         console.log(err)
+    //     }else{
+    //         ElMessage({
+    //             message: 'rbp-9000c端口打开成功',
+    //             type: 'success',
+    //         })
+    //     } 
     // })
-    parser.on('data',(value: string | any[]) =>{
-        console.log('-----------------')
-        if(value.length>=20){
-            sockteStore().setxyjSockte(
-                {
-                    deviceName:'HBP-9030',
-                    type:"血压计",
-                    result:value[14]+','+value[16]+','+value[18],
-                    resultTime:new Date().toString(),
-                    state:2
-                }
-            )
-        }
-        console.log(value,'血压计数据')
-    }) 
+    // serialport.on("close",(err: any)=>{
+    //     console.log('端口异常端口链接断开')
+    //     console.log(err)
+    // })
+    // //                 AA E0 03 28 64 02 71 06 44 51 A2 01 84 40 40 C0 5D D0
+    // // 解析分割数据流   AA 80 09 0F 01 06 00 17 05 0E 10 11 0F 00 8D 00 44 00 56 8C
+    // //AA 80 09 0F 01 06 00 17 05 0E 0F 37 37 00 74 00 46 00 50 70
+    // const parser = serialport.pipe(new ByteLengthParser({ length: 20 }))
+    // parser.on('data',(value: string | any[]) =>{
+    //     console.log('-----------------')
+    //     if(value.length>=20){
+    //         sockteStore().setxyjSockte(
+    //             {
+    //                 deviceName:'RBP-9000c',
+    //                 type:"血压计",
+    //                 result:value[14]+','+value[16]+','+value[18],
+    //                 resultTime:new Date().toString(),
+    //                 state:2
+    //             }
+    //         )
+    //     }
+    //     console.log(value,'血压计数据')
+    // }) 
 }
 
 export {

--
Gitblit v1.8.0