chenyc
2025-12-09 a7fb2f5eab3e548f040004c1019f1c994eb91ad4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 6.诊之助 TM2655VP
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)=>{
    console.log('初始化打开诊之助 TM2655VP端口')
    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 {
        const 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({
                    message: '诊之助端口打开成功',
                    type: 'success',
                })
            } 
        })
        serialport.on("close",(err: any)=>{
            ipcRenderer.invoke('logger', '诊之助端口异常端口链接关闭')
            console.log('诊之助端口异常端口链接断开')
            reconnectAttempts=0
            setTimeout(()=>{
                initPort(path,baudRate)
            }, RECONNECT_INTERVAL);
            console.log(err)
        })
         // // 解析分割数据流
        const parser = serialport.pipe(new ByteLengthParser({ length: 32*2 }))
        parser.on('data', (value: string | any[])=>{
            console.log(value.toString())
            const str=value.toString()
            console.log(str.length)
            const re=str.substring(34,37)+','+str.substring(39,41)+','+str.substring(44,46)
            console.log(re)
            if(str.length>=11){
                sockteStore().setxyjSockte(
                {
                    deviceName:'TM2655VP',
                    type:"血压计",
                    result:str.substring(34,37)+','+str.substring(44,47)+','+str.substring(49,52),
                    resultTime:new Date().toString(),
                    state:2
                }
            )
            }
            
        })
        
    }
    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: '诊之助 TM2655VP端口打开失败!',
    //             type: 'error',
    //         })
    //         console.log(err)
    //     }else{
    //         ElMessage({
    //             message: '诊之助 TM2655VP端口打开成功',
    //             type: 'success',
    //         })
    //     } 
    // })
    // serialport.on("close",(err: any)=>{
    //     console.log('诊之助 TM2655VP端口异常端口链接断开')
    //     console.log(err)
    // })
    // // 解析分割数据流
    // const parser = serialport.pipe(new ByteLengthParser({ length: 32*2 }))
    // parser.on('data', (value: string | any[])=>{
    //     console.log(value.toString())
    //     const str=value.toString()
    //     console.log(str.length)
    //     const re=str.substring(34,37)+','+str.substring(39,41)+','+str.substring(44,46)
    //     console.log(re)
    //     if(str.length>=11){
    //         sockteStore().setxyjSockte(
    //           {
    //               deviceName:'TM2655VP',
    //               type:"血压计",
    //               result:str.substring(34,37)+','+str.substring(44,47)+','+str.substring(49,52),
    //               resultTime:new Date().toString(),
    //               state:2
    //           }
    //       )
    //     }
        
    // })
}
 
export {
    initPort,
}