commit 2021-12-16]11:54:54

This commit is contained in:
Godopu 2021-12-16 11:54:57 +09:00
parent ab08e7ab15
commit 37705d6fe5
2 changed files with 11 additions and 12 deletions

13
cli.go
View File

@ -65,14 +65,13 @@ func AddRecvListener(h EventHandler) {
// } // }
func Run() error { func Run() error {
iface, err := puserial.InitDevice() ifaces, err := puserial.InitDevice()
if err != nil { if err != nil {
if err.Error() != "USB Not found" { return err
return err }
}
} else { for _, e := range ifaces {
// _managerObj.onAdd(iface) _managerObj.onAdded(e)
_managerObj.onAdded(iface)
} }
go puserial.WatchNewDevice(ctx, ch_discover) go puserial.WatchNewDevice(ctx, ch_discover)

View File

@ -2,7 +2,6 @@ package puserial
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
@ -11,19 +10,20 @@ import (
"github.com/rjeczalik/notify" "github.com/rjeczalik/notify"
) )
func InitDevice() (string, error) { func InitDevice() ([]string, error) {
fs, err := ioutil.ReadDir("/dev") fs, err := ioutil.ReadDir("/dev")
if err != nil { if err != nil {
return "", err return nil, err
} }
var result []string = nil
for _, f := range fs { for _, f := range fs {
if strings.Contains(f.Name(), "ttyACM") { if strings.Contains(f.Name(), "ttyACM") {
return filepath.Join("/dev", f.Name()), nil result = append(result, filepath.Join("/dev", f.Name()))
} }
} }
return "", errors.New("USB Not found") return result, nil
} }
func WatchNewDevice(ctx context.Context, ch_discover chan<- notify.EventInfo) error { func WatchNewDevice(ctx context.Context, ch_discover chan<- notify.EventInfo) error {