Rename serial.SerialPort to serial.Port
another lint suggestion.
This commit is contained in:
@@ -14,13 +14,13 @@ import "strings"
|
|||||||
import "syscall"
|
import "syscall"
|
||||||
import "unsafe"
|
import "unsafe"
|
||||||
|
|
||||||
// Opaque type that implements SerialPort interface for linux
|
// Port is the handler for a serial Port
|
||||||
type SerialPort struct {
|
type Port struct {
|
||||||
handle int
|
handle int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the serial port
|
// Close the serial port
|
||||||
func (port *SerialPort) Close() error {
|
func (port *Port) Close() error {
|
||||||
port.releaseExclusiveAccess()
|
port.releaseExclusiveAccess()
|
||||||
return syscall.Close(port.handle)
|
return syscall.Close(port.handle)
|
||||||
}
|
}
|
||||||
@@ -30,18 +30,18 @@ func (port *SerialPort) Close() error {
|
|||||||
//
|
//
|
||||||
// The Read function blocks until (at least) one byte is received from
|
// The Read function blocks until (at least) one byte is received from
|
||||||
// the serial port or an error occurs.
|
// the serial port or an error occurs.
|
||||||
func (port *SerialPort) Read(p []byte) (n int, err error) {
|
func (port *Port) Read(p []byte) (n int, err error) {
|
||||||
return syscall.Read(port.handle, p)
|
return syscall.Read(port.handle, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the content of the data byte array to the serial port.
|
// Send the content of the data byte array to the serial port.
|
||||||
// Returns the number of bytes written.
|
// Returns the number of bytes written.
|
||||||
func (port *SerialPort) Write(p []byte) (n int, err error) {
|
func (port *Port) Write(p []byte) (n int, err error) {
|
||||||
return syscall.Write(port.handle, p)
|
return syscall.Write(port.handle, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMode sets all parameters of the serial port
|
// SetMode sets all parameters of the serial port
|
||||||
func (port *SerialPort) SetMode(mode *Mode) error {
|
func (port *Port) SetMode(mode *Mode) error {
|
||||||
settings, err := port.getTermSettings()
|
settings, err := port.getTermSettings()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -62,7 +62,7 @@ func (port *SerialPort) SetMode(mode *Mode) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OpenPort opens the serial port using the specified modes
|
// OpenPort opens the serial port using the specified modes
|
||||||
func OpenPort(portName string, mode *Mode) (*SerialPort, error) {
|
func OpenPort(portName string, mode *Mode) (*Port, error) {
|
||||||
h, err := syscall.Open(portName, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NDELAY, 0)
|
h, err := syscall.Open(portName, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NDELAY, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err {
|
switch err {
|
||||||
@@ -73,7 +73,7 @@ func OpenPort(portName string, mode *Mode) (*SerialPort, error) {
|
|||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
port := &SerialPort{
|
port := &Port{
|
||||||
handle: h,
|
handle: h,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,20 +230,20 @@ func setRawMode(settings *syscall.Termios) {
|
|||||||
|
|
||||||
// native syscall wrapper functions
|
// native syscall wrapper functions
|
||||||
|
|
||||||
func (port *SerialPort) getTermSettings() (*syscall.Termios, error) {
|
func (port *Port) getTermSettings() (*syscall.Termios, error) {
|
||||||
settings := &syscall.Termios{}
|
settings := &syscall.Termios{}
|
||||||
err := ioctl(port.handle, ioctlTcgetattr, uintptr(unsafe.Pointer(settings)))
|
err := ioctl(port.handle, ioctlTcgetattr, uintptr(unsafe.Pointer(settings)))
|
||||||
return settings, err
|
return settings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (port *SerialPort) setTermSettings(settings *syscall.Termios) error {
|
func (port *Port) setTermSettings(settings *syscall.Termios) error {
|
||||||
return ioctl(port.handle, ioctlTcsetattr, uintptr(unsafe.Pointer(settings)))
|
return ioctl(port.handle, ioctlTcsetattr, uintptr(unsafe.Pointer(settings)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (port *SerialPort) acquireExclusiveAccess() error {
|
func (port *Port) acquireExclusiveAccess() error {
|
||||||
return ioctl(port.handle, syscall.TIOCEXCL, 0)
|
return ioctl(port.handle, syscall.TIOCEXCL, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (port *SerialPort) releaseExclusiveAccess() error {
|
func (port *Port) releaseExclusiveAccess() error {
|
||||||
return ioctl(port.handle, syscall.TIOCNXCL, 0)
|
return ioctl(port.handle, syscall.TIOCNXCL, 0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,7 @@ package serial // import "go.bug.st/serial"
|
|||||||
|
|
||||||
import "syscall"
|
import "syscall"
|
||||||
|
|
||||||
// opaque type that implements SerialPort interface for Windows
|
type Port struct {
|
||||||
type SerialPort struct {
|
|
||||||
handle syscall.Handle
|
handle syscall.Handle
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,11 +55,11 @@ func GetPortsList() ([]string, error) {
|
|||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (port *SerialPort) Close() error {
|
func (port *Port) Close() error {
|
||||||
return syscall.CloseHandle(port.handle)
|
return syscall.CloseHandle(port.handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (port *SerialPort) Read(p []byte) (int, error) {
|
func (port *Port) Read(p []byte) (int, error) {
|
||||||
var readed uint32
|
var readed uint32
|
||||||
params := &DCB{}
|
params := &DCB{}
|
||||||
for {
|
for {
|
||||||
@@ -83,7 +82,7 @@ func (port *SerialPort) Read(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (port *SerialPort) Write(p []byte) (int, error) {
|
func (port *Port) Write(p []byte) (int, error) {
|
||||||
var writed uint32
|
var writed uint32
|
||||||
err := syscall.WriteFile(port.handle, p, &writed, nil)
|
err := syscall.WriteFile(port.handle, p, &writed, nil)
|
||||||
return int(writed), err
|
return int(writed), err
|
||||||
@@ -171,7 +170,7 @@ const (
|
|||||||
TWOSTOPBITS = 2
|
TWOSTOPBITS = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
func (port *SerialPort) SetMode(mode *Mode) error {
|
func (port *Port) SetMode(mode *Mode) error {
|
||||||
params := DCB{}
|
params := DCB{}
|
||||||
if GetCommState(port.handle, ¶ms) != nil {
|
if GetCommState(port.handle, ¶ms) != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
@@ -196,7 +195,7 @@ func (port *SerialPort) SetMode(mode *Mode) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func OpenPort(portName string, mode *Mode) (*SerialPort, error) {
|
func OpenPort(portName string, mode *Mode) (*Port, error) {
|
||||||
portName = "\\\\.\\" + portName
|
portName = "\\\\.\\" + portName
|
||||||
path, err := syscall.UTF16PtrFromString(portName)
|
path, err := syscall.UTF16PtrFromString(portName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -219,7 +218,7 @@ func OpenPort(portName string, mode *Mode) (*SerialPort, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Create the serial port
|
// Create the serial port
|
||||||
port := &SerialPort{
|
port := &Port{
|
||||||
handle: handle,
|
handle: handle,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user