forked from tbrandon/mbserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverascii.go
67 lines (54 loc) · 1.46 KB
/
serverascii.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"
)
// ListenASCII starts the Modbus server listening to a serial device.
// For example: err := s.ListenASCII(&serial.Config{Address: "/dev/ttyUSB0"})
func (s *Server) ListenASCII(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.acceptSerialASCIIRequests(port)
}()
return err
}
func (s *Server) acceptSerialASCIIRequests(port serial.Port) {
SkipFrameError:
for {
select {
case <-s.portsCloseChan:
return
default:
}
buffer := make([]byte, 513)
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 := NewASCIIFrame(packet)
if err != nil {
log.Printf("bad serial frame error %v\n", err)
//The next line prevents ASCII 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 ASCII server running!!\n")
continue SkipFrameError
//return
}
request := &Request{port, frame}
s.requestChan <- request
}
}
}