forked from tbrandon/mbserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servertu.go
67 lines (54 loc) · 1.44 KB
/
servertu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package mbserver
import (
"io"
"log"
"github.com/goburrow/serial"
)
// ListenRTU starts the Modbus server listening to a serial device.
// For example: err := s.ListenRTU(&serial.Config{Address: "/dev/ttyUSB0"})
func (s *Server) ListenRTU(serialConfig *serial.Config) (err error) {
port, err := serial.Open(serialConfig)
if err != nil {
log.Fatalf("failed to open %s: %v\n", serialConfig.Address, err)
}
s.ports = append(s.ports, port)
s.portsWG.Add(1)
go func() {
defer s.portsWG.Done()
s.acceptSerialRequests(port)
}()
return err
}
func (s *Server) acceptSerialRequests(port serial.Port) {
SkipFrameError:
for {
select {
case <-s.portsCloseChan:
return
default:
}
buffer := make([]byte, 512)
bytesRead, err := port.Read(buffer)
if err != nil {
if err != io.EOF {
log.Printf("serial read error %v\n", err)
}
return
}
if bytesRead != 0 {
// Set the length of the packet to the number of read bytes.
packet := buffer[:bytesRead]
frame, err := NewRTUFrame(packet)
if err != nil {
log.Printf("bad serial frame error %v\n", err)
//The next line prevents RTU server from exiting when it receives a bad frame. Simply discard the erroneous
//frame and wait for next frame by jumping back to the beginning of the 'for' loop.
log.Printf("Keep the RTU server running!!\n")
continue SkipFrameError
//return
}
request := &Request{port, frame}
s.requestChan <- request
}
}
}