15815213711
2025-04-24 16c179b122eb8c69d31b0fab66c5e29b9c332b8d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<script setup>
  import {ipc} from '@/utils/ipcRenderer'
  import {onBeforeUnmount, ref} from "vue";
  import {useStore} from "@/stores/icrfStore";
  const store = useStore();
  // 连接模式
  const mode = ref(store.mode);
  // 设备编号
  const deviceId = ref(store.deviceId);
  // 串口地址
  const com = ref(store.com);
  // 波特率
  const serialBaud = ref(store.serialBaud);
  const serialBaudList = ref([
      9600,19200,38400,57600,115200
  ])
  const data = ref( [
  ]);
  //com列表
  const comList = ref([]);
  const isLJ = ref(store.isLJ)
  const icdev = ref();
 
  const initListPort = () =>{
    ipc.invoke('controller.port.initPort',1).then(res=>{
      console.log(res)
      comList.value = res;
 
      if (comList.value.length > 0){
        com.value = 0;
      }
    })
  }
  /**
   * 连接设备
   */
  const setLocalICRF = () => {
    if(mode.value === 1){
      ipc.invoke('controller.icrf.connectUsb',{number:deviceId.value});
    }else{
      let port = comList.value[com.value];
      let portNum = port.path.slice(3);
      ipc.invoke('controller.icrf.connectSerialPort',{port:(Number(portNum) -1),baud:serialBaud.value});
    }
 
  }
  /**
   * 断开设备
   */
  const disconnectLocalICRF =()=>{
    ipc.invoke('controller.icrf.connectExit').then(res=>{
      if (res.code === 0){
        isLJ.value = false;
        store.$reset();
      }
      data.value.unshift(res.msg);
    });
 
  }
 
  const resetLocal = ()=>{
    ipc.invoke('controller.icrf.connectExit').then(res=>{
      isLJ.value = false;
      store.$reset();
      data.value.unshift("重置连接成功");
    });
 
  }
 
  ipc.on('controller.icrf.communication',(event,ret)=>{
      if (Number(ret.handle) > 0){
        icdev.value = Number(ret.handle)
        isLJ.value = true;
        store.$patch({
          mode: mode.value,
          deviceId: deviceId.value,
          com: com.value,
          serialBaud: serialBaud.value,
          icdev: icdev.value,
          isLJ: true,
        })
      }
     data.value.unshift(ret.msg);
  })
  onBeforeUnmount(()=>{
    ipc.removeAllListeners('controller.icrf.communication')
  })
 
 
  initListPort();
</script>
 
<template>
  <div class="app-container">
    <a-card >
      <div class="card-box">
        <div class="flex-1 " style="margin-right: 10px">
          <Section title="连接模式">
            <a-radio-group v-model:value="mode" :disabled="isLJ">
              <a-radio :value="1">USB</a-radio>
              <a-radio :value="2">Serial Port</a-radio>
            </a-radio-group>
          </Section>
 
          <Section title="设备编号">
            <a-input :disabled="mode !== 1 || isLJ"  v-model:value="deviceId" placeholder="请输入" />
          </Section>
 
          <Section title="串口号">
            <a-select :disabled="mode !== 2 || isLJ"
                ref="select"
                v-model:value="com"
                style="width: 100%;"
            >
              <a-select-option :value="index" v-for="(item,index) in comList">{{ item.path }}</a-select-option>
            </a-select>
          </Section>
 
          <Section title="波特率">
            <a-select :disabled="mode !== 2 || isLJ"
                ref="select"
                v-model:value="serialBaud"
                style="width: 100%;"
            >
              <a-select-option :value="item" v-for="(item,index) in serialBaudList" :key="index">{{ item }}</a-select-option>
            </a-select>
          </Section>
          <div style="margin-top: 20px;text-align: right;">
            <a-button style="margin-right: 20px;width: 150px" :disabled="isLJ" @click="setLocalICRF">连接设备</a-button>
            <a-button style="width: 150px" :disabled="!isLJ" @click="disconnectLocalICRF">断开连接</a-button>
            <a-button style="margin-left: 20px;width: 150px;margin-top: 10px" @click="resetLocal">重置</a-button>
          </div>
        </div>
        <div class="flex-1 box-right">
          <div class="box-right-body">
            <a-list bordered :data-source="data" :locale="{emptyText: ' '}" class="aList">
              <template #renderItem="{ item }">
                <a-list-item>{{ item }}</a-list-item>
              </template>
            </a-list>
          </div>
        </div>
      </div>
 
 
    </a-card>
  </div>
</template>
 
<style scoped lang="less">
.card-box{
  display: flex;
  justify-content: space-between;
}
.flex-1{
  flex:1
}
.box-right{
  border-left: 1px dashed #e9e9e9;
 
  .box-right-body{
    margin-left: 10px;
    border-radius: 10px;
    background: #fafafa;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 10px;
    font-size: 14px;
    .aList{
      overflow: auto;
      height: 330px;
      max-height: 100%;
      min-height: 100%;
    }
  }
}
</style>