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
|
mainWindow.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
// console.log(event,'<---evnet')
console.log(portList,'<----portList')
// console.log(webContents,'<---webContents')
// console.log(callback,'<-----callback')
// Add listeners to handle ports being added or removed before the callback for `select-serial-port`
// is called.
// 串口设备插入监听
mainWindow.webContents.session.on('serial-port-added', (event, port) => {
console.log('serial-port-added FIRED WITH', port)
// Optionally update portList to add the new port
})
// 串口设备拔出监听
mainWindow.webContents.session.on('serial-port-removed', (event, port) => {
console.log('serial-port-removed FIRED WITH', port)
// Optionally update portList to remove the port
})
event.preventDefault()
// 监听到设备进行设备选择
if (portList && portList.length > 0) {
callback(portList[0].portId)
} else {
// eslint-disable-next-line standard/no-callback-literal
callback('') // Could not find any matching devices
}
})
mainWindow.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
if (permission === 'serial' && details.securityOrigin === 'file:///') {
return true
}
return false
})
mainWindow.webContents.session.setDevicePermissionHandler((details) => {
if (details.deviceType === 'serial' && details.origin === 'file://') {
return true
}
return false
})
|