1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package lc
- import (
- "github.com/jacobsa/go-serial/serial"
- "github.com/sirupsen/logrus"
- "lc-smartX/lc/model"
- "lc-smartX/util"
- )
- type Notifier interface {
- Notify()
- }
- func StartEventServer() {
- S = &Server{Radios: util.Config.Radios, Notifiers: make(map[string]Notifier, 4)}
- S.start()
- }
- var S *Server
- type Server struct {
- Radios []model.RadioInfo
- Notifiers map[string]Notifier //485通道名
- }
- func (s *Server) start() {
- for _, radio := range S.Radios {
- go OpenSerial(radio.Port)
- }
- }
- func RegisterCallback(branch byte, notifier Notifier) {
- for _, radio := range S.Radios {
- //关联主路led屏和支路雷达;关联支路led屏和主路雷达
- if branch == 0 && radio.Branch == 1 || branch == 1 && radio.Branch == 0 {
- S.Notifiers[radio.Port] = notifier
- }
- }
- }
- func (s *Server) Callback(port string) {
- notifier, ok := s.Notifiers[port]
- if !ok {
- logrus.Errorf("回调函数注册表没有该ip:%s", port)
- return
- }
- notifier.Notify()
- }
- func OpenSerial(portName string) {
- // 配置串口参数
- options := serial.OpenOptions{
- PortName: portName, // /dev/ttymxc4 6 3
- BaudRate: 9600,
- DataBits: 8,
- StopBits: 1,
- MinimumReadSize: 4,
- }
- // 打开串口
- port, err := serial.Open(options)
- if err != nil {
- panic(err.Error())
- }
- // 关闭串口
- defer port.Close()
- for {
- // 读取数据
- buf := make([]byte, 128)
- n, err := port.Read(buf)
- if err != nil {
- break
- }
- if n < 8 {
- continue
- }
- result := false
- if buf[0] == 'x' && (buf[1] > '0' || buf[2] > '0' || buf[3] > '0') {
- result = true
- }
- if result {
- S.Callback(portName)
- }
- }
- }
|