This repository was archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic windows port detection (#24)
* add basic windows port detection * Update app.go
- Loading branch information
1 parent
eb474c7
commit 980283e
Showing
5 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// +build linux | ||
|
||
package flwserial | ||
|
||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
func SetupCmd() { | ||
return nil, errors.New("Not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// +build windows | ||
|
||
package flwserial | ||
|
||
import ( | ||
"golang.org/x/sys/windows/registry" | ||
"github.com/tarm/serial" | ||
"log" | ||
"bufio" | ||
"errors" | ||
) | ||
|
||
func GetValidPort() (*serial.Port, error) { | ||
//Find active serial ports from registry key | ||
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `HARDWARE\\DEVICEMAP\\SERIALCOMM`, registry.QUERY_VALUE) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer k.Close() | ||
ki, err := k.Stat() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
r, err := k.ReadValueNames(int(ki.ValueCount)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
kvalue := make([]string, ki.ValueCount) | ||
for i, test := range r { | ||
q, _, err := k.GetStringValue(test) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
kvalue[i] = q | ||
} | ||
var s *serial.Port | ||
for _, element := range kvalue { | ||
c := &serial.Config{Name: element, Baud: 9600} | ||
s, err = serial.OpenPort(c) | ||
if err != nil { | ||
log.Print("Can't connect to port") | ||
continue | ||
} | ||
scanner := bufio.NewScanner(s) | ||
for scanner.Scan() { | ||
text := scanner.Text() | ||
//Rubbish way of checking if the device is correct | ||
if text[0] == '{' { | ||
return s, nil | ||
} | ||
} | ||
} | ||
return nil, errors.New("Error finding valid port") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters