懒羊羊
2024-03-13 b64ed2c784bde6dea9d18149ca706ebe532c857d
提交 | 用户 | 时间
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">
b64ed2 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="工单编号">
b64ed2 35                <span>{{workpieceInformation.workOrderNo}}</span>
e57a89 36            </el-descriptions-item>
b64ed2 37            <el-descriptions-item label="产品序列号">
e57a89 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>
b64ed2 46 <!--           <el-descriptions-item label="节拍">-->
47 <!--             <span>{{workpieceInformation.beat}}</span>-->
48 <!--           </el-descriptions-item>-->
e57a89 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";
b64ed2 292 import {listProductionOrde} from "@/api/main/om/productionOrde/productionOrde";
293 import {addPassingStationCollection} from "@/api/main/da/passingStationCollection/passingStationCollection";
294 import {addBasicParameters, addParamCollection} from "@/api/main/da/paramCollection/paramCollection";
e57a89 295
296 export default {
297   name: "stationTerminal",
298   data() {
299     return {
300       serialPortContent: '',
301       // 查询参数
302       formulaChildParams: {
303         pageNum: 1,
304         pageSize: 10,
305         productCode: null,
306         processesCode: null,
307       },
308       // 配方配置子信息表格数据
309       formulaChildList: [],
310       ipAddress: '',
311       imgSrc: '',
312       headContent: {
313         processesCode: 'OP1010',
314         processesName: '贴标机-贴码',
c2944a 315         sfcCode: '10',
e57a89 316       },
317       workpieceInformation: {
b64ed2 318         workOrderNo: null,
e57a89 319         productCode: null,
320         productModel: null,
321         productName: null,
322         beat: null,
323       },
324       // 查询参数
325       queryParams: {
326         pageNum: 1,
327         pageSize: 10,
328         sfcCode: null,
329       },
330       // 查询参数
331       StationConfQueryParams: {
332         pageNum: 1,
333         pageSize: 10,
334         ipAddress: null,
335       },
336       cakeLamp: {
337         plcState: 1, //plc
338         scannerState: 1, //扫码枪
339         InPlace: 0, //工件到位
340         scanFinish: 0,
341         startWork: 0,
342         release: 0 //允许放行
343       },
344       content: '',
345       activeName: 'first',
c2944a 346       url: "ws://192.168.11.134:8080/websocket/message/",
e57a89 347       message: "",
348       text_content: "",
349       ws: null,
c2944a 350       inputValue: '9',
351
352       open1: false,
353       input: "",
354       keepReading: true,
355       form1: {
356         baudRate: "9600",
357         dataBits: "8",
358         stopBits: "1",
359         parity: "none",
360         flowControl: "none",
361         desc: "",
362         type: "1",
363         isShowHistory: false,
364       },
365       btnType: "primary",
366       btnText: "连接串口",
367       restaurants: [],
368       portsList: [],
369       isShowSendArea: false,
370       readType: 1,
371       title: "",
b64ed2 372
373       passingStationForm: {},
e57a89 374     }
375   },
376   beforeDestroy() {
377     this.exit();
378   },
379   created() {
380     this.initStation();
381     // this.getStationConfList();
382     // setTimeout(() => {
383     //   this.connectWebsocket();
384     // }, 3000);
385   },
386   mounted() {
c2944a 387     if ("serial" in navigator) {
388       this.myserialport = new MySerialPort();
389       this.getPorts();
390       navigator.serial.addEventListener("connect", (e) => {
391         this.$message.success("设备已连接");
392         this.getPorts();
393       });
394       navigator.serial.addEventListener("disconnect", (e) => {
395         this.$message.error("设备已断开");
396       });
397       this.restaurants = this.loadAll();
398     } else {
399       this.$message.error(
400         "当前为HTTP模式或者浏览器版本过低,不支持网页连接串口"
401       );
402     }
403   },
404   computed: {
405     isDisable() {
406       return this.btnType === "danger";
407     },
e57a89 408   },
409   methods: {
c2944a 410     serialLink() {
411       this.open1 = true
412     },
e57a89 413     Release(){
93feb8 414       // this.$message('portsList!'+this.portsList.length);
415       if(this.btnType === "danger"){
416         this.$message('连接了!');
417
418       }else {
419         this.$message('mei连接了!');
420
e57a89 421       }
93feb8 422
423       // //判断是否可以释放
424       // let formulaChildParams = {
425       //   productCode: this.workpieceInformation.productCode,
426       //   processesCode: this.headContent.processesCode
427       // }
428       // releaseCheck(formulaChildParams).then(response => {
429       //   if(response.data === 0){
430       //     //改变状态
431       //     workpieceRelease(formulaChildParams).then(response => {
432       //     });
433       //     this.$message('放行成功!');
434       //     this.formulaChildList = []
435       //     this.getListFormulaChild()
436       //     // this.resetting()
437       //   }else {
438       //     this.$message('步骤未完成不许放行!');
439       //   }
440       // });
e57a89 441     },
c2944a 442     serialPortMethod(value){
e57a89 443       let formulaChildParams = {
b64ed2 444         materialCode: value,
445         workOrderNo: this.workpieceInformation.workOrderNo,
446         productCode: this.workpieceInformation.productCode,
447         locationCode: this.headContent.processesCode,
448         collectionTime: new Date()
e57a89 449       }
450       updateResults(formulaChildParams).then(response => {
451         this.getListFormulaChild()
452       });
453     },
454     rowStyle({ row }) {
455       if (row.results === 'OK') {
456         return 'background-color: PaleGreen';
457       } else if (row.results === 'NG') {
458         return 'background-color: LightSalmon';
459       }
460       return '';
461     },
462     /** 查询配方配置子信息列表 */
463     getListFormulaChild() {
464       this.formulaChildParams.productCode = this.workpieceInformation.productCode
465       this.formulaChildParams.processesCode = this.headContent.processesCode
466       listFormulaChild(this.formulaChildParams).then(response => {
467         this.formulaChildList = response.rows;
468       });
469     },
470     changeMenu(tab, event) {
471       console.log(tab, event);
472     },
473     clearClick(){
474       this.$message('这是一条清除消息提示');
475       this.headContent.sfcCode = null
476     },
477     scanCompleted(){
478       this.$message('扫码完成'+this.content);
479       this.headContent.sfcCode = this.content
480       this.queryParams.sfcCode = this.content
481       this.getList()
482       this.getListFormulaChild()
483       this.cakeLamp.scanFinish = 1
484       this.cakeLamp.startWork = 1
485
486     },
b64ed2 487     /** 查询工单列表 */
e57a89 488     async getList() {
b64ed2 489       await listProductionOrde(this.queryParams).then(response => {
e57a89 490         let rowsData = response.rows[0];
491         console.log(rowsData)
b64ed2 492         this.workpieceInformation.workOrderNo = rowsData.workOrderNo;
e57a89 493         this.workpieceInformation.productCode = rowsData.productCode;
494         this.workpieceInformation.productModel = rowsData.productModel;
495         this.workpieceInformation.productName = rowsData.productName;
496         this.timer = setInterval(() => {
497           this.workpieceInformation.beat++;
498         }, 1000);
499       });
500       this.getListFormulaChild()
b64ed2 501       this.addOverStationCollection()
502     },
503
504     /** 入站增加过站采集记录 **/
505     addOverStationCollection(){
506       this.passingStationForm = {
507         id: null,
508         workOrderNo: this.workpieceInformation.workOrderNo,
509         productCode: this.workpieceInformation.productCode,
510         locationCode: this.headContent.processesCode,
511         model: this.workpieceInformation.productModel,
512         productBarcode: this.headContent.sfcCode,
513         inboundTime: new Date()
514       }
515       addPassingStationCollection(this.passingStationForm).then(response => {
516       });
517       addBasicParameters(this.passingStationForm).then(response => {
518       });
e57a89 519     },
520     allowRelease(){
521       clearInterval(this.timer);
522       this.$message('保存节拍为'+this.workpieceInformation.beat+'秒');
523     },
524     /** 查询工位终端配置列表 */
525     // async getStationConfList() {
526     //   await getIpv4().then(response => {
527     //     this.StationConfQueryParams.ipAddress = response.msg
528     //     console.log('查询到本工位IP为'+this.StationConfQueryParams.ipAddress)
529     //   });
530     //   listStationConf(this.StationConfQueryParams).then(response => {
531     //     let rows = response.rows[0]
532     //     if(response.rows.length===0){
533     //       this.$message('该工位没有配置IP,请联系管理员配置IP');
534     //       return
535     //     }
536     //     this.headContent.processesName = rows.processesName
537     //     this.headContent.processesCode = rows.processesCode
538     //     console.log('设置工位编码'+this.headContent.processesCode)
539     //
540     //   });
541     // },
542     // connectWebsocket() {
543     //   console.log('websocket连接工位为'+this.headContent.processesCode)
544     //   const wsuri = this.url + this.headContent.processesCode;
545     //   this.ws = new WebSocket(wsuri);
546     //   const self = this;
547     //   this.ws.onopen = function (event) {
548     //     // self.text_content = self.text_content + "已经打开连接!" + "\n";
549     //     this.$message('websocket连接成功!');
550     //   };
551     //   this.ws.onmessage = function (event) {
552     //     self.text_content = event.data + "\n";
553     //     console.log(event.data)
554     //     if(event.data === "IN"){
555     //       self.cakeLamp.InPlace = "1"
556     //     }else if(event.data === "OUT"){
557     //       self.cakeLamp.outRsSign = "1"
558     //     }
559     //   };
560     // },
561     exit() {
562       if (this.ws) {
563         this.ws.close();
564         this.ws = null;
565       }
566     },
567     send() {
568       if (this.ws) {
569         this.ws.send(this.message);
570       } else {
571         alert("未连接到服务器");
572       }
573     },
574     async initStation(){
575       await getIpv4().then(response => {
576         this.StationConfQueryParams.ipAddress = response.msg
577         console.log('查询到本工位IP为'+this.StationConfQueryParams.ipAddress)
578       });
579       await listStationConf(this.StationConfQueryParams).then(response => {
580         let rows = response.rows[0]
581         if(response.rows.length===0){
582           this.$message('该工位没有配置IP,请联系管理员配置IP');
583           return
584         }
585         this.headContent.processesName = rows.processesName
586         this.headContent.processesCode = rows.processesCode
587         console.log('设置工位编码'+this.headContent.processesCode)
588
589       });
590       console.log('websocket连接工位为'+this.headContent.processesCode)
591       const wsuri = this.url + this.headContent.processesCode;
592       this.ws = new WebSocket(wsuri);
593       const self = this;
594       this.ws.onopen = function (event) {
595         this.$message('websocket连接成功!');
596       };
597       this.ws.onmessage = function (event) {
c2944a 598         const DELAY_TIME = 2000;
599         switch (event.data) {
600           case "IN":
601             self.cakeLamp.InPlace = 1;
602             break;
603           case "IN0":
604             self.cakeLamp.InPlace = 0;
605             break;
606           case "OUT":
607             self.cakeLamp.release = 1;
608             setTimeout(() => {
609               self.cakeLamp.InPlace = 0;
610               self.cakeLamp.release = 0;
611               self.cakeLamp.startWork = 0;
612               self.cakeLamp.scanFinish = 0;
613               self.Release();
614             }, DELAY_TIME);
615             break;
616           case "TIGHTEN":
617             this.formulaChildList = [];
618             self.getListFormulaChild();
619             break;
620           default:
621             break;
e57a89 622         }
623       };
624     },
c2944a 625
626
627     //接受数据的回调
628     callBack(value) {
629       if (this.form1.isShowHistory) this.form1.desc = this.readLi().join("");
630       else {
631         const scanValue = this.myserialport.hex2atostr(value).replace(/[\r\n]/g, '');
632         // if (value.length > 0)
633           // this.form1.desc = this.myserialport.hex2atostr(value);
634         this.$message('扫码完成'+this.content);
635         if (/T/.test(scanValue)) {
636           console.log('字符串包含字母"T"');
637           this.serialPortMethod(scanValue)
638         } else {
639           console.log('字符串不包含字母"T"');
640           this.headContent.sfcCode = scanValue;
641           this.queryParams.sfcCode = scanValue;
642           this.getList()
643           // this.getListFormulaChild()
644           this.cakeLamp.scanFinish = 1
645           this.cakeLamp.startWork = 1
646         }
e57a89 647       }
c2944a 648     },
649     clearHistory() {
650       this.form1.desc = "";
651       this.myserialport.state.readValue = [];
652     },
653     loadHistory() {
654       if (this.form1.isShowHistory) this.form1.desc = this.readLi().join("");
655       else {
656         let temp = this.readLi();
657         if (temp.length > 0) this.form1.desc = temp[temp.length - 1].join("");
658       }
659     },
660     readLi() {
661       let readType = this.readType;
662       return this.myserialport.state.readValue.map((items, index) => {
663         const item = items.value;
664         const type = items.type; // 1接收,2发送
665         let body = [];
666         if (item !== undefined) {
667           let strArr = [];
668           for (let hex of Array.from(item)) {
669             strArr.push(hex.toString(16).toLocaleUpperCase());
670           }
671           if (strArr.includes("D") && strArr.includes("A")) {
672             if (strArr.indexOf("A") - strArr.indexOf("D") === 1) {
673               strArr.splice(strArr.indexOf("D"), 1);
674               strArr.splice(strArr.indexOf("A"), 1, <br key={0} />);
675             }
676           }
677           strArr = strArr.map((item) => {
678             if (typeof item === "string") {
679               if (readType === 1) {
680                 return this.myserialport.hex2a(parseInt(item, 16));
681               } else if (readType === 2) {
682                 return item + " ";
683               }
684             }
685             return item;
686           });
687           if (typeof strArr[strArr.length - 1] === "string") {
688             strArr.push("\r\n");
689           }
690           body.push(strArr.join(""));
691         }
692         return body;
693       });
694     },
695     //连接
696     async connectBtn() {
697       if (this.btnType === "primary") {
698         try {
699           this.myserialport.state.baudRate = this.form1.baudRate;
700           this.myserialport.state.dataBits = this.form1.dataBits;
701           this.myserialport.state.stopBits = this.form1.stopBits;
702           this.myserialport.state.parity = this.form1.parity;
703           this.myserialport.state.flowControl = this.form1.flowControl;
704           await this.myserialport.openPort(this.form1.port, true, this.callBack);
705         } catch (error) {
706           this.$message.error("串口连接失败!请检查串口是否已被占用");
707         }
708         if (this.myserialport.state.isOpen) {
709           this.$message.success("串口连接成功");
710           this.open1 = false
711           this.btnType = "danger";
712           this.btnText = "关闭串口";
713         }
714       } else {
715         this.myserialport.openPort(this.form1.port, false, this.callBack);
716         this.$message.success("串口关闭成功");
717         this.btnType = "primary";
718         this.btnText = "连接串口";
719       }
720     },
721     //授权
722     async obtainAuthorization() {
723       if ("serial" in navigator) {
724         console.log("The Web Serial API is supported.");
725         if (!this.myserialport) this.myserialport = new MySerialPort();
726         try {
727           await this.myserialport.handleRequestPort();
728           this.$message.success("串口授权成功");
729           this.getPortInfo(this.myserialport.state.ports);
730         } catch (error) {
731           this.$message.warning("未选择新串口授权!");
732         }
733       } else {
734         this.$message.error(
735           "当前为HTTP模式或者浏览器版本过低,不支持网页连接串口"
736         );
737       }
738     },
739     //串口列表初始化
740     getPortInfo(portList) {
741       this.portsList = [];
742       portList.map((port, index) => {
743         const { usbProductId, usbVendorId } = port.getInfo();
744         if (usbProductId === undefined || usbVendorId === undefined) {
745           this.portsList.push({ label: "未知设备" + index, value: index });
746         } else {
747           const usbVendor = USBDevice.filter(
748             (item) => parseInt(item.vendor, 16) === usbVendorId
749           );
750           let usbProduct = [];
751           if (usbVendor.length === 1) {
752             usbProduct = usbVendor[0].devices.filter(
753               (item) => parseInt(item.devid, 16) === usbProductId
754             );
755           }
756           this.portsList.push({ label: usbProduct[0].devname, value: index });
757         }
758       });
759     },
760     // 发送
761     async sendCommon() {
762       if (this.myserialport.state.isOpen) {
763         if (this.form1.sendMsg.length !== 0) {
764           const writeType = this.form1.type;
765           let value = this.form1.sendMsg;
766           let arr = [];
767           if (writeType === 1) {
768             // ASCII
769             for (let i = 0; i < value.length; i++) {
770               arr.push(this.myserialport.a2hex(value[i]));
771             }
772           } else if (writeType === 2) {
773             // HEX
774             if (/^[0-9A-Fa-f]+$/.test(value) && value.length % 2 === 0) {
775               for (let i = 0; i < value.length; i = i + 2) {
776                 arr.push(parseInt(value.substring(i, i + 2), 16));
777               }
778             } else {
779               this.$message.error("格式错误");
780               return;
781             }
782           }
783           this.myserialport.writeText(arr);
784         } else {
785           this.$message.warning("请输入发送的信息");
786         }
787       } else {
788         this.$message.warning("串口处于关闭状态,请连接串口");
789       }
790     },
791     async getPorts() {
792       await this.myserialport.getPorts();
793       this.getPortInfo(this.myserialport.state.ports);
794     },
795     querySearch(queryString, cb) {
796       var restaurants = this.restaurants;
797       var results = queryString
798         ? restaurants.filter(this.createFilter(queryString))
799         : restaurants;
800       // 调用 callback 返回建议列表的数据
801       cb(results);
802     },
803     createFilter(queryString) {
804       return (restaurant) => {
805         return (
806           restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
807           0
808         );
809       };
810     },
811     loadAll() {
812       return [
813         { value: "110" },
814         { value: "300" },
815         { value: "600" },
816         { value: "1200" },
817         { value: "2400" },
818         { value: "4800" },
819         { value: "7200" },
820         { value: "9600" },
821         { value: "14400" },
822         { value: "19200" },
823         { value: "28800" },
824         { value: "38400" },
825         { value: "56000" },
826         { value: "57600" },
827         { value: "76800" },
828         { value: "115200" },
829         { value: "230400" },
830         { value: "460800" },
831       ];
832     },
e57a89 833   }
834 }
835
836 </script>
837 <style scoped>
838 .bottom-card{
839   height: 600px;
840 }
841 .circle-button{
842   height: 30px;
843   width: 30px;
844 }
845 .circle-red {
846   background-color: #e01a4f;
847 }
848 .circle-green {
849   background-color: green;
850 }
851 .circle-green-animate {
852   background-color: green;
853   animation: circle-green-animate 2s infinite;
854 }
855   @keyframes circle-green-animate {
856     50% {
857       opacity: 0.6;
858     }
859     0% {
860       opacity: 0.2;
861     }
862   }
863 .head-font{
c2944a 864   /*font-weight: bold;*/
865   /*font-size: 25px;*/
e57a89 866 }
867 span{
868   font-size: 15px;
869 }
870 .el-table .warning-row {
871   background: oldlace;
872 }
873
874 .el-table .success-row {
875   background: #f0f9eb;
876 }
877 </style>
878