@@ -6,14 +6,16 @@ import (
66 "github.com/mattn/go-ole"
77 "github.com/mattn/go-ole/oleutil"
88 //"github.com/tarm/goserial"
9- "github.com/johnlauer/goserial"
9+ //"github.com/johnlauer/goserial"
10+ "github.com/facchinm/go-serial"
1011 "log"
1112 "os"
1213 "strings"
1314 //"encoding/binary"
1415 "strconv"
1516 "sync"
1617 //"syscall"
18+ "regexp"
1719)
1820
1921var (
@@ -41,10 +43,12 @@ func getList() ([]OsSerialPort, os.SyscallError) {
4143
4244 // see if array has any data, if not fallback to the traditional
4345 // com port list model
44- if len (arr ) == 0 {
45- // assume it failed
46- arr , sysCallErr = getListViaOpen ()
47- }
46+ /*
47+ if len(arr) == 0 {
48+ // assume it failed
49+ arr, sysCallErr = getListViaOpen()
50+ }
51+ */
4852
4953 // see if array has any data, if not fallback to looking at
5054 // the registry list
@@ -182,9 +186,25 @@ func getListViaWmiPnpEntity() ([]OsSerialPort, os.SyscallError) {
182186 deviceIdStr , _ := oleutil .GetProperty (item , "DeviceID" )
183187 devIdItems := strings .Split (deviceIdStr .ToString (), "&" )
184188 log .Printf ("DeviceId elements:%v" , devIdItems )
185- list [i ].SerialNumber = devIdItems [3 ]
186- list [i ].IdProduct = strings .Replace (devIdItems [1 ], "PID_" , "" , 1 )
187- list [i ].IdVendor = strings .Replace (devIdItems [0 ], "USB\\ VID_" , "" , 1 )
189+ if len (devIdItems ) > 3 {
190+ list [i ].SerialNumber = devIdItems [3 ]
191+ list [i ].IdProduct = strings .Replace (devIdItems [1 ], "PID_" , "" , 1 )
192+ list [i ].IdVendor = strings .Replace (devIdItems [0 ], "USB\\ VID_" , "" , 1 )
193+ } else {
194+ list [i ].SerialNumber = deviceIdStr .ToString ()
195+ pidMatch := regexp .MustCompile ("PID_(\\ d+)" ).FindAllStringSubmatch (deviceIdStr .ToString (), - 1 )
196+ if len (pidMatch ) > 0 {
197+ if len (pidMatch [0 ]) > 1 {
198+ list [i ].IdProduct = pidMatch [0 ][1 ]
199+ }
200+ }
201+ vidMatch := regexp .MustCompile ("VID_(\\ d+)" ).FindAllStringSubmatch (deviceIdStr .ToString (), - 1 )
202+ if len (vidMatch ) > 0 {
203+ if len (vidMatch [0 ]) > 1 {
204+ list [i ].IdVendor = vidMatch [0 ][1 ]
205+ }
206+ }
207+ }
188208
189209 manufStr , _ := oleutil .GetProperty (item , "Manufacturer" )
190210 list [i ].Manufacturer = manufStr .ToString ()
@@ -222,8 +242,13 @@ func getListViaOpen() ([]OsSerialPort, os.SyscallError) {
222242 var igood int = 0
223243 for i := 0 ; i < 100 ; i ++ {
224244 prtname := "COM" + strconv .Itoa (i )
225- conf := & serial.Config {Name : prtname , Baud : 9600 }
226- sp , err := serial .OpenPort (conf )
245+ //conf := &serial.Config{Name: prtname, Baud: 1200}
246+ mode := & serial.Mode {
247+ BaudRate : 1200 ,
248+ Vmin : 0 ,
249+ Vtimeout : 10 ,
250+ }
251+ sp , err := serial .OpenPort (prtname , mode )
227252 //log.Println("Just tried to open port", prtname)
228253 if err == nil {
229254 //log.Println("Able to open port", prtname)
0 commit comments