done
This commit is contained in:
23
serialPort/get_serial.go
Normal file
23
serialPort/get_serial.go
Normal 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
|
||||
}
|
17
serialPort/serialPort_test.go
Normal file
17
serialPort/serialPort_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user