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
| import { ElMessage } from 'element-plus'
| const { SerialPort } = require('serialport')
| const { DelimiterParser } = require('@serialport/parser-delimiter')
|
| let serialport={}
| // 解析器分隔符
| const buf=Buffer.from('A555','hex')
| const hexString = 'A55501FB0D0A';
| const hexBuffer = Buffer.from(hexString, 'hex');
|
| const initPort=(path:String,baudRate:Number)=>{
| console.log('初始化打开测温端口')
| serialport = new SerialPort({ path, baudRate }, (err: any) => {
| if (err) {
| console.log(err)
| ElMessage({
| message: '端口打开失败!',
| type: 'error',
| })
| console.log(err)
| }else{
| ElMessage({
| message: '端口打开成功',
| type: 'success',
| })
| setInterval(()=>{
| serialport.write(hexBuffer,(err: any)=>{
| if(err){
| console.log('发送失败')
| }
| })
| },3000)
| }
| })
| serialport.on("close",(err: any)=>{
| console.log('端口异常端口链接断开')
| console.log(err)
| })
| // 解析分割数据流
| const parser = serialport.pipe(new DelimiterParser({
| delimiter:buf
| }))
| parser.on('data', (value: string | any[])=>{
| if(value.length>=5){
| const hex4e=value[0]
| const hex0e=value[1]
| const wendu=(hex4e+hex0e*256)/100
| ElMessage({
| message: '收到温度:'+wendu,
| type: 'success',
| })
| }
| })
| }
| const sundGetKey=()=>{
| const hexString = 'A55501FB';
| const hexBuffer = Buffer.from(hexString, 'hex');
| if(serialport){
| console.log('串口没有初始化')
| return false
| }
| serialport.write(hexBuffer,(err: any)=>{
| if(err){
| console.log('发送失败')
| }
| ElMessage({
| message: '发送KEY成功',
| type: 'success',
| })
| })
| }
|
|
|
| export {
| initPort,
| sundGetKey
| }
|
|