chenyc
2023-11-14 7394e5e60ec25ede11d1ef88358454da8f9c7390
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
// 6.诊之助 TM2655VP
import { ElMessage } 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'
 
 
const initPort=(path:String,baudRate:Number)=>{
    console.log('初始化打开诊之助 TM2655VP端口')
    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,
}