-
Notifications
You must be signed in to change notification settings - Fork 39
feat: introduce serial link recovery #123
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
Open
sansmoraxz
wants to merge
5
commits into
grid-x:master
Choose a base branch
from
sansmoraxz:feat/serial-recover
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+353
−29
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b06173d
feat: implement protocol recovery and connection handling in serial t…
sansmoraxz 81d1a8b
Lint issue fix
sansmoraxz 8fb2118
feat: add PTY tests for RTUSerialTransporter functionality
sansmoraxz ea9f466
feat: add PTY tests for ASCIISerialTransporter functionality
sansmoraxz c4c7d0d
Merge branch 'master' into feat/serial-recover
sansmoraxz 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| //go:build darwin || linux || freebsd || openbsd || netbsd | ||
| // +build darwin linux freebsd openbsd netbsd | ||
|
|
||
| // Copyright 2014 Quoc-Viet Nguyen. All rights reserved. | ||
| // This software may be modified and distributed under the terms | ||
| // of the BSD license. See the LICENSE file for details. | ||
|
|
||
| package modbus | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestASCIISerialTransporter_Send_PTY(t *testing.T) { | ||
| master, slavePath, err := openPTY() | ||
| if err != nil { | ||
| t.Skipf("Skipping PTY test: %v", err) | ||
| } | ||
| defer master.Close() | ||
|
|
||
| // Request: 01 03 00 00 00 01 (Read Holding Registers) | ||
| // ASCII: :010300000001FB\r\n | ||
| reqASCII := []byte(":010300000001FB\r\n") | ||
|
|
||
| // Response: 01 03 02 00 00 | ||
| // ASCII: :0103020000FA\r\n | ||
| respASCII := []byte(":0103020000FA\r\n") | ||
|
|
||
| transporter := &asciiSerialTransporter{} | ||
| transporter.Address = slavePath | ||
| transporter.BaudRate = 19200 | ||
| transporter.Timeout = 1 * time.Second | ||
| transporter.IdleTimeout = serialIdleTimeout | ||
|
|
||
| // Start a goroutine to read request and write response to master | ||
| go func() { | ||
| buf := make([]byte, 1024) | ||
| n, err := master.Read(buf) | ||
| if err != nil { | ||
| return | ||
| } | ||
| if !bytes.Equal(buf[:n], reqASCII) { | ||
| // t.Errorf would be racy here, just log or ignore | ||
| return | ||
| } | ||
| // Write response | ||
| _, err = master.Write(respASCII) | ||
| if err != nil { | ||
| t.Errorf("Failed to write response: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| ctx := context.Background() | ||
| aduResponse, err := transporter.Send(ctx, reqASCII) | ||
| if err != nil { | ||
| t.Fatalf("Send failed: %v", err) | ||
| } | ||
|
|
||
| if !bytes.Equal(aduResponse, respASCII) { | ||
| t.Errorf("Expected response %s, got %s", respASCII, aduResponse) | ||
| } | ||
| } | ||
|
|
||
| func TestASCIISerialTransporter_Timeout_PTY(t *testing.T) { | ||
| master, slavePath, err := openPTY() | ||
| if err != nil { | ||
| t.Skipf("Skipping PTY test: %v", err) | ||
| } | ||
| defer master.Close() | ||
|
|
||
| reqASCII := []byte(":010300000001FB\r\n") | ||
|
|
||
| transporter := &asciiSerialTransporter{} | ||
| transporter.Address = slavePath | ||
| transporter.BaudRate = 19200 | ||
| transporter.Timeout = 100 * time.Millisecond | ||
| transporter.IdleTimeout = serialIdleTimeout | ||
|
|
||
| // Don't write anything to master | ||
|
|
||
| ctx := context.Background() | ||
| _, err = transporter.Send(ctx, reqASCII) | ||
| if err == nil { | ||
| t.Fatal("Expected timeout error, got nil") | ||
| } | ||
| } |
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,117 @@ | ||
| //go:build darwin || linux || freebsd || openbsd || netbsd | ||
| // +build darwin linux freebsd openbsd netbsd | ||
|
|
||
| // Copyright 2014 Quoc-Viet Nguyen. All rights reserved. | ||
| // This software may be modified and distributed under the terms | ||
| // of the BSD license. See the LICENSE file for details. | ||
|
|
||
| package modbus | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| // openPTY opens a PTY pair and returns the master file and the slave path. | ||
| func openPTY() (master *os.File, slavePath string, err error) { | ||
| master, err = os.OpenFile("/dev/ptmx", os.O_RDWR, 0) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
||
| // unlockpt | ||
| var unlock int32 | ||
| // TIOCSPTLCK | ||
| _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, master.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&unlock))) | ||
| if errno != 0 { | ||
| master.Close() | ||
| return nil, "", errno | ||
| } | ||
|
|
||
| // ptsname | ||
| var ptyno int32 | ||
| // TIOCGPTN | ||
| _, _, errno = syscall.Syscall(syscall.SYS_IOCTL, master.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&ptyno))) | ||
| if errno != 0 { | ||
| master.Close() | ||
| return nil, "", errno | ||
| } | ||
|
|
||
| slavePath = fmt.Sprintf("/dev/pts/%d", ptyno) | ||
| return master, slavePath, nil | ||
| } | ||
|
|
||
| func TestRTUSerialTransporter_Send_PTY(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice to see PTY-based tests! |
||
| master, slavePath, err := openPTY() | ||
| if err != nil { | ||
| t.Skipf("Skipping PTY test: %v", err) | ||
| } | ||
| defer master.Close() | ||
|
|
||
| // Request: 01 03 00 00 00 01 84 0A (Read Holding Registers) | ||
| // Response: 01 03 02 00 00 B8 44 | ||
| req := []byte{0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A} | ||
| resp := []byte{0x01, 0x03, 0x02, 0x00, 0x00, 0xB8, 0x44} | ||
|
|
||
| transporter := &rtuSerialTransporter{} | ||
| transporter.Address = slavePath | ||
| transporter.BaudRate = 19200 | ||
| transporter.Timeout = 1 * time.Second | ||
|
|
||
| // Start a goroutine to read request and write response to master | ||
| go func() { | ||
| buf := make([]byte, 1024) | ||
| n, err := master.Read(buf) | ||
| if err != nil { | ||
| return | ||
| } | ||
| if !bytes.Equal(buf[:n], req) { | ||
| // t.Errorf would be racy here, just log or ignore | ||
| return | ||
| } | ||
| // Write response | ||
| _, err = master.Write(resp) | ||
| if err != nil { | ||
| t.Errorf("Failed to write response: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| ctx := context.Background() | ||
| aduResponse, err := transporter.Send(ctx, req) | ||
| if err != nil { | ||
| t.Fatalf("Send failed: %v", err) | ||
| } | ||
|
|
||
| if !bytes.Equal(aduResponse, resp) { | ||
| t.Errorf("Expected response %x, got %x", resp, aduResponse) | ||
| } | ||
| } | ||
|
|
||
| func TestRTUSerialTransporter_Timeout_PTY(t *testing.T) { | ||
| master, slavePath, err := openPTY() | ||
| if err != nil { | ||
| t.Skipf("Skipping PTY test: %v", err) | ||
| } | ||
| defer master.Close() | ||
|
|
||
| req := []byte{0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A} | ||
|
|
||
| transporter := &rtuSerialTransporter{} | ||
| transporter.Address = slavePath | ||
| transporter.BaudRate = 19200 | ||
| transporter.Timeout = 100 * time.Millisecond | ||
|
|
||
| // Don't write anything to master | ||
|
|
||
| ctx := context.Background() | ||
| _, err = transporter.Send(ctx, req) | ||
| if err == nil { | ||
| t.Fatal("Expected timeout error, got nil") | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The build tags include darwin but
openPTY()usessyscall.TIOCSPTLCKandsyscall.TIOCGPTN, which are Linux-only. This won't compile on macOS. Should we either restrict the tag to linux or use something likegithub.com/creack/ptyfor cross-platform PTY support?