首页 > 其他分享 >vue2扫码枪串口模式的使用

vue2扫码枪串口模式的使用

时间:2024-03-21 11:45:06浏览次数:148  
标签:codeGun function 扫码 console log entity vue2 串口 data

1.下载依赖包  serialport  

npm i serialport

2.创建文件code-gun.js

var { SerialPort } = require("serialport");

// 串口列表
SerialPort.list()
  .then((ports) => {
    ports.forEach((port) => {
      console.log(port);
    });
  })
  .catch((err) => {
    console.error("无法获取串口列表", err);
  });

  
const codeGun = {
  entity: null,
  isOpen: false,
  error: null,
  data: null,
};
codeGun.init = function (option, callback) { 
  codeGun.entity &&
    typeof codeGun.entity.close === "function" &&
    codeGun.entity.close();  
  codeGun.entity = new SerialPort(option);
  console.log(codeGun.entity)
  //连接串口后进行写入指令操作
  codeGun.entity.open(function (err) {
    if (!err) {
      console.log("扫码枪连接ok");
      codeGun.isOpen = codeGun.entity.isOpen;
    } else {
      console.log("扫码枪连接失败!");
      console.log(err);
    }
  });
  // close
  codeGun.entity.on("close", function () { 
    codeGun.isOpen = codeGun.entity.isOpen;
  });
  // //指令监听
  codeGun.entity.on("data", function (data) {
    let str = new String(data);
    console.log(str)
    codeGun.data = str.toString();
    typeof callback === "function" && callback();
  });
  // //错误监听
  codeGun.entity.on("error", function (error) {
    console.log("error: " + error);
  });
  return codeGun;
};
codeGun.close = function () { 
  codeGun.isOpen = false;
  codeGun.entity && codeGun.entity.close(); 
};

export default codeGun;

3.App.vue 初始化

import codeGun from "@/utils/code-gun"

data(){
    return{
      codeScanGun: {
        data: null,
        isOpen: false,
        timer:null,
      }
    }
  },

watch: { 
    "codeScanGun.data": {
      handler: function(val) {
        if(val) {
          // 设置当前focus的input元素的值
          let activeElement = document.activeElement;
          if(activeElement.tagName.toUpperCase() === 'INPUT') {
            activeElement.value = val
            // 手动触发input事件,实现数据绑定
            let event = new InputEvent('input', { bubbles: true })
            activeElement.dispatchEvent(event);
          }
          this.$nextTick(() => {
            // 重置扫码枪的值
            this.codeScanGun.data = ""
          })
        }
      }
    },
    "codeScanGun.isOpen": {
      handler: function(val) { 
        if(!val) {
          console.log("扫码枪断开连接!")
          this.timer && clearInterval(this.timer)
          this.timer = setInterval(()=>{
            this.initSMQ()
          },2000)
        }else{ 
          this.timer && clearInterval(this.timer)
        }
      } 
    }
  },    

created(){   
    this.codeScanGun = codeGun.init({
         path:"com1",//串口号
         baudRate:9600,//波特率
         autoOpen:false
     });
  },

  

标签:codeGun,function,扫码,console,log,entity,vue2,串口,data
From: https://www.cnblogs.com/daifuchao/p/18087001

相关文章

  • vue2 vue-print-nb
    一、安装插件1、npminstallvue-print-nb--save二、引入Vue项目在main.js中添加--全局挂载  importPrintfrom'vue-print-nb'  Vue.use(Print) 三、前端代码一、操作项中的打印按钮<spantitle="打印"><svg-iconicon-class="printer"class=&quo......
  • STM32发送串口数据丢失字节的解决办法
    发送数据函数voidUsart3_Send_Array(u8*buf,u8len){u8t;GPIO_WriteBit(GPIOB,RS485AB_EN_PIN,1);for(t=0;t<len;t++) //循环发送数据{while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);USART_SendData(USART3,......
  • 03-【K210】通用串口和高速串口
    目录null01串口通讯协议简介02K210的串口类型(1)高速UART(2)通用UART03通用UART测试例程04高速串口测试例程01串口通讯协议简介串口通讯(SerialCommunication)是一种设备间非常常用的串行通讯方式,因为它简单便捷,大部分电子设备都支持该通讯方式,电子工程师在调试设备时也经常......
  • vue2项目升级vue3的小变化
    1、template的slot写法变化vue2:<templateslot-scope="{row,index}"slot="action">vue3:<template#action="{row,index}">2、路由页面缓存的写法变化vue2:<keep-alive:include="cacheList"><router-view/>......
  • FPGA之串口接收数据(看注释)
    兜兜转转看了好多家视频和好几本书,明白了FPGA难学的原因之一是因为讲的好(我觉得就是很详细,告诉你为什么这么来写代码)的视频比较少,之前看到的那本书其实也很好,只是没有说为什么这么写,以及某些步骤的用意,这次看了野火的视频,发现挺符合我的需求,他们视频和我借的那本书思路是相同的,野......
  • vue2 上传图片
    一、前端<el-row><el-form-itemlabel="上传文件"props="fileList"><el-uploadref="upload"action="#"list-type="......
  • vue2结合element UI组件库封装的搜索组件
    可以根据不同的搜索条件自动排版,分为一个搜索条件,2-4个搜索条件,大于5的搜索条件具体样例见下方 封装的组件库:el-seacher.vue<template> <divv-if="!isModalSearch"class="searchFormborder-basic":class="isHeaderSearch?'headerBack':'whiteBtnBgd'&q......
  • Vue2(五):收集表单数据、过滤器、自定义指令、Vue的生命周期
    一、收集表单数据爱好:学习<inputtype="checkbox"value="study"v-model="hobby">打游戏<inputtype="checkbox"value="games"v-model="hobby">吃饭<inputtype="checkbo......
  • vue面试题(vue2响应式源码剖析)
    一、前言这篇文章结合Vue2.7.16的源码和一个Vue2的项目,来详细讲解Vue2实现响应式数据的核心代码1.1准备安装@vue/clinpminstall-g@vue/cli创建vue项目vuecreatevue2-test修改Vue实例的配置对象二、响应式处理的入口通过newVue()调用Vue构造函数,......
  • vue面试题(vue2响应式数据基础)
    一、什么是响应式数据响应式数据是指当数据发生变化时,相关的视图或组件会自动更新,保持与数据的同步。这样的设计使得开发者能够更方便地管理和更新数据,无需手动操作DOM或显式地更新视图。当数据发生变化时,所有使用该数据的地方都会自动更新。二、观察者模式观察者模式定义对......