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