-
Notifications
You must be signed in to change notification settings - Fork 453
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
Added port list functionality and increased range of baud rates available via serial_posix.go #37
Open
cormacc
wants to merge
2
commits into
tarm:master
Choose a base branch
from
cormacc:port_list
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.
Open
Changes from all commits
Commits
Show all changes
2 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 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,119 @@ | ||
// +build !windows | ||
|
||
package serial | ||
|
||
import ( | ||
"io/ioutil" | ||
"regexp" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const ( | ||
devFolder = "/dev" | ||
) | ||
|
||
|
||
// Converts the timeout values for Linux / POSIX systems | ||
// Moved to this new source module from serial.go | ||
func posixTimeoutValues(readTimeout time.Duration) (vmin uint8, vtime uint8) { | ||
const MAXUINT8 = 1<<8 - 1 // 255 | ||
// set blocking / non-blocking read | ||
var minBytesToRead uint8 = 1 | ||
var readTimeoutInDeci int64 | ||
if readTimeout > 0 { | ||
// EOF on zero read | ||
minBytesToRead = 0 | ||
// convert timeout to deciseconds as expected by VTIME | ||
readTimeoutInDeci = (readTimeout.Nanoseconds() / 1e6 / 100) | ||
// capping the timeout | ||
if readTimeoutInDeci < 1 { | ||
// min possible timeout 1 Deciseconds (0.1s) | ||
readTimeoutInDeci = 1 | ||
} else if readTimeoutInDeci > MAXUINT8 { | ||
// max possible timeout is 255 deciseconds (25.5s) | ||
readTimeoutInDeci = MAXUINT8 | ||
} | ||
} | ||
return minBytesToRead, uint8(readTimeoutInDeci) | ||
} | ||
|
||
/* | ||
This function was taken with minor modifications from the go.bug.st/serial package (https://github.com/bugst/go-serial), and is subject to the conditions of its license (reproduced below): | ||
|
||
Copyright (c) 2014, Cristian Maglie. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
|
||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in | ||
the documentation and/or other materials provided with the | ||
distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
func listPorts() ([]string, error) { | ||
files, err := ioutil.ReadDir(devFolder) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ports := make([]string, 0, len(files)) | ||
for _, f := range files { | ||
// Skip folders | ||
if f.IsDir() { | ||
continue | ||
} | ||
|
||
// Keep only devices with name matching the port name filter defined for this platform | ||
// (discarding placeholder entries for non-existent onboard COM ports) | ||
match, err := regexp.MatchString(portNameFilter, f.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !match || isALegacyPlaceholder(f.Name()) { | ||
continue | ||
} | ||
|
||
// Save serial port in the resulting list | ||
ports = append(ports, devFolder + "/" + f.Name()) | ||
} | ||
|
||
return ports, nil | ||
} | ||
|
||
// Checks whether port entry is just a placeholder -- e.g. reserved for a legacy ISA COM | ||
// port that doesn't exist | ||
func isALegacyPlaceholder(portName string) (bool){ | ||
const legacyComPortPrefix = "ttyS" | ||
if strings.HasPrefix(portName, legacyComPortPrefix) { | ||
port, err := openPort(devFolder + "/" + portName, 9600, 100) | ||
if err != nil { | ||
return true; | ||
} else { | ||
port.Close() | ||
} | ||
} | ||
return false; | ||
} |
This file contains 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,15 @@ | ||
//+build darwin | ||
|
||
package serial | ||
|
||
// | ||
// Apparently OSX creates a CU (Call-Up) and a tty device entry for | ||
// each attached serial device, prefixing them with 'cu.' and 'tty.' | ||
// respectively | ||
// Should maybe restrict filter to cu devices? | ||
// (see http://pbxbook.com/other/mac-tty.html) | ||
// Although linux has dispensed with / deprecated this distinction: | ||
// http://tldp.org/HOWTO/Modem-HOWTO-9.html#ss9.8 | ||
const ( | ||
portNameFilter = `^(cu|tty)\..*` | ||
) |
This file contains 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,15 @@ | ||
//+build linux | ||
|
||
package serial | ||
|
||
/* | ||
This heuristic regex filter should catch most serial devices under linux. | ||
The prefixes represent devices of the following types | ||
ttyS: onboard uarts | ||
ttyUSB: USB<->uart bridges | ||
ttyACM: Abstract Control Model devices (e.g. modems -- see https://www.rfc1149.net/blog/2013/03/05/what-is-the-difference-between-devttyusbx-and-devttyacmx/) | ||
ttyAMA: Don't know what AMA stands for, but seems to be used for Raspberry PI onboard ports at least | ||
*/ | ||
const ( | ||
portNameFilter = `^(ttyS|ttyUSB|ttyACM|ttyAMA)\d+` | ||
) |
This file contains 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,10 @@ | ||
// +build !linux,!darwin | ||
|
||
package serial | ||
|
||
/* | ||
This is a catchall for posix platforms other than linux and OS X | ||
*/ | ||
const ( | ||
portNameFilter = `^tty.*` | ||
) |
This file contains 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 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 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
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.
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.
This is almost the standard Go License, which tarm/serial is already under. The Go license is a 3 clause BSD license like this one Copyright the Go Authors. I would like to keep the existing license text and not add this.
Unfortunately @cmaglie is not in the standard Go AUTHORS file right now:
https://golang.org/AUTHORS