diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml new file mode 100644 index 0000000..b544840 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report.yml @@ -0,0 +1,78 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/issue-templates/forms/platform-dependent/bug-report.yml +# See: https://docs.github.com/communities/using-templates-to-encourage-useful-issues-and-pull-requests/syntax-for-issue-forms + +name: Bug report +description: Report a problem with the code or documentation in this repository. +labels: + - bug +body: + - type: textarea + id: description + attributes: + label: Describe the problem + validations: + required: true + - type: textarea + id: reproduce + attributes: + label: To reproduce + description: | + Provide the specific set of steps we can follow to reproduce the + problem in particular the exact golang source code you used. + validations: + required: true + - type: checkboxes + id: checklist-reproduce + attributes: + label: | + Please double-check that you have reported each of the following + before submitting the issue. + options: + - label: I've provided the FULL source code that causes the problem + required: true + - label: I've provided all the actions required to reproduce the problem + required: true + - type: textarea + id: expected + attributes: + label: Expected behavior + description: | + What would you expect to happen after following those instructions? + validations: + required: true + - type: input + id: os + attributes: + label: Operating system and version + description: | + Which operating system(s) version are you using on your computer? + validations: + required: true + - type: textarea + id: boards + attributes: + label: Please describe your hardware setup + description: | + Arduino boards, USB dongles, hubs or embedded devices you are using and how they + are connected together. + - type: textarea + id: additional + attributes: + label: Additional context + description: | + Add here any additional information that you think might be relevant to + the problem. + validations: + required: false + - type: checkboxes + id: checklist + attributes: + label: Issue checklist + description: | + Please double-check that you have done each of the following things before + submitting the issue. + options: + - label: I searched for previous requests in [the issue tracker](https://github.com/bugst/go-serial/issues) + required: true + - label: My request contains all necessary details + required: true diff --git a/.github/ISSUE_TEMPLATE/feature-request.yml b/.github/ISSUE_TEMPLATE/feature-request.yml new file mode 100644 index 0000000..d56393d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature-request.yml @@ -0,0 +1,29 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/issue-templates/forms/platform-dependent/feature-request.yml +# See: https://docs.github.com/communities/using-templates-to-encourage-useful-issues-and-pull-requests/syntax-for-issue-forms + +name: Feature request +description: Suggest an enhancement to this project. +labels: + - "type: enhancement" +body: + - type: textarea + id: description + attributes: + label: Describe the new feature or change suggestion + validations: + required: true + - type: textarea + id: additional + attributes: + label: Additional context + description: Add any additional information about the feature request here. + - type: checkboxes + id: checklist + attributes: + label: Issue checklist + description: Please double-check that you have done each of the following things before submitting the issue. + options: + - label: I searched for previous requests in [the issue tracker](https://github.com/bugst/go-serial/issues) + required: true + - label: My request contains all necessary details + required: true diff --git a/LICENSE b/LICENSE index e34cbff..5aca55c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ -Copyright (c) 2014-2023, Cristian Maglie. +Copyright (c) 2014-2024, Cristian Maglie. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/doc.go b/doc.go index 6e230ff..360f0cc 100644 --- a/doc.go +++ b/doc.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator/doc.go b/enumerator/doc.go index a3f887d..5d27f00 100644 --- a/enumerator/doc.go +++ b/enumerator/doc.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator/enumerator.go b/enumerator/enumerator.go index 420f451..2bd5043 100644 --- a/enumerator/enumerator.go +++ b/enumerator/enumerator.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator/example_getdetailedportlist_test.go b/enumerator/example_getdetailedportlist_test.go index d2796fb..6d3c2db 100644 --- a/enumerator/example_getdetailedportlist_test.go +++ b/enumerator/example_getdetailedportlist_test.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator/usb_darwin.go b/enumerator/usb_darwin.go index d9adb1b..1f51c47 100644 --- a/enumerator/usb_darwin.go +++ b/enumerator/usb_darwin.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator/usb_freebsd.go b/enumerator/usb_freebsd.go index db5d96c..553c5a0 100644 --- a/enumerator/usb_freebsd.go +++ b/enumerator/usb_freebsd.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator/usb_linux.go b/enumerator/usb_linux.go index f756367..2e3737e 100644 --- a/enumerator/usb_linux.go +++ b/enumerator/usb_linux.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator/usb_openbsd.go b/enumerator/usb_openbsd.go index db5d96c..553c5a0 100644 --- a/enumerator/usb_openbsd.go +++ b/enumerator/usb_openbsd.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator/usb_wasm.go b/enumerator/usb_wasm.go new file mode 100644 index 0000000..3f3b22f --- /dev/null +++ b/enumerator/usb_wasm.go @@ -0,0 +1,11 @@ +// +// Copyright 2014-2024 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package enumerator + +func nativeGetDetailedPortsList() ([]*PortDetails, error) { + return nil, &PortEnumerationError{} +} diff --git a/enumerator/usb_windows.go b/enumerator/usb_windows.go index 7883e45..c8018f9 100644 --- a/enumerator/usb_windows.go +++ b/enumerator/usb_windows.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator/usb_windows_test.go b/enumerator/usb_windows_test.go index 3160ef3..3d39e0a 100644 --- a/enumerator/usb_windows_test.go +++ b/enumerator/usb_windows_test.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/enumerator_wasm.go b/enumerator_wasm.go new file mode 100644 index 0000000..349d272 --- /dev/null +++ b/enumerator_wasm.go @@ -0,0 +1,15 @@ +// +// Copyright 2014-2024 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package serial + +import ( + "errors" +) + +func nativeGetPortsList() ([]string, error) { + return nil, errors.New("nativeGetPortsList is not supported on wasm") +} diff --git a/example_getportlist_test.go b/example_getportlist_test.go index beb4773..a1756ac 100644 --- a/example_getportlist_test.go +++ b/example_getportlist_test.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/example_modem_bits_test.go b/example_modem_bits_test.go index 34b212f..cc6969d 100644 --- a/example_modem_bits_test.go +++ b/example_modem_bits_test.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/example_serialport_test.go b/example_serialport_test.go index 12f68b5..b56fd7f 100644 --- a/example_serialport_test.go +++ b/example_serialport_test.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/example_test.go b/example_test.go index 27430d0..7ad5eef 100644 --- a/example_test.go +++ b/example_test.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/go.mod b/go.mod index c3abe90..61d0d6d 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,12 @@ go 1.17 require ( github.com/creack/goselect v0.1.2 - github.com/stretchr/testify v1.7.0 - golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 + github.com/stretchr/testify v1.8.4 + golang.org/x/sys v0.17.0 ) require ( - github.com/davecgh/go-spew v1.1.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index dac8e08..43d4dac 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,21 @@ github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0= github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k= -golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY= -golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/portlist/portlist.go b/portlist/portlist.go index 8c36e5c..845a6c9 100644 --- a/portlist/portlist.go +++ b/portlist/portlist.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/serial.go b/serial.go index e06c698..f799a23 100644 --- a/serial.go +++ b/serial.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/serial_bsd.go b/serial_bsd.go index fb7c849..0368d18 100644 --- a/serial_bsd.go +++ b/serial_bsd.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/serial_darwin.go b/serial_darwin.go index 2817041..a87458e 100644 --- a/serial_darwin.go +++ b/serial_darwin.go @@ -1,15 +1,20 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // package serial -import "golang.org/x/sys/unix" +import ( + "regexp" + + "golang.org/x/sys/unix" +) const devFolder = "/dev" -const regexFilter = "^(cu|tty)\\..*" + +var osPortFilter = regexp.MustCompile("^(cu|tty)\\..*") const ioctlTcgetattr = unix.TIOCGETA const ioctlTcsetattr = unix.TIOCSETA diff --git a/serial_darwin_386.go b/serial_darwin_386.go index 3594a6c..f3842b5 100644 --- a/serial_darwin_386.go +++ b/serial_darwin_386.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/serial_darwin_64.go b/serial_darwin_64.go index d1ae7c4..c0ed76c 100644 --- a/serial_darwin_64.go +++ b/serial_darwin_64.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/serial_freebsd.go b/serial_freebsd.go index aceb08a..775b7a6 100644 --- a/serial_freebsd.go +++ b/serial_freebsd.go @@ -1,15 +1,20 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // package serial -import "golang.org/x/sys/unix" +import ( + "regexp" + + "golang.org/x/sys/unix" +) const devFolder = "/dev" -const regexFilter = "^(cu|tty)\\..*" + +var osPortFilter = regexp.MustCompile("^(cu|tty)\\..*") // termios manipulation functions diff --git a/serial_linux.go b/serial_linux.go index fe114d9..a1218bb 100644 --- a/serial_linux.go +++ b/serial_linux.go @@ -1,15 +1,20 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // package serial -import "golang.org/x/sys/unix" +import ( + "regexp" + + "golang.org/x/sys/unix" +) const devFolder = "/dev" -const regexFilter = "(ttyS|ttyHS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO|ttymxc)[0-9]{1,3}" + +var osPortFilter = regexp.MustCompile("(ttyS|ttyHS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO|ttymxc)[0-9]{1,3}") // termios manipulation functions diff --git a/serial_linux_test.go b/serial_linux_test.go index 1a7e1a3..4a76fbe 100644 --- a/serial_linux_test.go +++ b/serial_linux_test.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/serial_openbsd.go b/serial_openbsd.go index 644dc92..ad5efd0 100644 --- a/serial_openbsd.go +++ b/serial_openbsd.go @@ -1,15 +1,20 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // package serial -import "golang.org/x/sys/unix" +import ( + "regexp" + + "golang.org/x/sys/unix" +) const devFolder = "/dev" -const regexFilter = "^(cu|tty)\\..*" + +var osPortFilter = regexp.MustCompile("^(cu|tty)\\..*") // termios manipulation functions diff --git a/serial_resetbuf_linux_bsd.go b/serial_resetbuf_linux_bsd.go index 105669d..9e24604 100644 --- a/serial_resetbuf_linux_bsd.go +++ b/serial_resetbuf_linux_bsd.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/serial_specialbaudrate_linux.go b/serial_specialbaudrate_linux.go index ff48974..4e55fdb 100644 --- a/serial_specialbaudrate_linux.go +++ b/serial_specialbaudrate_linux.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/serial_specialbaudrate_linux_ppc64le.go b/serial_specialbaudrate_linux_ppc64le.go index cd80b86..b31d6c5 100644 --- a/serial_specialbaudrate_linux_ppc64le.go +++ b/serial_specialbaudrate_linux_ppc64le.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/serial_unix.go b/serial_unix.go index 7b7304b..f7457c1 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // @@ -9,8 +9,8 @@ package serial import ( - "io/ioutil" - "regexp" + "fmt" + "os" "strings" "sync" "sync/atomic" @@ -249,7 +249,7 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) { settings, err := port.getTermSettings() if err != nil { port.Close() - return nil, &PortError{code: InvalidSerialPort} + return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error getting term settings: %w", err)} } // Set raw mode @@ -260,13 +260,14 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) { if port.setTermSettings(settings) != nil { port.Close() - return nil, &PortError{code: InvalidSerialPort} + return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error setting term settings: %w", err)} } if mode.InitialStatusBits != nil { status, err := port.getModemBitsStatus() if err != nil { - return nil, &PortError{code: InvalidSerialPort, causedBy: err} + port.Close() + return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error getting modem bits status: %w", err)} } if mode.InitialStatusBits.DTR { status |= unix.TIOCM_DTR @@ -279,15 +280,16 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) { status &^= unix.TIOCM_RTS } if err := port.setModemBitsStatus(status); err != nil { - return nil, &PortError{code: InvalidSerialPort, causedBy: err} + port.Close() + return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error setting modem bits status: %w", err)} } } // MacOSX require that this operation is the last one otherwise an // 'Invalid serial port' error is returned... don't know why... - if port.SetMode(mode) != nil { + if err := port.SetMode(mode); err != nil { port.Close() - return nil, &PortError{code: InvalidSerialPort} + return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error configuring port: %w", err)} } unix.SetNonblock(h, false) @@ -304,7 +306,7 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) { pipe := &unixutils.Pipe{} if err := pipe.Open(); err != nil { port.Close() - return nil, &PortError{code: InvalidSerialPort, causedBy: err} + return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error opening signaling pipe: %w", err)} } port.closeSignal = pipe @@ -312,16 +314,12 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) { } func nativeGetPortsList() ([]string, error) { - files, err := ioutil.ReadDir(devFolder) + files, err := os.ReadDir(devFolder) if err != nil { return nil, err } ports := make([]string, 0, len(files)) - regex, err := regexp.Compile(regexFilter) - if err != nil { - return nil, err - } for _, f := range files { // Skip folders if f.IsDir() { @@ -329,7 +327,7 @@ func nativeGetPortsList() ([]string, error) { } // Keep only devices with the correct name - if !regex.MatchString(f.Name()) { + if !osPortFilter.MatchString(f.Name()) { continue } diff --git a/serial_wasm.go b/serial_wasm.go new file mode 100644 index 0000000..b2369f1 --- /dev/null +++ b/serial_wasm.go @@ -0,0 +1,15 @@ +// +// Copyright 2014-2024 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package serial + +import ( + "errors" +) + +func nativeOpen(portName string, mode *Mode) (Port, error) { + return nil, errors.New("nativeOpen is not supported on wasm") +} diff --git a/serial_windows.go b/serial_windows.go index 2e8a4cc..0aca838 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // @@ -254,6 +254,15 @@ func (port *windowsPort) SetMode(mode *Mode) error { port.Close() return &PortError{code: InvalidSerialPort} } + port.setModeParams(mode, ¶ms) + if setCommState(port.handle, ¶ms) != nil { + port.Close() + return &PortError{code: InvalidSerialPort} + } + return nil +} + +func (port *windowsPort) setModeParams(mode *Mode, params *dcb) { if mode.BaudRate == 0 { params.BaudRate = 9600 // Default to 9600 } else { @@ -266,11 +275,6 @@ func (port *windowsPort) SetMode(mode *Mode) error { } params.StopBits = stopBitsMap[mode.StopBits] params.Parity = parityMap[mode.Parity] - if setCommState(port.handle, ¶ms) != nil { - port.Close() - return &PortError{code: InvalidSerialPort} - } - return nil } func (port *windowsPort) SetDTR(dtr bool) error { @@ -432,16 +436,12 @@ func nativeOpen(portName string, mode *Mode) (*windowsPort, error) { } // Set port parameters - if port.SetMode(mode) != nil { - port.Close() - return nil, &PortError{code: InvalidSerialPort} - } - params := &dcb{} if getCommState(port.handle, params) != nil { port.Close() return nil, &PortError{code: InvalidSerialPort} } + port.setModeParams(mode, params) params.Flags &= dcbDTRControlDisableMask params.Flags &= dcbRTSControlDisbaleMask if mode.InitialStatusBits == nil { diff --git a/syscall_windows.go b/syscall_windows.go index c814528..6a95dcf 100644 --- a/syscall_windows.go +++ b/syscall_windows.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/unixutils/pipe.go b/unixutils/pipe.go index 748de34..c11e0d1 100644 --- a/unixutils/pipe.go +++ b/unixutils/pipe.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // diff --git a/unixutils/select.go b/unixutils/select.go index 9f0e214..42cacc3 100644 --- a/unixutils/select.go +++ b/unixutils/select.go @@ -1,5 +1,5 @@ // -// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Copyright 2014-2024 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. //