-
Notifications
You must be signed in to change notification settings - Fork 39
Implement Modbus UDP #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| package modbus | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "sync" | ||
| ) | ||
|
|
||
| // ErrADURequestLength informs about a wrong ADU request length. | ||
| type ErrADURequestLength int | ||
|
|
||
| func (length ErrADURequestLength) Error() string { | ||
| return fmt.Sprintf("modbus: ADU request length '%d' must not be less than 2", length) | ||
| } | ||
|
|
||
| // ErrADUResponseLength informs about a wrong ADU request length. | ||
| type ErrADUResponseLength int | ||
|
|
||
| func (length ErrADUResponseLength) Error() string { | ||
| return fmt.Sprintf("modbus: ADU response length '%d' must not be less than 2", length) | ||
| } | ||
|
|
||
| // RTUOverUDPClientHandler implements Packager and Transporter interface. | ||
| type RTUOverUDPClientHandler struct { | ||
| rtuPackager | ||
| rtuUDPTransporter | ||
| } | ||
|
|
||
| // NewRTUOverUDPClientHandler allocates and initializes a RTUOverUDPClientHandler. | ||
| func NewRTUOverUDPClientHandler(address string) *RTUOverUDPClientHandler { | ||
| handler := &RTUOverUDPClientHandler{} | ||
| handler.Address = address | ||
| return handler | ||
| } | ||
|
|
||
| // RTUOverUDPClient creates RTU over UDP client with default handler and given connect string. | ||
| func RTUOverUDPClient(address string) Client { | ||
| handler := NewRTUOverUDPClientHandler(address) | ||
| return NewClient(handler) | ||
| } | ||
|
|
||
| // rtuUDPTransporter implements Transporter interface. | ||
| type rtuUDPTransporter struct { | ||
| // Connect string | ||
| Address string | ||
| // Transmission logger | ||
| Logger logger | ||
|
|
||
| // UDP connection | ||
| mu sync.Mutex | ||
| conn net.Conn | ||
| } | ||
|
|
||
| // Send sends data to server and ensures adequate response for request type | ||
| func (mb *rtuUDPTransporter) Send(aduRequest []byte) (aduResponse []byte, err error) { | ||
| mb.mu.Lock() | ||
| defer mb.mu.Unlock() | ||
|
|
||
| // Check ADU request length | ||
| if len(aduRequest) < 2 { | ||
| err = ErrADURequestLength(len(aduRequest)) | ||
| return | ||
| } | ||
|
|
||
| // Establish a new connection if not connected | ||
| if err = mb.connect(); err != nil { | ||
| return | ||
| } | ||
|
|
||
| // Send the request | ||
| mb.logf("modbus: send % x\n", aduRequest) | ||
| if _, err = mb.conn.Write(aduRequest); err != nil { | ||
| return | ||
| } | ||
| function := aduRequest[1] | ||
| functionFail := aduRequest[1] & 0x80 | ||
| bytesToRead := calculateResponseLength(aduRequest) | ||
|
|
||
| var n int | ||
| var n1 int | ||
| var data [rtuMaxSize]byte | ||
| // We first read the minimum length and then read either the full package | ||
| // or the error package, depending on the error status (byte 2 of the response) | ||
| n, err = io.ReadAtLeast(mb.conn, data[:], rtuMinSize) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| // Check ADU response length | ||
| if len(data) < 2 { | ||
| err = ErrADUResponseLength(len(data)) | ||
| return | ||
| } | ||
|
|
||
| // if the function is correct | ||
| if data[1] == function { | ||
StefanNienhuis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // we read the rest of the bytes | ||
| if n < bytesToRead { | ||
| if bytesToRead > rtuMinSize && bytesToRead <= rtuMaxSize { | ||
| n1, err = io.ReadFull(mb.conn, data[n:bytesToRead]) | ||
| n += n1 | ||
| } | ||
| } | ||
| } else if data[1] == functionFail { | ||
| // for error we need to read 5 bytes | ||
| if n < rtuExceptionSize { | ||
| n1, err = io.ReadFull(mb.conn, data[n:rtuExceptionSize]) | ||
| } | ||
| n += n1 | ||
| } | ||
|
|
||
| if err != nil { | ||
| return | ||
| } | ||
| aduResponse = data[:n] | ||
| mb.logf("modbus: recv % x\n", aduResponse) | ||
| return | ||
| } | ||
|
|
||
| func (mb *rtuUDPTransporter) logf(format string, v ...interface{}) { | ||
| if mb.Logger != nil { | ||
| mb.Logger.Printf(format, v...) | ||
| } | ||
| } | ||
|
|
||
| // Connect establishes a new connection to the address in Address. | ||
| func (mb *rtuUDPTransporter) Connect() error { | ||
| mb.mu.Lock() | ||
| defer mb.mu.Unlock() | ||
|
|
||
| return mb.connect() | ||
| } | ||
|
|
||
| // connect establishes a new connection to the address in Address. Caller must hold the mutex before calling this method. | ||
| // Since UDP is connectionless this does little more than setting up the connection object. | ||
| func (mb *rtuUDPTransporter) connect() error { | ||
| if mb.conn == nil { | ||
| dialer := net.Dialer{} | ||
| conn, err := dialer.Dial("udp", mb.Address) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| mb.conn = conn | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Close closes current connection. | ||
| func (mb *rtuUDPTransporter) Close() error { | ||
| mb.mu.Lock() | ||
| defer mb.mu.Unlock() | ||
|
|
||
| return mb.close() | ||
| } | ||
|
|
||
| // close closes current connection. Caller must hold the mutex before calling this method. | ||
| // Since UDP is connectionless this does little more than freeing up the connection object. | ||
| func (mb *rtuUDPTransporter) close() (err error) { | ||
| if mb.conn != nil { | ||
| err = mb.conn.Close() | ||
| mb.conn = nil | ||
| } | ||
| return | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.