2020-07-10 09:04:18 +08:00
|
|
|
package device
|
|
|
|
|
|
|
|
import (
|
2020-07-21 10:54:22 +08:00
|
|
|
"encoding/json"
|
2021-02-20 21:19:11 +08:00
|
|
|
"goAdapter/setting"
|
2020-07-10 09:04:18 +08:00
|
|
|
"log"
|
2020-07-21 10:54:22 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2021-05-11 22:05:45 +08:00
|
|
|
|
|
|
|
"github.com/tarm/serial"
|
2020-07-10 09:04:18 +08:00
|
|
|
)
|
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
type SerialInterfaceParam struct {
|
|
|
|
Name string `json:"Name"`
|
|
|
|
BaudRate string `json:"BaudRate"`
|
|
|
|
DataBits string `json:"DataBits"` //数据位: 5, 6, 7 or 8 (default 8)
|
|
|
|
StopBits string `json:"StopBits"` //停止位: 1 or 2 (default 1)
|
|
|
|
Parity string `json:"Parity"` //校验: N - None, E - Even, O - Odd (default E),(The use of no parity requires 2 stop bits.)
|
|
|
|
Timeout string `json:"Timeout"` //通信超时
|
|
|
|
Interval string `json:"Interval"` //通信间隔
|
2020-07-10 09:04:18 +08:00
|
|
|
}
|
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
type CommunicationSerialTemplate struct {
|
2021-05-19 19:53:24 +08:00
|
|
|
Name string `json:"Name"` //接口名称
|
|
|
|
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
|
2021-02-20 21:19:11 +08:00
|
|
|
Param SerialInterfaceParam `json:"Param"` //接口参数
|
|
|
|
Port *serial.Port `json:"-"` //通信句柄
|
2020-07-10 09:04:18 +08:00
|
|
|
}
|
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
var CommunicationSerialMap = make([]*CommunicationSerialTemplate, 0)
|
2020-07-10 09:04:18 +08:00
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
func (c *CommunicationSerialTemplate) Open() bool {
|
2020-07-10 09:04:18 +08:00
|
|
|
|
2020-07-21 10:54:22 +08:00
|
|
|
serialParam := c.Param
|
2021-02-20 21:19:11 +08:00
|
|
|
serialBaud, _ := strconv.Atoi(serialParam.BaudRate)
|
2020-07-10 09:04:18 +08:00
|
|
|
|
|
|
|
var serialParity serial.Parity
|
|
|
|
switch serialParam.Parity {
|
|
|
|
case "N":
|
|
|
|
serialParity = serial.ParityNone
|
|
|
|
case "O":
|
|
|
|
serialParity = serial.ParityOdd
|
|
|
|
case "E":
|
|
|
|
serialParity = serial.ParityEven
|
|
|
|
}
|
|
|
|
|
|
|
|
var serialStop serial.StopBits
|
|
|
|
switch serialParam.StopBits {
|
|
|
|
case "1":
|
|
|
|
serialStop = serial.Stop1
|
|
|
|
case "1.5":
|
|
|
|
serialStop = serial.Stop1Half
|
|
|
|
case "2":
|
|
|
|
serialStop = serial.Stop2
|
|
|
|
}
|
|
|
|
|
|
|
|
serialConfig := &serial.Config{
|
2021-02-20 21:19:11 +08:00
|
|
|
Name: serialParam.Name,
|
|
|
|
Baud: serialBaud,
|
|
|
|
Parity: serialParity,
|
|
|
|
StopBits: serialStop,
|
|
|
|
ReadTimeout: time.Millisecond * 1,
|
2020-07-10 09:04:18 +08:00
|
|
|
}
|
|
|
|
|
2020-07-31 16:22:43 +08:00
|
|
|
var err error
|
|
|
|
c.Port, err = serial.OpenPort(serialConfig)
|
2020-07-10 09:04:18 +08:00
|
|
|
if err != nil {
|
2021-07-22 11:41:15 +08:00
|
|
|
setting.Logger.Errorf("open serial err %v", err)
|
2020-07-10 09:04:18 +08:00
|
|
|
return false
|
2021-02-20 21:19:11 +08:00
|
|
|
} else {
|
2021-07-22 11:41:15 +08:00
|
|
|
setting.Logger.Debugf("open serial %s ok", c.Param.Name)
|
2020-07-10 09:04:18 +08:00
|
|
|
}
|
2020-07-21 10:54:22 +08:00
|
|
|
|
2020-07-10 09:04:18 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
func (c *CommunicationSerialTemplate) Close() bool {
|
2020-07-10 09:04:18 +08:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
func (c *CommunicationSerialTemplate) WriteData(data []byte) int {
|
2020-07-10 09:04:18 +08:00
|
|
|
|
2020-08-15 10:21:39 +08:00
|
|
|
//log.Printf("len is %d\n",len(data))
|
2020-07-31 16:22:43 +08:00
|
|
|
//log.Printf("c %+v\n",c)
|
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
if c.Port == nil {
|
|
|
|
setting.Logger.Errorf("serial writeData err")
|
2020-08-13 21:58:07 +08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
cnt, err := c.Port.Write(data)
|
|
|
|
if err != nil {
|
2020-07-31 16:22:43 +08:00
|
|
|
log.Println(err)
|
|
|
|
}
|
2020-07-10 09:04:18 +08:00
|
|
|
|
2020-07-10 16:10:32 +08:00
|
|
|
return cnt
|
2020-07-10 09:04:18 +08:00
|
|
|
}
|
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
func (c *CommunicationSerialTemplate) ReadData(data []byte) int {
|
2020-07-10 16:10:32 +08:00
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
if c.Port == nil {
|
2020-08-13 21:58:07 +08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-02-20 21:19:11 +08:00
|
|
|
cnt, _ := c.Port.Read(data)
|
2020-07-10 09:04:18 +08:00
|
|
|
|
2020-07-10 16:10:32 +08:00
|
|
|
return cnt
|
2020-07-10 09:04:18 +08:00
|
|
|
}
|
2020-07-21 10:54:22 +08:00
|
|
|
|
2021-05-11 22:05:45 +08:00
|
|
|
func (c *CommunicationSerialTemplate) GetName() string {
|
|
|
|
return c.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommunicationSerialTemplate) GetTimeOut() string {
|
|
|
|
return c.Param.Timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommunicationSerialTemplate) GetInterval() string {
|
|
|
|
return c.Param.Interval
|
|
|
|
}
|
|
|
|
|
2021-05-19 19:53:24 +08:00
|
|
|
//func NewCommunicationSerialTemplate(commName, commType string, param SerialInterfaceParam) *CommunicationSerialTemplate {
|
|
|
|
//
|
|
|
|
// return &CommunicationSerialTemplate{
|
|
|
|
// Param: param,
|
|
|
|
// CommunicationTemplate: CommunicationTemplate{
|
|
|
|
// Name: commName,
|
|
|
|
// Type: commType,
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
//}
|
2020-07-21 10:54:22 +08:00
|
|
|
|
|
|
|
func ReadCommSerialInterfaceListFromJson() bool {
|
|
|
|
|
|
|
|
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
fileDir := exeCurDir + "/selfpara/commSerialInterface.json"
|
|
|
|
|
|
|
|
if fileExist(fileDir) == true {
|
|
|
|
fp, err := os.OpenFile(fileDir, os.O_RDONLY, 0777)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("open commSerialInterface.json err", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
data := make([]byte, 20480)
|
|
|
|
dataCnt, err := fp.Read(data)
|
|
|
|
|
2020-07-22 09:33:05 +08:00
|
|
|
err = json.Unmarshal(data[:dataCnt], &CommunicationSerialMap)
|
2020-07-21 10:54:22 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println("commSerialInterface unmarshal err", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
log.Println("commSerialInterface.json is not exist")
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteCommSerialInterfaceListToJson() {
|
|
|
|
|
|
|
|
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
|
|
|
|
fileDir := exeCurDir + "/selfpara/commSerialInterface.json"
|
|
|
|
|
|
|
|
fp, err := os.OpenFile(fileDir, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("open commSerialInterface.json err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
|
2020-07-22 09:33:05 +08:00
|
|
|
sJson, _ := json.Marshal(CommunicationSerialMap)
|
2020-07-21 10:54:22 +08:00
|
|
|
|
|
|
|
_, err = fp.Write(sJson)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("write commSerialInterface.json err", err)
|
|
|
|
}
|
|
|
|
log.Println("write commSerialInterface.json sucess")
|
|
|
|
}
|