add stopbits parameter , add serial docs
This commit is contained in:
+4
-1
@@ -17,9 +17,10 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -28,6 +29,7 @@ var (
|
||||
device string
|
||||
baudrate int
|
||||
bytesize byte
|
||||
stopbits byte
|
||||
lograw bool
|
||||
|
||||
dbFile string
|
||||
@@ -95,6 +97,7 @@ func initConfig() {
|
||||
device = viper.GetString("serial.device")
|
||||
baudrate = viper.GetInt("serial.baudrate")
|
||||
bytesize = byte(viper.GetInt("serial.bytesize"))
|
||||
stopbits = byte(viper.GetInt("serial.stopbits"))
|
||||
lograw = viper.GetBool("serial.lograw")
|
||||
dbFile = viper.GetString("sqlite.file")
|
||||
dbInit = viper.GetBool("sqlite.init")
|
||||
|
||||
+1
-1
@@ -86,7 +86,7 @@ func start() {
|
||||
for utils.ServerRunning {
|
||||
if !utils.IsPortOpen() {
|
||||
log.Println("try to open port")
|
||||
err := utils.OpenPort(device, baudrate, bytesize)
|
||||
err := utils.OpenPort(device, baudrate, bytesize, stopbits)
|
||||
if err != nil {
|
||||
log.Fatal("error in open serial port ", err)
|
||||
}
|
||||
|
||||
+3
-2
@@ -16,9 +16,10 @@ limitations under the License.
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"it2000.com.cn/tele-recv/utils"
|
||||
"log"
|
||||
)
|
||||
|
||||
// testCmd represents the test command
|
||||
@@ -35,7 +36,7 @@ Test load sqlite database and init table`,
|
||||
|
||||
func test() {
|
||||
log.Println("Testing environment")
|
||||
_ = utils.OpenPort(device, baudrate, bytesize)
|
||||
_ = utils.OpenPort(device, baudrate, bytesize, stopbits)
|
||||
|
||||
err := utils.InitDb(dbFile, dbInit)
|
||||
if err != nil {
|
||||
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
serial:
|
||||
device: ttyS1
|
||||
baudrate: 9600
|
||||
bytesize: 7
|
||||
bytesize: 8
|
||||
stopbits: 7
|
||||
lograw: true
|
||||
|
||||
|
||||
|
||||
+13
-3
@@ -12,13 +12,21 @@ var (
|
||||
device string
|
||||
baudrate int
|
||||
bytesize byte
|
||||
stopbits byte
|
||||
port *serial.Port
|
||||
portOpened = false
|
||||
)
|
||||
|
||||
func OpenPort(device string, baudrate int, bytesize byte) error {
|
||||
log.Println("opening port device:", device, ", baudrate=", baudrate, ", bytesize=", bytesize)
|
||||
config := &serial.Config{Name: device, Baud: baudrate, Size: bytesize, ReadTimeout: time.Second * 1}
|
||||
const readDuration = 500 * time.Millisecond
|
||||
|
||||
// OpenPort open serial port
|
||||
func OpenPort(device string, baudrate int, bytesize byte, stopbits byte) error {
|
||||
log.Println("opening port device:", device, ", baudrate=", baudrate, ", bytesize=", bytesize, "stopbits=", stopbits)
|
||||
config := &serial.Config{Name: device,
|
||||
Baud: baudrate,
|
||||
Size: bytesize,
|
||||
StopBits: serial.StopBits(stopbits),
|
||||
ReadTimeout: readDuration}
|
||||
var err error
|
||||
port, err = serial.OpenPort(config)
|
||||
if err != nil {
|
||||
@@ -30,6 +38,7 @@ func OpenPort(device string, baudrate int, bytesize byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//ReadPort read byte array from port
|
||||
func ReadPort() ([]byte, int) {
|
||||
buffer := make([]byte, 256)
|
||||
if !ServerRunning {
|
||||
@@ -43,6 +52,7 @@ func ReadPort() ([]byte, int) {
|
||||
return buffer, count
|
||||
}
|
||||
|
||||
//IsPortOpen check serial port status
|
||||
func IsPortOpen() bool {
|
||||
return portOpened
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user