重点
async closeport ( ) {
this . $emit ( "changeSerialStatus" , false ) ;
this . status = false ;
this . reader. cancel ( ) ;
} ,
vue组件
< template>
< div>
< el- button @click= "setPort" v- if = "!status" > 连接串口< / el- button>
< el- button @click= "closeport" v- if = "status" > 关闭串口< / el- button>
< ! -- < el- button @click= "changeWgtStatus" > 重新称重< / el- button> -- >
< ! -- < label class = "pl15" > 注:当前页面称配置的串口是{ { serialInfo. portName } } < / label> -- >
< / div>
< / template>
< script>
export default {
name : "serialSelect" ,
props : {
serialInfo : {
type : Object,
require : true ,
default : ( ) => { } ,
} ,
} ,
data ( ) {
return {
wgtStatus : true ,
lastWeight : null ,
count : 4 ,
times : 0 ,
port : null ,
reader : null ,
setIntervalName : "" ,
dataFirstTime : null ,
standardTimes : null ,
status : false ,
} ;
} ,
created ( ) {
this . standardTimes = this . serialInfo. dataTime;
} ,
methods : {
retrieveWgt ( ) {
this . wgtStatus = true ;
this . lastWeight = null ;
} ,
changeWgtStatus ( ) {
this . wgtStatus = true ;
this . dataFirstTime = Date. now ( ) ;
this . $emit ( "changeColor" , "red" ) ;
this . $emit ( "getWgt" , null ) ;
this . $emit ( "updateWgt" , true ) ;
} ,
async setPort ( ) {
this . port = await navigator. serial. requestPort ( ) ;
this . getPort ( ) ;
} ,
async getPort ( ) {
let port = this . port;
await port
. open ( { baudRate : this . serialInfo. baudRate } )
. then ( ( res ) => {
this . status = true ;
} )
. catch ( ( res ) => {
this . wgtStatus = false ;
this . status = false ;
this . $emit ( "changeSerialStatus" , false ) ;
console. log ( "error:" , res) ;
} )
. finally ( ( res ) => {
this . wgtStatus = true ;
this . status = true ;
this . $emit ( "changeSerialStatus" , true ) ;
console. log ( "finally:" , res) ;
} ) ;
while ( port. readable && this . status) {
this . reader = port. readable. getReader ( ) ;
try {
while ( true ) {
const { value, done } = await this . reader. read ( ) ;
if ( done) {
this . reader. releaseLock ( ) ;
break ;
}
let _this = this ;
if ( value) {
_this. getwgt ( value) ;
}
}
} catch ( error) {
console. log ( "处理非致命的读错误" , error) ;
} finally {
this . reader. releaseLock ( ) ;
}
}
await port. close ( ) ;
} ,
getwgt ( value ) {
let result = this . Utf8ArrayToStr ( value) ;
console. log ( "收到的数据:" , result, result. includes ( "-" ) ) ;
if ( ! result. includes ( "+" ) && ! result. includes ( "-" ) ) return ;
const flagSymbol = result. includes ( "-" ) ? "-" : "" ;
const resultNum = result. slice ( 1 ) ;
if ( ! resultNum) return ;
let wgt = Number ( resultNum) > 0 ? Number ( resultNum) / 10 : 0 ;
wgt = flagSymbol ? flagSymbol + wgt : wgt;
if ( this . lastWeight == wgt) {
const lastDataTime = Date. now ( ) ;
const dataTime = lastDataTime - this . dataFirstTime;
console. log ( this . wgtStatus, dataTime) ;
if ( ( this . wgtStatus || flagSymbol) && dataTime >= this . standardTimes) {
this . dataFirstTime = Date. now ( ) ;
this . lastWeight = null ;
if ( wgt > 0 ) {
this . wgtStatus = false ;
this . $emit ( "changeColor" , "green" ) ;
} else {
this . wgtStatus = true ;
this . $emit ( "changeColor" , "red" ) ;
}
this . $emit ( "getWgt" , wgt) ;
}
} else {
this . dataFirstTime = Date. now ( ) ;
this . lastWeight = wgt;
if ( this . wgtStatus) {
this . $emit ( "changeColor" , "red" ) ;
this . $emit ( "getWgt" , wgt) ;
}
}
} ,
Utf8ArrayToStr ( array ) {
var out, i, len, c;
var char2, char3;
out = "" ;
len = array. length;
i = 0 ;
while ( i < len) {
c = array[ i++ ] ;
switch ( c >> 4 ) {
case 0 :
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
out += String. fromCharCode ( c) ;
break ;
case 12 :
case 13 :
char2 = array[ i++ ] ;
out += String. fromCharCode ( ( ( c & 0x1f ) << 6 ) | ( char2 & 0x3f ) ) ;
break ;
case 14 :
char2 = array[ i++ ] ;
char3 = array[ i++ ] ;
out += String. fromCharCode (
( ( c & 0x0f ) << 12 ) | ( ( char2 & 0x3f ) << 6 ) | ( ( char3 & 0x3f ) << 0 )
) ;
break ;
}
}
return out;
} ,
async closeport ( ) {
this . $emit ( "changeSerialStatus" , false ) ;
this . status = false ;
this . reader. cancel ( ) ;
} ,
} ,
beforeDestroy ( ) {
console. log ( "离开" ) ;
this . closeport ( ) ;
} ,
} ;
< / script>