ccj 3 years ago
parent
commit
3d526b2c52

+ 14 - 5
src/views/home/index.vue

@@ -554,19 +554,28 @@ export default {
         // 监听来自串行设备的数据
         while (true) {
           const { value, done } = await this.reader.read();
-          // console.log(value,done);
+          // console.log("value",value);
           if (done) {
             // 允许稍后关闭串口。
             this.reader.releaseLock();
             break;
           }
           var result="";
-          
+          //2。获取16进制字符串
+          // var receData = HexConvert.ByteToString(value);
+          // console.log("receData",receData);
+          var flag = false;
           for(var i=0;i<value.length;i++){
-            result += String.fromCharCode(value[i])
-            console.log(result);
+            var tmp = String.fromCharCode(value[i])
+            if(tmp == "+"){
+              flag = true
+            }
+            if(flag && result.length <7){
+              result += tmp
+            }
           }
-          this.text = result
+          this.text = parseInt(result)
+          setTimeout(1000)
           // value 是一个 Uint8Array
         }
          await port.close();

+ 60 - 7
src/views/houseSelfCollect/weightCheck.vue

@@ -88,7 +88,7 @@
             <el-button
               type="primary"
               :class="tpyeNo != 2 ? '' : 'butCss'"
-              @click="chongxin"
+              @click="openPort"
               >重新获取</el-button
             >
             <ws-form-item
@@ -103,9 +103,10 @@
                 placeholder="未获取到皮重"
                 maxlength="100"
                 size="small"
-                :disabled="disabled"/>
+                :disabled="!disabled"
+                />
             </ws-form-item>
-            <el-button type="primary" v-if="this.tpyeNo == 2">重新获取</el-button>
+            <el-button type="primary" v-if="this.tpyeNo == 2" @click="openPort">重新获取</el-button>
             <div class="formItem">
               <ws-form-item
                 label="扣杂(公斤)"
@@ -188,6 +189,7 @@ export default {
       information: '',
       weighingList: {
         grossWeight: '',
+        tare: '',
         buckleMiscellaneous: 0,
       },
       tpyeNo: '',
@@ -196,7 +198,9 @@ export default {
       warehouseId: '',
       purchasePriceList: [],
       isShowPrint: false,
-      tableData:{}
+      tableData:{},
+      reader:null,
+      param:9600
     }
   },
   activated() {
@@ -225,10 +229,59 @@ export default {
       this.information = '皮重'
     }
   },
+  deactivated(){
+    // this.closePort()
+  },
   methods: {
-    chongxin() {
-      if (this.tpyeNo == 2) {
-        return
+    async closePort(){
+      console.log('closePort');
+      this.reader.cancel()
+    },
+    async openPort(){
+      console.log('openPort',navigator);
+      if ('serial' in navigator) {
+        // The Web Serial API is supported.
+        console.log('the Web Serial API is supported.');
+        console.log(this.param);
+        const port = await navigator.serial.requestPort();
+        await port.open({ baudRate: this.param }); // set baud rate
+        this.reader = port.readable.getReader();
+        // 监听来自串行设备的数据
+        while (true) {
+          const { value, done } = await this.reader.read();
+          // console.log("value",value);
+          if (done) {
+            // 允许稍后关闭串口。
+            this.reader.releaseLock();
+            break;
+          }
+          var result="";
+          //2。获取16进制字符串
+          // var receData = HexConvert.ByteToString(value);
+          // console.log("receData",receData);
+          var flag = false;
+          for(var i=0;i<value.length;i++){
+            var tmp = String.fromCharCode(value[i])
+            if(tmp == "+"){
+              flag = true
+            }
+            if(flag && result.length <6 && tmp != "+"){
+              result += tmp
+            }
+          }
+          if(this.tpyeNo != 2){
+            this.weighingList.grossWeight = parseInt(result)
+          }
+          else{
+            this.weighingList.tare = parseInt(result) 
+          }
+          setTimeout(1000)
+          // value 是一个 Uint8Array
+        }
+         await port.close();
+      }
+      else{
+        console.log('the Web Serial API is not supported.',navigator);
       }
     },
     cancel() {

+ 55 - 5
src/views/outboundManagement/weighing.vue

@@ -50,14 +50,14 @@
 							<ws-input v-model="weighingList.grossWeight" @input="calculation" placeholder="未获取到毛重"
 								maxlength="100" size="small" :disabled="disabled2" />
 						</ws-form-item>
-						<el-button type="primary" v-if="this.types == 1" @click="chongxin">重新获取</el-button>
+						<el-button type="primary" v-if="this.types == 1" @click="openPort">重新获取</el-button>
 
 						<ws-form-item label="皮重(公斤)" span="1" prop="tare">
 							<!-- <ws-input v-model="weighingList.tare" placeholder="未获取到皮重" maxlength="100" size="small"
 								disabled /> -->
 								<ws-input @input="calculation" v-model="weighingList.tare" placeholder="未获取到皮重" maxlength="100" size="small" />
 						</ws-form-item>
-						<el-button type="primary" v-if="this.types == 2 || this.types == null">重新获取</el-button>
+						<el-button type="primary" v-if="this.types == 2 || this.types == null" @click="openPort">重新获取</el-button>
 						<!-- <div class="formItem"> -->
 						<ws-form-item label="净重(公斤)" span="1" prop="netWeight" v-if="this.types == 1">
 							<ws-input v-model="weighingList.netWeight" placeholder="请输入净重" maxlength="100" size="small"
@@ -116,6 +116,8 @@
 					addressUrl: '',
 					buckleMiscellaneous: 0,
 					compId: sessionStorage.getItem('ws-pf_compId'),
+					grossWeight:'',
+					tare:''
 				},
 				disabled: false,
 				disabled1: false,
@@ -145,6 +147,8 @@
 				warehouseId: '', //仓库id
 				compId: sessionStorage.getItem('ws-pf_compId'),
 				size: 10,
+				reader:null,
+				param:9600
 			}
 		},
 		activated() {
@@ -171,10 +175,56 @@
 			this.getList(id)
 		},
 		methods: {
-			chongxin() {
-				if (this.types == 2 || this.types == 3) {
-					return
+			async closePort(){
+				console.log('closePort');
+				this.reader.cancel()
+			},
+			async openPort(){
+			console.log('openPort',navigator);
+			if ('serial' in navigator) {
+				// The Web Serial API is supported.
+				console.log('the Web Serial API is supported.');
+				console.log(this.param);
+				const port = await navigator.serial.requestPort();
+				await port.open({ baudRate: this.param }); // set baud rate
+				this.reader = port.readable.getReader();
+				// 监听来自串行设备的数据
+				while (true) {
+				const { value, done } = await this.reader.read();
+				// console.log("value",value);
+				if (done) {
+					// 允许稍后关闭串口。
+					this.reader.releaseLock();
+					break;
+				}
+				var result="";
+				//2。获取16进制字符串
+				// var receData = HexConvert.ByteToString(value);
+				// console.log("receData",receData);
+				var flag = false;
+				for(var i=0;i<value.length;i++){
+					var tmp = String.fromCharCode(value[i])
+					if(tmp == "+"){
+					flag = true
+					}
+					if(flag && result.length <6 && tmp != "+"){
+					result += tmp
+					}
+				}
+				if(this.tpyeNo != 2){
+					this.weighingList.grossWeight = parseInt(result)
 				}
+				else{
+					this.weighingList.tare = parseInt(result) 
+				}
+				setTimeout(1000)
+				// value 是一个 Uint8Array
+				}
+				await port.close();
+			}
+			else{
+				console.log('the Web Serial API is not supported.',navigator);
+			}
 			},
 			//附件
 			//上传附件