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