修改:

1、可用串口设备改成从ini配置文件中获取
This commit is contained in:
pengwang 2020-08-12 19:41:16 +08:00
parent f18b2dac9a
commit ce4c7c893d
4 changed files with 42 additions and 10 deletions

View File

@ -2,3 +2,5 @@
AppMode = debug
HttpPort = :8080
[serial]
Name = /dev/tty.usbserial-1410,/dev/ttyS1

View File

@ -9,14 +9,24 @@ import (
func apiGetSerial(context *gin.Context) {
type SerialPortNameTemplate struct{
Name string `json:"Name"`
}
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data []setting.SerialPortNameTemplate `json:"Data"`
Data []SerialPortNameTemplate `json:"Data"`
}{
Code: "0",
Message: "",
Data: setting.SerialPortNameTemplateMap[:],
Data: make([]SerialPortNameTemplate,0),
}
SerialPortName := SerialPortNameTemplate{}
for _,v := range setting.SerialPortNameTemplateMap.Name{
SerialPortName.Name = v
aParam.Data = append(aParam.Data,SerialPortName)
}
sJson, _ := json.Marshal(aParam)

View File

@ -17,6 +17,20 @@ func LoadServer(file *ini.File){
HttpPort = file.Section("server").Key("HttpPort").MustString(":8080")
}
func LoadSerial(file *ini.File){
//type SerialPortTemplate struct{
// Name []string `json:"Name"`
//}
//
//SerialPortName := &SerialPortTemplate{}
err := file.Section("serial").MapTo(&SerialPortNameTemplateMap)
if err != nil{
log.Println(err)
}
}
/**************获取配置信息************************/
func GetConf() {
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
@ -34,11 +48,15 @@ func GetConf() {
cfg.Section("server").Key("AppMode").SetValue("debug")
cfg.Section("server").Key("HttpPort").SetValue(":8080")
cfg.Section("serial").Key("serialPort").SetValue("/dev/ttyS0")
cfg.SaveTo(path)
return
}
LoadServer(iniFile)
LoadSerial(iniFile)
log.Printf("serial %+v\n",SerialPortNameTemplateMap)
}

View File

@ -1,13 +1,15 @@
package setting
type SerialPortNameTemplate struct{
Name string `json:"Name"`
Name []string `json:"Name"`
}
var SerialPortNameTemplateMap = [...]SerialPortNameTemplate{
{Name:"/dev/ttyUSB0"},
{Name:"/dev/ttyUSB1"},
{Name:"/dev/ttyS0"},
{Name:"/dev/ttyS1"},
{Name:"/dev/tty.SLAB_USBtoUART"},
}
//var SerialPortNameTemplateMap = [...]SerialPortNameTemplate{
// {Name:"/dev/ttyUSB0"},
// {Name:"/dev/ttyUSB1"},
// {Name:"/dev/ttyS0"},
// {Name:"/dev/ttyS1"},
// {Name:"/dev/tty.SLAB_USBtoUART"},
//}
var SerialPortNameTemplateMap = SerialPortNameTemplate{}