From ccedcc15a84a2c59ec13e1ae70e6843d3c7ddbc4 Mon Sep 17 00:00:00 2001
From: chenyc <501753378@qq.com>
Date: 星期六, 23 十一月 2024 13:55:29 +0800
Subject: [PATCH] 34
---
src/views/home/index.vue | 4 +
src/samples/deviceApi/xinanguojiTZC.ts | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 165 insertions(+), 0 deletions(-)
diff --git a/src/samples/deviceApi/xinanguojiTZC.ts b/src/samples/deviceApi/xinanguojiTZC.ts
new file mode 100644
index 0000000..795e95c
--- /dev/null
+++ b/src/samples/deviceApi/xinanguojiTZC.ts
@@ -0,0 +1,161 @@
+import { ElMessage, ElMessageBox } from 'element-plus'
+const { SerialPort } = require('serialport')
+const { DelimiterParser } = require('@serialport/parser-delimiter')
+const { ReadyParser } = require('@serialport/parser-ready')
+import { sockteStore } from '@/stores/sockteInfo'
+import { ipcRenderer } from 'electron'
+import console from 'console'
+
+
+// 设置重连间隔和最大重试次数
+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('初始化打开新安国际体重秤体重秤端口')
+ // 是否清零了
+ let sfqingling=true
+ let lisijieguo=''
+ let duibinum=0
+ if (reconnectAttempts !== 0) {
+ console.log('ssss', reconnectAttempts)
+ ipcRenderer.invoke('logger', '串口重连第${reconnectAttempts}次')
+ ElMessage({
+ 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', '新安国际体重秤端口打开失败!')
+ ElMessage({
+ message: '新安国际体重秤端口打开失败!',
+ type: 'error',
+ })
+ 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)
+ })
+ // 解析分割数据流
+
+ //80 02 23 30 20 30 30 30 36 35 32 30 30 30 30 30 30 0D
+ //80 02 23 30 20 30 30 30 36 35 32 30 30 30 30 30 30 0D
+ //80 02 23 30 20 30 30 30 36 35 32 30 30 30 30 30 30 0D
+ //€#0 000652000000
+
+
+ const parser = serialport.pipe(new DelimiterParser({ delimiter: Buffer.from([13]), includeDelimiter: true }))
+ parser.on('data', (value: string | any[]) => {
+ console.log(value.toString())
+ const str = value.toString()
+ // 截取结果
+ // const list=str.substring(2,10) 得到数字
+ const cd=str.length
+ const zhengshu = str.substring(cd-11,cd-8)
+ const xiaoshu = str.substring(cd-8,cd-7)
+ console.log(zhengshu,xiaoshu)
+ const list=`${zhengshu}.${xiaoshu}`
+ console.log(list)
+ if (Number(list) > 20) {
+ // 判断是否相等 相等加一
+ if (list === lisijieguo) {
+ duibinum++
+ } else {
+ lisijieguo = list
+ duibinum = 0
+ }
+ // 对比4次结果都一致
+ if (duibinum >= 3) {
+ // 数据发送 体重没有清零 下次就不播报不发送
+ sfqingling = false
+ const res = Number(list)
+ console.log(res, '获取到的体重')
+ sockteStore().setweightSockte(
+ {
+ deviceName: 'XK3190-A12',
+ type: "体重秤",
+ result: res.toString(),
+ resultTime: new Date().toString(),
+ state: 2
+ }
+ )
+ duibinum = 0
+ lisijieguo = ''
+ } else {
+ }
+
+
+
+ } else {
+ sfqingling = true
+ console.log('小于20')
+ }
+ })
+ }
+ catch (error: any) {
+ console.error('无法创建串口实例:', error.message);
+ reconnectAttempts = 0
+ setTimeout(() => {
+ initPort(path, baudRate)
+ }, RECONNECT_INTERVAL);
+ }
+
+
+}
+
+export {
+ initPort,
+}
diff --git a/src/views/home/index.vue b/src/views/home/index.vue
index 3bf2ed6..63e2fec 100644
--- a/src/views/home/index.vue
+++ b/src/views/home/index.vue
@@ -292,6 +292,8 @@
import {initPort as seca102} from '@/samples/deviceApi/seca102'
import {initPort as seca101Banger} from '@/samples/deviceApi/seca101Banger'
import {initPort as liangjiang} from '@/samples/deviceApi/liangjiang'
+import {initPort as xinanguojiTZC} from '@/samples/deviceApi/xinanguojiTZC'
+
// 引入模块
import config from '../../../package.json'
import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue'
@@ -1019,6 +1021,8 @@
zhiRongHehui(configData.value.tzcPortPath,configData.value.tzcBaudRate)
}else if(configData.value.tzc_type==='lianjiang'){
liangjiang(configData.value.tzcPortPath,1200)
+ }else if(configData.value.tzc_type==='xinanguojiTZC'){
+ xinanguojiTZC(configData.value.tzcPortPath,9600)
}
else{
zhiRongT605(configData.value.tzcPortPath,configData.value.tzcBaudRate)
--
Gitblit v1.8.0