fei.wang
7 天以前 ae7b22322555448d95fd56f505bafa325c167a26
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<template>
  <view class="container">
    <view class="area">
      <view class="label">基本操作</view>
      <view class="btn-group">
        <button class="btn-item" @click="handleDevice" size="mini">获取设备</button>
        <button class="btn-item" @click="handleConnect" size="mini">打开设备</button>
        <button class="btn-item" @click="handleRead" size="mini">读取数据</button>
        <button class="btn-item" @click="handleDisconnect" size="mini">关闭设备</button>
      </view>
    </view> 
    <view class="cell">
      <view class="area">
        <view class="label">设备订阅</view>
        <view class="btn-group">
          <button class="btn-item" @click="handleSubscribeUsb" size="mini">订阅</button>
          <button class="btn-item" @click="handleUnsubscribeUsb" size="mini">取消订阅</button>
        </view>
      </view>
      <view class="area">
        <view class="label">消息订阅</view>
        <view class="btn-group">
          <button class="btn-item" @click="handleSubscribeMsg" size="mini">订阅</button>
          <button class="btn-item" @click="handleUnsubscribeMsg" size="mini">取消订阅</button>
        </view>
      </view>
    </view>
 
 
    <view class="table">
      <view class="th">
        <text class="td">设备名称</text>
        <text class="td">设备ID</text>
        <text class="td">vendorId(VID)</text>
        <text class="td">产品名称</text>
        <text class="td">productId(PID)</text>
      </view>
      <view>
        <view class="tr" v-for="(item,i) in list" :key="i">
          <text class="td">{{item.deviceName}}</text>
          <text class="td">{{item.deviceId}}</text>
          <text class="td">{{item.vendorId}}</text>
          <text class="td">{{item.productName}}</text>
          <text class="td">{{item.productId}}</text>
        </view>
      </view>
    </view>
    <view class="result">
      <view class="result-btn">
        <view class="title">输出:</view>
        <button class="btn-item" @click="handleClear" size="mini">清除</button>
      </view>
      <textarea class="output" v-model="output" disabled></textarea>
    </view>
  </view>
</template>
<script>
  // 引入插件
  import {
    UsbSerial
  } from '@/uni_modules/shmily-usb-serial';
  let usbSerial;
  export default {
    data() {
      return {
        list: [],
        output: ''
      };
    },
    mounted() {
      // 实例化
      usbSerial = new UsbSerial();
      this.handleDevice();
    },
    methods: {
      // 订阅消息,收到数据自动执行回调
      handleSubscribeMsg() {
        const { success, message } = usbSerial.subscribe({
          dataType: 'HEX',
          // dataType: 'ASCII',
          bufferSize: 64
        }, (data) => {
          console.log(data.trim());
        });
        if (success) {
          this.updateOutput('消息订阅成功');
        } else {
          this.updateOutput(message);
        }
      },
      // 取消消息订阅
      handleUnsubscribeMsg() {
        usbSerial.unsubscribe();
        this.updateOutput('取消消息订阅成功');
      },
      // 订阅设备,设备连接自动执行回调
      handleSubscribeUsb() {
        usbSerial.registerUsbAttach((action) => {
          this.updateOutput(action ? '设备插入' : '设备移除');
          this.handleDevice();
        });
        this.updateOutput('设备订阅成功');
      },
      // 取消设备订阅
      handleUnsubscribeUsb() {
        usbSerial.unregisterUsbAttach();
        this.updateOutput('取消设备订阅成功');
      },
      // 查询设备
      handleDevice() {
        this.list = usbSerial.getDeviceList();
        this.updateOutput('设备获取成功');
      },
      // 打开设备
      handleConnect() {
        const success = usbSerial.connect({
          vendorId: 6790, // 请填写实际值
          productId: 29987, // 请填写实际值
          baudRate: 115200, // 请填写实际值
          dataBits: 8, // 请填写实际值
          stopBits: 1, // 请填写实际值
          parity: 0, // 请填写实际值
        }, ({ success, message}) =>{
            console.log(message);
          this.updateOutput(message);
          if (success) {
            this.handleSubscribeMsg();
          }
        });
      },
      // 读取数据
      handleRead() {
        const { data, success, message } = usbSerial.read('ASCII');
        if (success) {
          this.updateOutput(`读取数据:${data}`);
        } else {
          this.updateOutput(message);
        }
      },
      // 关闭连接
      handleDisconnect() {
        usbSerial.disconnect(({success, message}) => {
          this.updateOutput(message);
        });
      },
      handleClear() {
        this.output = '';
      },
      updateOutput(msg) {
        this.output += `${msg}\n`;
      }
    }
  }
</script>
<style lang="scss" scoped>
  .container {
    padding: 20rpx;
    font-size: 14px;
  }
 
  .cell {
    display: flex;
    justify-content: space-between;
    column-gap: 15rpx;
 
    .area {
      flex: 1;
    }
  }
 
  .area {
    padding: 10rpx 10rpx 20rpx;
    border: 1px dashed #cccccc;
    margin-bottom: 20rpx;
 
    &+& {
      margin-left: 15rpx;
    }
 
    .label {
      width: fit-content;
      margin-top: -30rpx;
      background-color: white;
    }
  }
 
  .btn-group {
    width: fit-content;
    display: flex;
    margin-top: 15rpx;
  }
 
  .btn-item {
    width: fit-content;
    font-size: 12px;
 
    &+& {
      margin-left: 15rpx;
    }
  }
 
  .table {
    min-height: 200rpx;
    border: 1px solid #cccccc;
 
    .th {
      border-bottom: 1px solid #cccccc;
    }
 
    .tr+.tr {
      border-top: 1px dashed #cccccc;
    }
 
    .th,
    .tr {
      display: flex;
      align-items: center;
      font-size: 12px;
 
 
      .td {
        flex: 1;
        text-align: center;
        max-width: 25%;
        word-break: break-all;
        padding: 8px 4px;
      }
    }
  }
 
  .result {
    margin-top: 20rpx;
 
    .result-btn {
      display: flex;
      align-items: center;
      justify-content: space-between;
 
      .btn-item {
        margin: initial;
      }
    }
 
    .output {
      font-size: 13px;
      line-height: 1.5;
      margin-top: 20rpx;
      height: 500rpx;
      width: 100%;
      background-color: #333333;
      color: white;
      padding: 20rpx;
      border-radius: 8rpx;
      box-sizing: border-box;
    }
  }
</style>