Skip to content
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

windows: setting buffer sizes with SetupComm #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ type Port interface {
// to disable read timeout.
SetReadTimeout(t time.Duration) error

// SetBufferSize sets internal buffer sizes for the Read and Write operations.
// Please note that this function may not be available on all OS:
// in that case a FunctionNotImplemented error is returned.
SetBufferSize(txSize, rxSize int) error

// Close the serial port
Close() error

Expand Down Expand Up @@ -159,6 +164,8 @@ const (
InvalidStopBits
// InvalidTimeoutValue the timeout value is not valid or not supported
InvalidTimeoutValue
// InvalidBufferSize the requested buffer size is not valid or not supported
InvalidBufferSize
// ErrorEnumeratingPorts an error occurred while listing serial port
ErrorEnumeratingPorts
// PortClosed the port has been closed while the operation is in progress
Expand Down Expand Up @@ -188,6 +195,8 @@ func (e PortError) EncodedErrorString() string {
return "Port stop bits invalid or not supported"
case InvalidTimeoutValue:
return "Timeout value invalid or not supported"
case InvalidBufferSize:
return "Buffer size value invalid or not supported"
case ErrorEnumeratingPorts:
return "Could not enumerate serial ports"
case PortClosed:
Expand Down
6 changes: 6 additions & 0 deletions serial_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ func (port *unixPort) SetReadTimeout(timeout time.Duration) error {
return nil
}

func (port *unixPort) SetBufferSize(rxSize, txSize int) error {
// Unix does not provide any means to change the buffer size from 4096 bytes.
// The only reported way to do so is the kernel recompilation.
return &PortError{code: FunctionNotImplemented, causedBy: nil}
}

func (port *unixPort) GetModemStatusBits() (*ModemStatusBits, error) {
status, err := port.getModemBitsStatus()
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,17 @@ func (port *windowsPort) SetReadTimeout(timeout time.Duration) error {
return nil
}

func (port *windowsPort) SetBufferSize(rxSize, txSize int) error {
rxSize_ := uint32(rxSize)
txSize_ := uint32(txSize)

if err := setupComm(port.handle, rxSize_, txSize_); err != nil {
return &PortError{code: InvalidBufferSize, causedBy: err}
}

return nil
}

func (port *windowsPort) Break(d time.Duration) error {
if err := setCommBreak(port.handle); err != nil {
return &PortError{causedBy: err}
Expand Down
2 changes: 2 additions & 0 deletions syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ package serial

//sys setCommState(handle syscall.Handle, dcb *dcb) (err error) = SetCommState

//sys setupComm(handle syscall.Handle, rxSize uint32, txSize uint32) (err error) = SetupComm

//sys setCommTimeouts(handle syscall.Handle, timeouts *commTimeouts) (err error) = SetCommTimeouts

//sys escapeCommFunction(handle syscall.Handle, function uint32) (res bool) = EscapeCommFunction
Expand Down
9 changes: 9 additions & 0 deletions zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.