admin
2024-08-08 3dca2fe9a9c1022d73c4ad60e5a8b0a9f7708844
提交 | 用户 | 时间
b78728 1 <template>
A 2   <div class="app-container">
3     <el-row :gutter="5">
4       <el-col :span="7">
5         <el-card shadow="never">
6           <span class="head-font">工位编号 : {{headContent.processesCode}}</span>
7         </el-card>
8       </el-col>
9       <el-col :span="7">
10         <el-card shadow="never">
11           <span class="head-font">工位名称 : {{headContent.processesName}}</span>
12         </el-card>
13       </el-col>
14       <el-col :span="10">
15         <el-card shadow="never">
16           <span class="head-font">产品序列号 : {{headContent.sfcCode}}</span>
359f3b 17           <el-button @click="print1">打印</el-button>
A 18
b78728 19           <el-button type="primary" style="float: right" icon="el-icon-search" size="mini" @click="serialLink">串口连接</el-button>
A 20           <el-button @click="clearClick" style="float: right" type="danger" size="mini">清除</el-button>
21         </el-card>
22       </el-col>
23
24     </el-row>
25     <el-row :gutter="5" style="margin-top: 5px">
26       <el-col :span="12">
27         <el-tabs type="border-card"  style="height: 600px" v-model="activeName" @tab-click="changeMenu">
28           <el-tab-pane name="first">
29             <span slot="label"> <a class="el-icon-date"></a>首页</span>
30             <el-col :span="24">
31               <div>
359f3b 32                 <div id="printMe">
3dca2f 33                   <div style="font-size: 29px;text-align: center;margin-top: 64px">
ef4246 34                     <span>电驱系统总成</span>
A 35                   </div>
3dca2f 36                   <div style="display: flex;margin-top: 2px;margin-left: 37px;font-size: 15px">
359f3b 37                     <div class="div1">
3dca2f 38                       <div >
ef4246 39                         <span>电机型号:</span><span>TZ180XSDSZX01</span><br>
A 40                       </div>
3dca2f 41                       <div style="margin-top: 4px">
ef4246 42                         <span>额定功率:</span><span>30KW</span><br>
A 43                       </div>
3dca2f 44                       <div style="margin-top: 4px">
ef4246 45                         <span>额定扭矩:</span><span>70N.m</span><br>
A 46                       </div>
3dca2f 47                       <div style="margin-top: 4px">
ef4246 48                         <span>额定电压:</span><span>336VDS</span><br>
A 49                       </div>
3dca2f 50                       <div style="margin-top: 4px">
ef4246 51                         <span>额工作制:</span><span>S9</span><br>
A 52                       </div>
3dca2f 53                       <div style="margin-top: 4px">
ef4246 54                         <span>持续工作电流:</span><span>115A</span><br>
A 55                       </div>
3dca2f 56                       <div style="margin-top: 4px">
ef4246 57                         <span>相数:</span><span>3</span><br>
A 58                       </div>
3dca2f 59                       <div style="margin-top: 4px">
ef4246 60                         <span>零件号:</span><span>9900200461</span><br>
A 61                       </div>
359f3b 62                     </div>
A 63
3dca2f 64                     <div class="div2" style="font-size: 15px;margin-left: 17px">
A 65                       <div>
66                         <span>控制器型号:</span><span>KTZ34X23SZX07</span><br>
67                       </div>
68                       <div style="margin-top: 4px">
ef4246 69                         <span>峰值功率:</span><span>60KW</span><br>
A 70                       </div>
3dca2f 71                       <div style="margin-top: 4px">
ef4246 72                         <span>峰值转矩:</span><span>175N.m</span><br>
A 73                       </div>
3dca2f 74                       <div style="margin-top: 4px">
ef4246 75                         <span>最高转速:</span><span>12000rpm</span><br>
A 76                       </div>
3dca2f 77                       <div style="margin-top: 4px">
ef4246 78                         <span>防护等级:</span><span>IP68</span><br>
A 79                       </div>
3dca2f 80                       <div style="margin-top: 4px">
ef4246 81                         <span>峰值工作电流:</span><span>230A</span><br>
A 82                       </div>
3dca2f 83                       <div style="margin-top: 4px">
ef4246 84                         <span>绝缘等级:</span><span>H</span><br>
A 85                       </div>
3dca2f 86                       <div style="margin-top: 4px">
ef4246 87                         <span>出厂编号:</span><span>2407190000002</span><br>
A 88                       </div>
359f3b 89                     </div>
A 90                   </div>
3dca2f 91                   <div style="font-size: 29px;margin-top: 2px;margin-left: 80px">
A 92                     <span>湖州智芯动力发展有限公司</span>
93                   </div>
359f3b 94
A 95
3dca2f 96                   <div style="text-align: center;margin-top: 97px;font-size: 20px">
359f3b 97                     <span>TZ180XSDSZX01</span><br>
A 98                     <span>KTZ34X23SZX07</span><br>
99                     <span>993083</span><br>
100                     <span>9900166410</span><br>
101                     <span>2407190000002</span>
102                   </div>
103
3dca2f 104                   <div style="margin-top: 76px;display: flex;margin-left: 32px">
359f3b 105                     <div ref="canvasWrapper" id="canvasWrapper1" style="width: 30%;flex: 1"></div>
3dca2f 106                     <div style="width: 70%;flex: 1;margin-top: 18px;margin-right: 17px">
A 107                       <span style="margin-left: -80px;font-size: 18px;font-family: FangSong">
359f3b 108                         P9900166410#T{{text1}}
A 109                       </span><br>
3dca2f 110                       <span style="margin-left: -80px;font-size: 18px;font-family: FangSong">
ef4246 111                         #V993983#SSW002.005#HHW001.001#NMCU#
359f3b 112                       </span>
A 113 <!--                      <span style="font-size: 10px;word-break: break-all;display: flex;justify-content: center;width: 155px;margin-left: 9px">P9900166410#T{{text1}}#V993983#SSW002.005#HHW001.001#NBMS#</span>-->
114                     </div>
115                   </div>
116
3dca2f 117                   <div style="margin-top: 42px;display: flex;margin-left: 32px">
359f3b 118                     <div ref="canvasWrapper" id="canvasWrapper2" style="width: 30%;flex: 1"></div>
3dca2f 119                     <div style="width: 70%;flex: 1;margin-top: 18px;margin-right: 17px">
A 120                       <span style="margin-left: -80px;font-size: 18px;font-family: FangSong">
359f3b 121                         P9900166410#T{{text1}}
A 122                       </span><br>
3dca2f 123                       <span style="margin-left: -80px;font-size: 18px;font-family: FangSong">
ef4246 124                         #V993983#SSW002.005#HHW001.001#NMCU#
359f3b 125                       </span>
A 126                       <!--                      <span style="font-size: 10px;word-break: break-all;display: flex;justify-content: center;width: 155px;margin-left: 9px">P9900166410#T{{text1}}#V993983#SSW002.005#HHW001.001#NBMS#</span>-->
127                     </div>
128                   </div>
129
34f9c8 130                   <div style="text-align: center;margin-left: -130px;margin-top: 42px">
359f3b 131                     <span>9900200461</span><br>
A 132                     <span>MCU</span><br>
133                     <span>HW 001.001</span><br>
134                     <span>SW 001.001</span><br>
135                   </div>
136
b78728 137                 </div>
A 138                 <el-button style="display: none" @click="test1">生成二维码</el-button>
139                 <el-button style="display: none" @click="test2">生成二维码</el-button>
140
141               </div>
142             </el-col>
143           </el-tab-pane>
144         </el-tabs>
145       </el-col>
146       <el-col :span="12">
147         <el-input v-model="weightValue" placeholder="请输入称重数据"></el-input>
148         <el-button @click="enterWeighing">录入</el-button>
149       </el-col>
150     </el-row>
151     <el-dialog v-dialogpop-up :title="title" :visible.sync="open1" width="1000" append-to-body>
152       <el-row
153         type="flex"
154         class="row-bg"
155         justify="center"
156         v-show="portsList.length === 0"
157       >
158         <el-col :span="7">
159           <div style="margin-top: 400px">
160           <span style="display: block">
161             仅支持Chrome 89+或者Edge 89+浏览器(安全上下文(HTTPS)中可用)
162           </span>
163             <el-button type="primary" @click="obtainAuthorization">授权</el-button>
164           </div>
165         </el-col>
166       </el-row>
167       <el-form
168         v-show="portsList.length > 0"
169         ref="form1"
170         :model="form1"
171         label-width="100px">
172         <el-row>
173           <el-col :span="24"
174           ><div>
175             <el-form-item label="串口">
176               <el-select
177                 v-model="form1.port"
178                 filterable
179                 placeholder="请选择串口"
180                 :disabled="isDisable"
181               >
182                 <el-option
183                   v-for="item in portsList"
184                   :key="item.value"
185                   :label="item.label"
186                   :value="item.value"
187                 >
188                 </el-option>
189               </el-select>
190             </el-form-item>
191             <el-form-item label="波特率">
192               <el-autocomplete
193                 popper-class="my-autocomplete"
194                 v-model="form1.baudRate"
195                 :fetch-suggestions="querySearch"
196                 placeholder="请输入波特率"
197                 :disabled="isDisable"
198               >
199                 <i class="el-icon-edit el-input__icon" slot="suffix"> </i>
200                 <template slot-scope="{ item }">
201                   <div class="name">{{ item.value }}</div>
202                   <span class="addr">{{ item.address }}</span>
203                 </template>
204               </el-autocomplete>
205             </el-form-item>
206             <el-form-item label="数据位">
207               <el-select
208                 v-model="form1.dataBits"
209                 placeholder="请选择数据位"
210                 :disabled="isDisable"
211               >
212                 <el-option label="7" value="7"></el-option>
213                 <el-option label="8" value="8"></el-option>
214               </el-select>
215             </el-form-item>
216             <el-form-item label="停止位">
217               <el-select
218                 v-model="form1.stopBits"
219                 placeholder="请选择停止位"
220                 :disabled="isDisable"
221               >
222                 <el-option label="1" value="1"></el-option>
223                 <el-option label="2" value="2"></el-option>
224               </el-select>
225             </el-form-item>
226
227             <el-form-item label="校验位">
228               <el-select
229                 v-model="form1.parity"
230                 placeholder="请选择校验位"
231                 :disabled="isDisable"
232               >
233                 <el-option label="None" value="none"></el-option>
234                 <el-option label="Even" value="even"></el-option>
235                 <el-option label="Odd" value="odd"></el-option>
236               </el-select>
237             </el-form-item>
238
239             <el-form-item label="流控制">
240               <el-select
241                 v-model="form1.flowControl"
242                 placeholder="请选择流控制"
243                 :disabled="isDisable"
244               >
245                 <el-option label="None" value="none"></el-option>
246                 <el-option label="HardWare" value="hardware"></el-option>
247               </el-select>
248             </el-form-item>
249             <el-form-item label="显示历史">
250               <el-switch
251                 v-model="form1.isShowHistory"
252                 @change="loadHistory"
253               ></el-switch>
254               <el-button
255                 type="danger"
256                 icon="el-icon-delete"
257                 circle
258                 title="清空历史"
259                 @click="clearHistory"
260               ></el-button>
261             </el-form-item>
262             <el-form-item label="发送区设置" v-show="isShowSendArea">
263               <el-form-item label="发送格式">
264                 <el-radio-group v-model="form1.type">
265                   <el-radio label="1">ASCII</el-radio>
266                   <el-radio label="2">HEX</el-radio>
267                 </el-radio-group>
268               </el-form-item>
269               <el-form-item label="发送信息">
270                 <el-input type="textarea" v-model="form1.sendMsg"></el-input>
271               </el-form-item>
272               <el-button type="primary" @click="sendCommon">发送</el-button>
273             </el-form-item>
274
275             <el-form-item>
276               <el-button :type="btnType" @click="connectBtn">{{
277                   btnText
278                 }}</el-button>
279               <el-button type="info" @click="obtainAuthorization"
280               >新增授权</el-button
281               >
282             </el-form-item>
283           </div>
284           </el-col>
285         </el-row>
286       </el-form>
287     </el-dialog>
288   </div>
289 </template>
290 <script>
291 import {getIpv4, listStationConf} from "@/api/main/sc/stationConf";
292 import {
293   noPageListFormulaChild,
294   workpieceRelease,
295   yzUpdateResults,
296   yzUpdateTighteningFormula
297 } from "@/api/main/bs/formulaChild/formulaChild";
298 import MySerialPort from "@/utils/MySerialPort";
299 import USBDevice from "@/utils/usb.json";
300 import {addPassingStationCollection} from "@/api/main/da/passingStationCollection/passingStationCollection";
301 import {
302   addTighteningParameters, enterWeighing,
303   replaceAssemblyCode,
304   saveCampaignTimeParameters,
305   yzAddBasicParameters,
306 } from "@/api/main/da/paramCollection/paramCollection";
307 import QRCode from "qrcodejs2";
308
309 export default {
310   name: "stationTerminal",
311   data() {
312     return {
313       weightValue: '',
314       showInput: true,
315       serialPortContent: '',
316       // 查询参数
317       formulaChildParams: {
318         pageNum: 1,
319         pageSize: 10,
320         productCode: null,
321         processesCode: null,
322       },
323       // 配方配置子信息表格数据
324       formulaChildList: [],
325       ipAddress: '',
326       imgSrc: '',
327       headContent: {
328         processesCode: 'OP520',
329         processesName: '成品下线',
330         sfcCode: '',
331         yzSfcCode: '',
332       },
333       workpieceInformation: {
334         workOrderNo: null,
335         productCode: null,
336         productModel: null,
337         productName: null,
338         beat: null,
339       },
340       // 查询参数
341       queryParams: {
342         pageNum: 1,
343         pageSize: 10,
344         sfcCode: null,
345       },
346       // 查询参数
347       StationConfQueryParams: {
348         pageNum: 1,
349         pageSize: 10,
350         ipAddress: null,
351       },
352       cakeLamp: {
353         plcState: 1, //plc
354         scannerState: 1, //扫码枪
355         InPlace: 0, //工件到位
356         scanFinish: 0,
357         startWork: 0,
358         release: 0 //允许放行
359       },
360       content: '',
361       activeName: 'first',
d5a701 362       url: "ws://192.168.20.250:8080/websocket/message/",
b78728 363       message: "",
A 364       text_content: "",
365       ws: null,
366       inputValue: '9',
367
368       open1: false,
369       input: "",
370       keepReading: true,
371       form1: {
372         baudRate: "115200",
373         dataBits: "8",
374         stopBits: "1",
375         parity: "none",
376         flowControl: "none",
377         desc: "",
378         type: "1",
379         isShowHistory: false,
380       },
381       btnType: "primary",
382       btnText: "连接串口",
383       restaurants: [],
384       portsList: [],
385       isShowSendArea: false,
386       readType: 1,
387       title: "",
388
389       passingStationForm: {},
390       originalArray: [],
391       text1: '',
392     }
393   },
394   beforeDestroy() {
395     this.exit();
396   },
397   created() {
398     this.initStation();
399     // this.getStationConfList();
400     // setTimeout(() => {
401     //   this.connectWebsocket();
402     // }, 3000);
403   },
404   mounted() {
405     if ("serial" in navigator) {
406       this.myserialport = new MySerialPort();
407       this.getPorts();
408       navigator.serial.addEventListener("connect", (e) => {
409         this.$message.success("设备已连接");
410         this.getPorts();
411       });
412       navigator.serial.addEventListener("disconnect", (e) => {
413         this.$message.error("设备已断开");
414       });
415       this.restaurants = this.loadAll();
416     } else {
417       this.$message.error(
418         "当前为HTTP模式或者浏览器版本过低,不支持网页连接串口"
419       );
420     }
421   },
422   computed: {
423     isDisable() {
424       return this.btnType === "danger";
425     },
426   },
427   methods: {
428     enterWeighing(){
429       if(this.headContent.sfcCode ==='' || this.weightValue === '' ){
430         this.$message.error('参数不全!');
431       }else {
432         let param = {
433           sfcCode: this.headContent.sfcCode,
434           weightValue: this.weightValue,
435         }
436         enterWeighing(param).then(response => {
437           this.$message('录入完成!');
438         });
439       }
440     },
441     serialLink() {
442       this.open1 = true
443     },
444     bindYzSfc(){
445       if(this.headContent.sfcCode!==""&&this.headContent.yzSfcCode!==""){
446         let param = {
447           sfcCode: this.headContent.sfcCode,
448           locationCode: this.headContent.processesCode,
449           yzSfcCode: this.headContent.yzSfcCode
450         }
451         this.$message('可以绑定!');
452         replaceAssemblyCode(param).then(response => {
453
454         });
455       }
456     },
457
458     Release(){
459       // this.$message('portsList!'+this.portsList.length);
460       if(this.btnType === "danger"){
461         this.$message('连接了!!');
462
463       }else {
464         this.$message('mei连接了!');
465
466       }
467
468     },
469     serialPortMethod(value){
470       let formulaChildParams = {
471         scanBarcode: value,
472         sfcBarcode: this.headContent.sfcCode,
473         workOrderNo: this.workpieceInformation.workOrderNo,
474         productCode: "1P102S",
475         locationCode: this.headContent.processesCode,
476         collectionTime: new Date()
477       }
478       yzUpdateResults(formulaChildParams).then(response => {
479         this.getListFormulaChild()
480       });
481     },
482     rowStyle({ row }) {
483       if (row.results === 'OK') {
484         return 'background-color: PaleGreen';
485       } else if (row.results === 'NG') {
486         return 'background-color: LightSalmon';
487       }
488       return '';
489     },
490     /** 查询配方配置子信息列表 */
491     getListFormulaChild() {
492       this.formulaChildParams.productCode = "1P102S"
493       this.formulaChildParams.processesCode = this.headContent.processesCode
494       console.log(this.formulaChildParams)
495       noPageListFormulaChild(this.formulaChildParams).then(response => {
496         this.formulaChildList = response.rows;
497         if (this.formulaChildList.length >0){
498           let pos = 0
499           const tempArr = this.formulaChildList.filter(x=> 'OK' === x.results)
500           if (tempArr.length>0){
501             pos = tempArr.length
502             this.$nextTick(() => {
503               let temp33 = document.getElementsByClassName('el-table__row')
504               console.log('temp33',temp33)
505               console.log('len',temp33.item(pos))
506               if (temp33.length > 0){
507                 console.log('1111111111111111')
508                 let arr = temp33[pos-1]
509                 console.log('srr',arr)
510                 arr.scrollIntoView({ block: 'center' })
511               }
512             })
513           }
514
515         }
516       });
517     },
518     endClear(){
519
520     },
521     changeMenu(tab, event) {
522       console.log(tab, event);
523     },
524     clearClick(){
525       this.$message('这是一条清除消息提示');
526       this.headContent.sfcCode = null
527     },
528     scanCompleted(){
529       this.$message('扫码完成'+this.content);
530       this.headContent.sfcCode = this.content
531       this.queryParams.sfcCode = this.content
532       this.getList()
533       this.getListFormulaChild()
534       this.cakeLamp.scanFinish = 1
535       this.cakeLamp.startWork = 1
536
537     },
538     /** 查询工单列表 */
539     async getList() {
540       // this.getListFormulaChild()
541       // this.addOverStationCollection()
542       this.print1()
543     },
544     print1() {
545       // 新开页面打印
546       let newStr = document.getElementById('printMe').innerHTML;
547       let newWin = window.open('', '_blank');
548       newWin.document.body.innerHTML = newStr;
549       newWin.print();
550       return false;
551     },
552     test1(){
553       document.getElementById('canvasWrapper1').innerHTML = '';
554       this.$nextTick(() => {
555         let qrCode = new QRCode('canvasWrapper1',{
3dca2f 556           width: 83,
A 557           height: 83,
558           text: 'P9900166410#T'+ this.text1 +'#V993983#SSW002.005#HHW001.001#NMCU#',
b78728 559           colorDark: '#000',
A 560           colorLight: '#fff'
561         })
562       }
563       )
564     },
565     test2(){
566       document.getElementById('canvasWrapper2').innerHTML = '';
567       this.$nextTick(() => {
568           let qrCode = new QRCode('canvasWrapper2',{
3dca2f 569             width: 83,
A 570             height: 83,
571             text: 'P9900166410#T'+ this.text1 +'#V993983#SSW002.005#HHW001.001#NMCU#',
b78728 572             colorDark: '#000',
A 573             colorLight: '#fff'
574           })
575         }
576       )
577     },
578
579     /** 入站增加过站采集记录 **/
580     addOverStationCollection(){
581       this.passingStationForm = {
582         id: null,
583         // workOrderNo: this.workpieceInformation.workOrderNo,
584         productCode: "1P102S",
585         locationCode: this.headContent.processesCode,
586         // model: this.workpieceInformation.productModel,
587         productBarcode: this.headContent.sfcCode,
588         sfcCode: this.headContent.sfcCode,
589         inboundTime: new Date()
590       }
591       addPassingStationCollection(this.passingStationForm).then(response => {});
592       yzAddBasicParameters(this.passingStationForm).then(response => {});
593     },
594     exit() {
595       if (this.ws) {
596         this.ws.close();
597         this.ws = null;
598       }
599     },
600     send() {
601       if (this.ws) {
602         this.ws.send(this.message);
603       } else {
604         alert("未连接到服务器");
605       }
606     },
607
608     initStation: async function () {
609       await getIpv4().then(response => {
610         this.StationConfQueryParams.ipAddress = response.msg
611         console.log('查询到本工位IP为' + this.StationConfQueryParams.ipAddress)
612       });
613       await listStationConf(this.StationConfQueryParams).then(response => {
614         let rows = response.rows[0]
615         if (response.rows.length === 0) {
616           // this.$message('该工位没有配置IP,请联系管理员配置IP');
617           return
618         }
619         this.headContent.processesName = rows.processesName
620         this.headContent.processesCode = rows.processesCode
621         console.log('设置工位编码' + this.headContent.processesCode)
622
623       });
624       this.conCom()
625       console.log('websocket连接工位为' + this.headContent.processesCode)
626       const wsuri = this.url + this.headContent.processesCode;
627       this.ws = new WebSocket(wsuri);
628       const self = this;
629       this.ws.onopen = function (event) {
630         this.$message('websocket连接成功!');
631       };
632       this.ws.onmessage = function (event) {
633         if (event.data === "IN") {
634           self.cakeLamp.InPlace = 1;
635         } else if (event.data === "IN0") {
636           self.cakeLamp.InPlace = 0;
637         } else if (event.data === "OUT") {
638           console.log("out")
639
640           const param = {
641             workOrderNo: self.workpieceInformation.workOrderNo,
642             productCode: "1P102S",
643             locationCode: self.headContent.processesCode,
644             productBarcode: self.headContent.sfcCode,
645           }
646           // saveCampaignTimeParameters(param).then(response => {});
647           // self.cakeLamp.release = 1;
648           workpieceRelease(param).then(response => {});
649           self.formulaChildList = [];
650           self.headContent.sfcCode = '';
651           console.log(self.formulaChildList)
652         } else if (event.data === "END") {
653           // const param = {
654           //   workOrderNo: self.workpieceInformation.workOrderNo,
655           //   productCode: self.workpieceInformation.productCode,
656           //   locationCode: self.headContent.processesCode,
657           //   productBarcode: self.headContent.sfcCode,
658           // }
659           // saveCampaignTimeParameters(param).then(response => {});
660           // self.endClear()
661           // workpieceRelease(param).then(response => {});
662         } else if (event.data.includes("[")) {
663           let formulaChilds = "";
664           self.formulaChildList.sort((a, b) => a.stepSort - b.stepSort);
665           self.formulaChildList
666             .filter((formulaChild) => formulaChild.operationType === '1');
667           for (let i = 0; i < self.formulaChildList.length; i++) {
668             let formulaChild = self.formulaChildList[i];
669             let results = formulaChild.results;
670             if (results === '' || results === null || results === 'NG') {
671               formulaChilds = formulaChild;
672               break;
673             }
674           }
675           const param = {
676             tightenTheArray: event.data,
677             paramCode: formulaChilds.paramCode,
678             // workOrderNo: self.workpieceInformation.workOrderNo,
679             productCode: "1P102S",
680             locationCode: self.headContent.processesCode,
681             productBarcode: self.headContent.sfcCode,
682           }
683           addTighteningParameters(param).then(response => {});
684           yzUpdateTighteningFormula(param).then(response => {
685             console.log(response.msg)
686             if(response.msg === "1"){
687               self.getListFormulaChild()
688
689             }else {
690               const param = {
691                 workOrderNo: self.workpieceInformation.workOrderNo,
692                 productCode: "1P102S",
693                 locationCode: self.headContent.processesCode,
694                 productBarcode: self.headContent.sfcCode,
695               }
696               saveCampaignTimeParameters(param).then(response => {});
697               workpieceRelease(param).then(response => {});
698               self.formulaChildList = [];
699               self.headContent.sfcCode = '';
700             }
701           });
702         }
703       };
704     },
705
706
707
708     //接受数据的回调
709     callBack(value) {
710       if (this.form1.isShowHistory) this.form1.desc = this.readLi().join("");
711       else {
712         const scanValue = this.myserialport.hex2atostr(value).replace(/[\r\n]/g, '');
713         console.log("串口收到数据-------------------"+scanValue)
714         this.test1()
715         this.test2()
716         this.text1 = scanValue
717         this.headContent.sfcCode = scanValue
718         // this.print1()
719         // setTimeout(() => {
720         //   this.print()
721         // },50)
722         // this.getList()
723
724       }
725     },
726     clearHistory() {
727       this.form1.desc = "";
728       this.myserialport.state.readValue = [];
729     },
730     loadHistory() {
731       if (this.form1.isShowHistory) this.form1.desc = this.readLi().join("");
732       else {
733         let temp = this.readLi();
734         if (temp.length > 0) this.form1.desc = temp[temp.length - 1].join("");
735       }
736     },
737     readLi() {
738       let readType = this.readType;
739       return this.myserialport.state.readValue.map((items, index) => {
740         const item = items.value;
741         const type = items.type; // 1接收,2发送
742         let body = [];
743         if (item !== undefined) {
744           let strArr = [];
745           for (let hex of Array.from(item)) {
746             strArr.push(hex.toString(16).toLocaleUpperCase());
747           }
748           if (strArr.includes("D") && strArr.includes("A")) {
749             if (strArr.indexOf("A") - strArr.indexOf("D") === 1) {
750               strArr.splice(strArr.indexOf("D"), 1);
751               strArr.splice(strArr.indexOf("A"), 1, <br key={0} />);
752             }
753           }
754           strArr = strArr.map((item) => {
755             if (typeof item === "string") {
756               if (readType === 1) {
757                 return this.myserialport.hex2a(parseInt(item, 16));
758               } else if (readType === 2) {
759                 return item + " ";
760               }
761             }
762             return item;
763           });
764           if (typeof strArr[strArr.length - 1] === "string") {
765             strArr.push("\r\n");
766           }
767           body.push(strArr.join(""));
768         }
769         return body;
770       });
771     },
772     conCom(){
773       try {
774         this.myserialport.state.baudRate = this.form1.baudRate;
775         this.myserialport.state.dataBits = this.form1.dataBits;
776         this.myserialport.state.stopBits = this.form1.stopBits;
777         this.myserialport.state.parity = this.form1.parity;
778         this.myserialport.state.flowControl = this.form1.flowControl;
779         this.myserialport.openPort(0, true, this.callBack);
780         console.log(this.form1.port)
781       } catch (error) {
782         this.$message.error("串口连接失败!请检查串口是否已被占用");
783       }
784       if (this.myserialport.state.isOpen) {
785         this.$message.success("串口连接成功");
786       }
787     },
788     //连接
789     async connectBtn() {
790       if (this.btnType === "primary") {
791         try {
792           this.myserialport.state.baudRate = this.form1.baudRate;
793           this.myserialport.state.dataBits = this.form1.dataBits;
794           this.myserialport.state.stopBits = this.form1.stopBits;
795           this.myserialport.state.parity = this.form1.parity;
796           this.myserialport.state.flowControl = this.form1.flowControl;
797           await this.myserialport.openPort(this.form1.port, true, this.callBack);
798           console.log(this.form1.port)
799         } catch (error) {
800           this.$message.error("串口连接失败!请检查串口是否已被占用");
801         }
802         if (this.myserialport.state.isOpen) {
803           this.$message.success("串口连接成功");
804           this.open1 = false
805           this.btnType = "danger";
806           this.btnText = "关闭串口";
807         }
808       } else {
809         this.myserialport.openPort(this.form1.port, false, this.callBack);
810         this.$message.success("串口关闭成功");
811         this.btnType = "primary";
812         this.btnText = "连接串口";
813       }
814     },
815     //授权
816     async obtainAuthorization() {
817       if ("serial" in navigator) {
818         console.log("The Web Serial API is supported.");
819         if (!this.myserialport) this.myserialport = new MySerialPort();
820         try {
821           await this.myserialport.handleRequestPort();
822           this.$message.success("串口授权成功");
823           this.getPortInfo(this.myserialport.state.ports);
824         } catch (error) {
825           this.$message.warning("未选择新串口授权!");
826         }
827       } else {
828         this.$message.error(
829           "当前为HTTP模式或者浏览器版本过低,不支持网页连接串口"
830         );
831       }
832     },
833     //串口列表初始化
834     getPortInfo(portList) {
835       this.portsList = [];
836       portList.map((port, index) => {
837         const { usbProductId, usbVendorId } = port.getInfo();
838         if (usbProductId === undefined || usbVendorId === undefined) {
839           this.portsList.push({ label: "未知设备" + index, value: index });
840         } else {
841           const usbVendor = USBDevice.filter(
842             (item) => parseInt(item.vendor, 16) === usbVendorId
843           );
844           let usbProduct = [];
845           if (usbVendor.length === 1) {
846             usbProduct = usbVendor[0].devices.filter(
847               (item) => parseInt(item.devid, 16) === usbProductId
848             );
849           }
850           this.portsList.push({ label: usbProduct[0].devname, value: index });
851         }
852       });
853     },
854     // 发送
855     async sendCommon() {
856       if (this.myserialport.state.isOpen) {
857         if (this.form1.sendMsg.length !== 0) {
858           const writeType = this.form1.type;
859           let value = this.form1.sendMsg;
860           let arr = [];
861           if (writeType === 1) {
862             // ASCII
863             for (let i = 0; i < value.length; i++) {
864               arr.push(this.myserialport.a2hex(value[i]));
865             }
866           } else if (writeType === 2) {
867             // HEX
868             if (/^[0-9A-Fa-f]+$/.test(value) && value.length % 2 === 0) {
869               for (let i = 0; i < value.length; i = i + 2) {
870                 arr.push(parseInt(value.substring(i, i + 2), 16));
871               }
872             } else {
873               this.$message.error("格式错误");
874               return;
875             }
876           }
877           this.myserialport.writeText(arr);
878         } else {
879           this.$message.warning("请输入发送的信息");
880         }
881       } else {
882         this.$message.warning("串口处于关闭状态,请连接串口");
883       }
884     },
885     async getPorts() {
886       await this.myserialport.getPorts();
887       this.getPortInfo(this.myserialport.state.ports);
888     },
889     querySearch(queryString, cb) {
890       var restaurants = this.restaurants;
891       var results = queryString
892         ? restaurants.filter(this.createFilter(queryString))
893         : restaurants;
894       // 调用 callback 返回建议列表的数据
895       cb(results);
896     },
897     createFilter(queryString) {
898       return (restaurant) => {
899         return (
900           restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
901           0
902         );
903       };
904     },
905     loadAll() {
906       return [
907         { value: "110" },
908         { value: "300" },
909         { value: "600" },
910         { value: "1200" },
911         { value: "2400" },
912         { value: "4800" },
913         { value: "7200" },
914         { value: "9600" },
915         { value: "14400" },
916         { value: "19200" },
917         { value: "28800" },
918         { value: "38400" },
919         { value: "56000" },
920         { value: "57600" },
921         { value: "76800" },
922         { value: "115200" },
923         { value: "230400" },
924         { value: "460800" },
925       ];
926     },
927   }
928 }
929
930 </script>
931 <style scoped>
359f3b 932 .div1, .div2 {
A 933   flex: 1; /* 两个div平分容器宽度 */
934 }
3dca2f 935 .span{
A 936   font-family: 'Microsoft YaHei', sans-serif;
937 }
359f3b 938 .div2{
A 939   margin-left: 30px;
940 }
b78728 941 .bottom-card{
A 942   height: 600px;
943 }
944 .circle-button{
945   height: 30px;
946   width: 30px;
947 }
948 .circle-red {
949   background-color: #e01a4f;
950 }
951 .circle-green {
952   background-color: green;
953 }
954 .circle-green-animate {
955   background-color: green;
956   animation: circle-green-animate 2s infinite;
957 }
958 @keyframes circle-green-animate {
959   50% {
960     opacity: 0.6;
961   }
962   0% {
963     opacity: 0.2;
964   }
965 }
966 .head-font{
967   /*font-weight: bold;*/
968   /*font-size: 25px;*/
969 }
970 span{
971   font-size: 15px;
972 }
973 .el-table .warning-row {
974   background: oldlace;
975 }
976
977 .el-table .success-row {
978   background: #f0f9eb;
979 }
980 </style>
981