This commit is contained in:
TonyChyi
2018-03-19 10:25:16 +08:00
parent 3ab177a7fe
commit a8b3540a37
12 changed files with 363 additions and 1 deletions

23
serialPort/get_serial.go Normal file
View File

@@ -0,0 +1,23 @@
package serialPort
import (
"io/ioutil"
"regexp"
)
// GetSerialPorts is to list all serial port for Arduino device.
func GetSerialPorts() ([]string, error) {
f, err := ioutil.ReadDir("/dev")
if err != nil {
return nil, err
}
var fileList []string
for _, file := range f {
if regexp.MustCompile("^ttyACM([0-9]+)|^cu.usbmodem").MatchString(file.Name()) {
fileList = append(fileList, file.Name())
}
}
return fileList, nil
}

View File

@@ -0,0 +1,17 @@
package serialPort
import (
"testing"
)
func TestGetSerialPorts(T *testing.T) {
portList, err := GetSerialPorts()
if err != nil {
T.Error("ERROR: ", err, "\n")
}
T.Log("Show all serial ports")
for _, p := range portList {
T.Log("\t", p)
}
}