forked from johnlauer/serial-port-json-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seriallist_windows.go
283 lines (233 loc) · 7.93 KB
/
seriallist_windows.go
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
//"fmt"
//"github.com/lxn/win"
"github.com/mattn/go-ole"
"github.com/mattn/go-ole/oleutil"
//"github.com/tarm/goserial"
"github.com/johnlauer/goserial"
"log"
"os"
"strings"
//"encoding/binary"
"strconv"
"sync"
//"syscall"
)
var (
serialListWindowsWg sync.WaitGroup
)
func getList() ([]OsSerialPort, os.SyscallError) {
// use a queue to do this to avoid conflicts
// we've been getting crashes when this getList is requested
// too many times too fast. i think it's something to do with
// the unsafe syscalls overwriting memory
// this will only block if waitgroupctr > 0. so first time
// in shouldn't block
serialListWindowsWg.Wait()
serialListWindowsWg.Add(1)
arr, sysCallErr := getListViaWmiPnpEntity()
serialListWindowsWg.Done()
//arr = make([]OsSerialPort, 0)
// see if array has any data, if not fallback to the traditional
// com port list model
if len(arr) == 0 {
// assume it failed
arr, sysCallErr = getListViaOpen()
}
// see if array has any data, if not fallback to looking at
// the registry list
/*
arr = make([]OsSerialPort, 0)
if len(arr) == 0 {
// assume it failed
arr, sysCallErr = getListViaRegistry()
}
*/
return arr, sysCallErr
}
func getListSynchronously() {
}
func getListViaWmiPnpEntity() ([]OsSerialPort, os.SyscallError) {
log.Println("Doing getListViaWmiPnpEntity()")
// this method panics a lot and i'm not sure why, just catch
// the panic and return empty list
defer func() {
if e := recover(); e != nil {
// e is the interface{} typed-value we passed to panic()
log.Println("Got panic: ", e) // Prints "Whoops: boom!"
}
}()
var err os.SyscallError
//var friendlyName string
// init COM, oh yeah
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("WbemScripting.SWbemLocator")
defer unknown.Release()
wmi, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer wmi.Release()
// service is a SWbemServices
serviceRaw, _ := oleutil.CallMethod(wmi, "ConnectServer")
service := serviceRaw.ToIDispatch()
defer service.Release()
// result is a SWBemObjectSet
//pname := syscall.StringToUTF16("SELECT * FROM Win32_PnPEntity where Name like '%" + "COM35" + "%'")
pname := "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0 and Name like '%(COM%'"
//pname := "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0"
resultRaw, err2 := oleutil.CallMethod(service, "ExecQuery", pname)
log.Println("Got result from oleutil.CallMethod")
if err2 != nil {
// we got back an error or empty list
log.Printf("Got an error back from oleutil.CallMethod. err:%v", err2)
return nil, err
}
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, _ := oleutil.GetProperty(result, "Count")
count := int(countVar.Val)
list := make([]OsSerialPort, count)
for i := 0; i < count; i++ {
// item is a SWbemObject, but really a Win32_Process
itemRaw, _ := oleutil.CallMethod(result, "ItemIndex", i)
item := itemRaw.ToIDispatch()
defer item.Release()
asString, _ := oleutil.GetProperty(item, "Name")
log.Println(asString.ToString())
// get the com port
//if false {
s := strings.Split(asString.ToString(), "(COM")[1]
s = "COM" + s
s = strings.Split(s, ")")[0]
list[i].Name = s
list[i].FriendlyName = asString.ToString()
//}
}
for index, element := range list {
log.Println("index ", index, " element ", element.Name+
" friendly ", element.FriendlyName)
}
return list, err
}
func getListViaOpen() ([]OsSerialPort, os.SyscallError) {
log.Println("Doing getListViaOpen(). Will try to open COM1 to COM99.")
var err os.SyscallError
list := make([]OsSerialPort, 100)
var igood int = 0
for i := 0; i < 100; i++ {
prtname := "COM" + strconv.Itoa(i)
conf := &serial.Config{Name: prtname, Baud: 9600}
sp, err := serial.OpenPort(conf)
//log.Println("Just tried to open port", prtname)
if err == nil {
//log.Println("Able to open port", prtname)
list[igood].Name = prtname
sp.Close()
list[igood].FriendlyName = prtname
//list[igood].FriendlyName = getFriendlyName(prtname)
igood++
}
}
for index, element := range list[:igood] {
log.Println("index ", index, " element ", element.Name+
" friendly ", element.FriendlyName)
}
return list[:igood], err
}
/*
func getListViaRegistry() ([]OsSerialPort, os.SyscallError) {
log.Println("Doing getListViaRegistry()")
var err os.SyscallError
var root win.HKEY
rootpath, _ := syscall.UTF16PtrFromString("HARDWARE\\DEVICEMAP\\SERIALCOMM")
log.Println(win.RegOpenKeyEx(win.HKEY_LOCAL_MACHINE, rootpath, 0, win.KEY_READ, &root))
var name_length uint32 = 72
var key_type uint32
var lpDataLength uint32 = 72
var zero_uint uint32 = 0
name := make([]uint16, 72)
lpData := make([]byte, 72)
var retcode int32
retcode = 0
for retcode == 0 {
retcode = win.RegEnumValue(root, zero_uint, &name[0], &name_length, nil, &key_type, &lpData[0], &lpDataLength)
log.Println("Retcode:", retcode)
log.Println("syscall name: "+syscall.UTF16ToString(name[:name_length-2])+"---- name_length:", name_length)
log.Println("syscall lpdata:"+string(lpData[:lpDataLength-2])+"--- lpDataLength:", lpDataLength)
//log.Println()
zero_uint++
}
win.RegCloseKey(root)
win.RegOpenKeyEx(win.HKEY_LOCAL_MACHINE, rootpath, 0, win.KEY_READ, &root)
list := make([]OsSerialPort, zero_uint)
var i uint32 = 0
for i = 0; i < zero_uint; i++ {
win.RegEnumValue(root, i-1, &name[0], &name_length, nil, &key_type, &lpData[0], &lpDataLength)
//name := string(lpData[:lpDataLength])
//name = name[:strings.Index(name, '\0')]
//nameb := []byte(strings.TrimSpace(string(lpData[:lpDataLength])))
//list[i].Name = string(nameb)
//list[i].Name = string(name[:strings.Index(name, "\0")])
//list[i].Name = fmt.Sprintf("%s", string(lpData[:lpDataLength-1]))
pname := make([]uint16, (lpDataLength-2)/2)
pname = convertByteArrayToUint16Array(lpData[:lpDataLength-2], lpDataLength-2)
list[i].Name = syscall.UTF16ToString(pname)
log.Println("The length of the name is:", len(list[i].Name))
log.Println("list[i].Name=" + list[i].Name + "---")
//list[i].FriendlyName = getFriendlyName(list[i].Name)
list[i].FriendlyName = getFriendlyName("COM34")
}
win.RegCloseKey(root)
return list, err
}
*/
func convertByteArrayToUint16Array(b []byte, mylen uint32) []uint16 {
log.Println("converting. len:", mylen)
var i uint32
ret := make([]uint16, mylen/2)
for i = 0; i < mylen; i += 2 {
//ret[i/2] = binary.LittleEndian.Uint16(b[i : i+1])
ret[i/2] = uint16(b[i]) | uint16(b[i+1])<<8
}
return ret
}
func getFriendlyName(portname string) string {
// this method panics a lot and i'm not sure why, just catch
// the panic and return empty list
defer func() {
if e := recover(); e != nil {
// e is the interface{} typed-value we passed to panic()
log.Println("Got panic: ", e) // Prints "Whoops: boom!"
}
}()
var friendlyName string
// init COM, oh yeah
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("WbemScripting.SWbemLocator")
defer unknown.Release()
wmi, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer wmi.Release()
// service is a SWbemServices
serviceRaw, _ := oleutil.CallMethod(wmi, "ConnectServer")
service := serviceRaw.ToIDispatch()
defer service.Release()
// result is a SWBemObjectSet
//pname := syscall.StringToUTF16("SELECT * FROM Win32_PnPEntity where Name like '%" + "COM35" + "%'")
pname := "SELECT * FROM Win32_PnPEntity where Name like '%" + portname + "%'"
resultRaw, _ := oleutil.CallMethod(service, "ExecQuery", pname)
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, _ := oleutil.GetProperty(result, "Count")
count := int(countVar.Val)
for i := 0; i < count; i++ {
// item is a SWbemObject, but really a Win32_Process
itemRaw, _ := oleutil.CallMethod(result, "ItemIndex", i)
item := itemRaw.ToIDispatch()
defer item.Release()
asString, _ := oleutil.GetProperty(item, "Name")
println(asString.ToString())
friendlyName = asString.ToString()
}
return friendlyName
}