serialmgr.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/goburrow/serial"
  6. "github.com/sirupsen/logrus"
  7. "lc/common/protocol"
  8. )
  9. var _once sync.Once
  10. var _single *SerialMgr
  11. type SerialMgr struct {
  12. mu sync.Mutex
  13. mapSerial map[uint8]*serialPort
  14. mapSerialPort map[uint8]*protocol.SerialPort
  15. }
  16. func GetSerialMgr() *SerialMgr {
  17. _once.Do(func() {
  18. _single = &SerialMgr{
  19. mapSerial: make(map[uint8]*serialPort),
  20. mapSerialPort: make(map[uint8]*protocol.SerialPort),
  21. }
  22. })
  23. return _single
  24. }
  25. func openSerialPort(sp *protocol.SerialPort) (*serialPort, error) {
  26. config := serial.Config{Address: sp.Address, BaudRate: sp.BaudRate, DataBits: int(sp.DataBits),
  27. StopBits: int(sp.StopBits), Parity: sp.Parity,
  28. Timeout: time.Duration(sp.Timeout) * time.Microsecond}
  29. var err error
  30. var serial serialPort
  31. serial.setSerialConfig(config)
  32. serial.SetAutoReconnect(SerialDefaultAutoReconnect)
  33. err = serial.connect()
  34. if err != nil {
  35. logrus.Errorf("打开串口[code=%d,address=%s]失败:%s", sp.Code, sp.Address, err.Error())
  36. return nil, err
  37. }
  38. logrus.Infof("打开串口[code=%d,address=%s]成功", sp.Code, sp.Address)
  39. return &serial, nil
  40. }
  41. func (o *SerialMgr) AddSerialPorts(mapSP map[uint8]*protocol.SerialPort) {
  42. if len(mapSP) == 0 {
  43. return
  44. }
  45. o.mu.Lock()
  46. defer o.mu.Unlock()
  47. //先全部关闭并删除
  48. for k, v := range o.mapSerial {
  49. v.Close()
  50. delete(o.mapSerial, k)
  51. }
  52. //重新打开
  53. for _, sp := range mapSP {
  54. var s protocol.SerialPort
  55. s = *sp
  56. newsp, _ := openSerialPort(&s)
  57. if newsp != nil {
  58. o.mapSerial[sp.Code] = newsp
  59. }
  60. }
  61. o.mapSerialPort = mapSP
  62. }
  63. func (o *SerialMgr) UpdateSerialPort(sp *protocol.SerialPort) error {
  64. if sp == nil {
  65. return nil
  66. }
  67. o.mu.Lock()
  68. defer o.mu.Unlock()
  69. if s, ok := o.mapSerial[sp.Code]; ok { //存在则关闭,并重新打开
  70. err := s.Close() //关闭
  71. if err != nil {
  72. logrus.Errorf("关闭串口[code=%d,address=%s]失败:%s", sp.Code, sp.Address, err.Error())
  73. }
  74. delete(o.mapSerial, sp.Code)
  75. }
  76. newsp, err := openSerialPort(sp)
  77. if newsp != nil {
  78. o.mapSerial[sp.Code] = newsp
  79. }
  80. o.mapSerialPort[sp.Code] = sp
  81. return err
  82. }
  83. func (o *SerialMgr) RemoveSerialPort(sp *protocol.SerialPort) error {
  84. if sp == nil {
  85. return nil
  86. }
  87. var err error
  88. o.mu.Lock()
  89. defer o.mu.Unlock()
  90. if s, ok := o.mapSerial[sp.Code]; ok { //存在则关闭,并重新打开
  91. err = s.Close() //关闭
  92. delete(o.mapSerial, sp.Code)
  93. }
  94. delete(o.mapSerialPort, sp.Code)
  95. return err
  96. }
  97. func (o *SerialMgr) GetSerialPort(code uint8) *serialPort {
  98. o.mu.Lock()
  99. defer o.mu.Unlock()
  100. if s, ok := o.mapSerial[code]; ok {
  101. return s
  102. }
  103. if sp, ok := o.mapSerialPort[code]; ok {
  104. newsp, _ := openSerialPort(sp)
  105. if newsp != nil {
  106. o.mapSerial[code] = newsp
  107. return newsp
  108. }
  109. }
  110. return nil
  111. }