Another round of lint on Windows implementation
This commit is contained in:
@@ -22,7 +22,7 @@ type windowsPort struct {
|
|||||||
handle syscall.Handle
|
handle syscall.Handle
|
||||||
}
|
}
|
||||||
|
|
||||||
//sys RegEnumValue(key syscall.Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, value *uint16, valueLen *uint32) (regerrno error) = advapi32.RegEnumValueW
|
//sys regEnumValue(key syscall.Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, value *uint16, valueLen *uint32) (regerrno error) = advapi32.RegEnumValueW
|
||||||
|
|
||||||
func nativeGetPortsList() ([]string, error) {
|
func nativeGetPortsList() ([]string, error) {
|
||||||
subKey, err := syscall.UTF16PtrFromString("HARDWARE\\DEVICEMAP\\SERIALCOMM\\")
|
subKey, err := syscall.UTF16PtrFromString("HARDWARE\\DEVICEMAP\\SERIALCOMM\\")
|
||||||
@@ -47,7 +47,7 @@ func nativeGetPortsList() ([]string, error) {
|
|||||||
dataSize := uint32(len(data))
|
dataSize := uint32(len(data))
|
||||||
var name [1024]uint16
|
var name [1024]uint16
|
||||||
nameSize := uint32(len(name))
|
nameSize := uint32(len(name))
|
||||||
if RegEnumValue(h, uint32(i), &name[0], &nameSize, nil, nil, &data[0], &dataSize) != nil {
|
if regEnumValue(h, uint32(i), &name[0], &nameSize, nil, nil, &data[0], &dataSize) != nil {
|
||||||
return nil, &PortError{code: ErrorEnumeratingPorts}
|
return nil, &PortError{code: ErrorEnumeratingPorts}
|
||||||
}
|
}
|
||||||
list[i] = syscall.UTF16ToString(data[:])
|
list[i] = syscall.UTF16ToString(data[:])
|
||||||
@@ -61,7 +61,7 @@ func (port *windowsPort) Close() error {
|
|||||||
|
|
||||||
func (port *windowsPort) Read(p []byte) (int, error) {
|
func (port *windowsPort) Read(p []byte) (int, error) {
|
||||||
var readed uint32
|
var readed uint32
|
||||||
params := &DCB{}
|
params := &dcb{}
|
||||||
for {
|
for {
|
||||||
if err := syscall.ReadFile(port.handle, p, &readed, nil); err != nil {
|
if err := syscall.ReadFile(port.handle, p, &readed, nil); err != nil {
|
||||||
return int(readed), err
|
return int(readed), err
|
||||||
@@ -74,8 +74,8 @@ func (port *windowsPort) Read(p []byte) (int, error) {
|
|||||||
// a serial port is alive in Windows is to check if the SetCommState
|
// a serial port is alive in Windows is to check if the SetCommState
|
||||||
// function fails.
|
// function fails.
|
||||||
|
|
||||||
GetCommState(port.handle, params)
|
getCommState(port.handle, params)
|
||||||
if err := SetCommState(port.handle, params); err != nil {
|
if err := setCommState(port.handle, params); err != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -89,27 +89,27 @@ func (port *windowsPort) Write(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DCB_BINARY = 0x00000001
|
dcbBinary = 0x00000001
|
||||||
DCB_PARITY = 0x00000002
|
dcbParity = 0x00000002
|
||||||
DCB_OUT_X_CTS_FLOW = 0x00000004
|
dcbOutXCTSFlow = 0x00000004
|
||||||
DCB_OUT_X_DSR_FLOW = 0x00000008
|
dcbOutXDSRFlow = 0x00000008
|
||||||
DCB_DTR_CONTROL_DISABLE_MASK = ^0x00000030
|
dcbDTRControlDisableMask = ^0x00000030
|
||||||
DCB_DTR_CONTROL_ENABLE = 0x00000010
|
dcbDTRControlEnable = 0x00000010
|
||||||
DCB_DTR_CONTROL_HANDSHAKE = 0x00000020
|
dcbDTRControlHandshake = 0x00000020
|
||||||
DCB_DSR_SENSITIVITY = 0x00000040
|
dcbDSRSensitivity = 0x00000040
|
||||||
DCB_TX_CONTINUE_ON_XOFF = 0x00000080
|
dcbTXContinueOnXOFF = 0x00000080
|
||||||
DCB_OUT_X = 0x00000100
|
dcbOutX = 0x00000100
|
||||||
DCB_IN_X = 0x00000200
|
dcbInX = 0x00000200
|
||||||
DCB_ERROR_CHAR = 0x00000400
|
dcbErrorChar = 0x00000400
|
||||||
DCB_NULL = 0x00000800
|
dcbNull = 0x00000800
|
||||||
DCB_RTS_CONTROL_DISABLE_MASK = ^0x00003000
|
dcbRTSControlDisbaleMask = ^0x00003000
|
||||||
DCB_RTS_CONTROL_ENABLE = 0x00001000
|
dcbRTSControlEnable = 0x00001000
|
||||||
DCB_RTS_CONTROL_HANDSHAKE = 0x00002000
|
dcbRTSControlHandshake = 0x00002000
|
||||||
DCB_RTS_CONTROL_TOGGLE = 0x00003000
|
dcbRTSControlToggle = 0x00003000
|
||||||
DCB_ABORT_ON_ERROR = 0x00004000
|
dcbAbortOnError = 0x00004000
|
||||||
)
|
)
|
||||||
|
|
||||||
type DCB struct {
|
type dcb struct {
|
||||||
DCBlength uint32
|
DCBlength uint32
|
||||||
BaudRate uint32
|
BaudRate uint32
|
||||||
|
|
||||||
@@ -139,12 +139,12 @@ type DCB struct {
|
|||||||
XonChar byte
|
XonChar byte
|
||||||
XoffChar byte
|
XoffChar byte
|
||||||
ErrorChar byte
|
ErrorChar byte
|
||||||
EofChar byte
|
EOFChar byte
|
||||||
EvtChar byte
|
EvtChar byte
|
||||||
wReserved1 uint16
|
wReserved1 uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
type COMMTIMEOUTS struct {
|
type commTimeouts struct {
|
||||||
ReadIntervalTimeout uint32
|
ReadIntervalTimeout uint32
|
||||||
ReadTotalTimeoutMultiplier uint32
|
ReadTotalTimeoutMultiplier uint32
|
||||||
ReadTotalTimeoutConstant uint32
|
ReadTotalTimeoutConstant uint32
|
||||||
@@ -152,27 +152,27 @@ type COMMTIMEOUTS struct {
|
|||||||
WriteTotalTimeoutConstant uint32
|
WriteTotalTimeoutConstant uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
//sys GetCommState(handle syscall.Handle, dcb *DCB) (err error)
|
//sys getCommState(handle syscall.Handle, dcb *dcb) (err error) = GetCommState
|
||||||
//sys SetCommState(handle syscall.Handle, dcb *DCB) (err error)
|
//sys setCommState(handle syscall.Handle, dcb *dcb) (err error) = SetCommState
|
||||||
//sys SetCommTimeouts(handle syscall.Handle, timeouts *COMMTIMEOUTS) (err error)
|
//sys setCommTimeouts(handle syscall.Handle, timeouts *commTimeouts) (err error) = SetCommTimeouts
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NOPARITY = 0 // Default
|
noParity = 0 // Default
|
||||||
ODDPARITY = 1
|
oddParity = 1
|
||||||
EVENPARITY = 2
|
evenParity = 2
|
||||||
MARKPARITY = 3
|
markParity = 3
|
||||||
SPACEPARITY = 4
|
spaceParity = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ONESTOPBIT = 0 // Default
|
oneStopBit = 0 // Default
|
||||||
ONE5STOPBITS = 1
|
one5StopBits = 1
|
||||||
TWOSTOPBITS = 2
|
twoStopBits = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
func (port *windowsPort) SetMode(mode *Mode) error {
|
func (port *windowsPort) SetMode(mode *Mode) error {
|
||||||
params := DCB{}
|
params := dcb{}
|
||||||
if GetCommState(port.handle, ¶ms) != nil {
|
if getCommState(port.handle, ¶ms) != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
return &PortError{code: InvalidSerialPort}
|
return &PortError{code: InvalidSerialPort}
|
||||||
}
|
}
|
||||||
@@ -188,7 +188,7 @@ func (port *windowsPort) SetMode(mode *Mode) error {
|
|||||||
}
|
}
|
||||||
params.StopBits = byte(mode.StopBits)
|
params.StopBits = byte(mode.StopBits)
|
||||||
params.Parity = byte(mode.Parity)
|
params.Parity = byte(mode.Parity)
|
||||||
if SetCommState(port.handle, ¶ms) != nil {
|
if setCommState(port.handle, ¶ms) != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
return &PortError{code: InvalidSerialPort}
|
return &PortError{code: InvalidSerialPort}
|
||||||
}
|
}
|
||||||
@@ -228,38 +228,38 @@ func nativeOpen(portName string, mode *Mode) (*windowsPort, error) {
|
|||||||
return nil, &PortError{code: InvalidSerialPort}
|
return nil, &PortError{code: InvalidSerialPort}
|
||||||
}
|
}
|
||||||
|
|
||||||
params := &DCB{}
|
params := &dcb{}
|
||||||
if GetCommState(port.handle, params) != nil {
|
if getCommState(port.handle, params) != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
return nil, &PortError{code: InvalidSerialPort}
|
return nil, &PortError{code: InvalidSerialPort}
|
||||||
}
|
}
|
||||||
params.Flags |= DCB_RTS_CONTROL_ENABLE | DCB_DTR_CONTROL_ENABLE
|
params.Flags |= dcbRTSControlEnable | dcbDTRControlEnable
|
||||||
params.Flags &= ^uint32(DCB_OUT_X_CTS_FLOW)
|
params.Flags &= ^uint32(dcbOutXCTSFlow)
|
||||||
params.Flags &= ^uint32(DCB_OUT_X_DSR_FLOW)
|
params.Flags &= ^uint32(dcbOutXDSRFlow)
|
||||||
params.Flags &= ^uint32(DCB_DSR_SENSITIVITY)
|
params.Flags &= ^uint32(dcbDSRSensitivity)
|
||||||
params.Flags |= DCB_TX_CONTINUE_ON_XOFF
|
params.Flags |= dcbTXContinueOnXOFF
|
||||||
params.Flags &= ^uint32(DCB_IN_X | DCB_OUT_X)
|
params.Flags &= ^uint32(dcbInX | dcbOutX)
|
||||||
params.Flags &= ^uint32(DCB_ERROR_CHAR)
|
params.Flags &= ^uint32(dcbErrorChar)
|
||||||
params.Flags &= ^uint32(DCB_NULL)
|
params.Flags &= ^uint32(dcbNull)
|
||||||
params.Flags &= ^uint32(DCB_ABORT_ON_ERROR)
|
params.Flags &= ^uint32(dcbAbortOnError)
|
||||||
params.XonLim = 2048
|
params.XonLim = 2048
|
||||||
params.XoffLim = 512
|
params.XoffLim = 512
|
||||||
params.XonChar = 17 // DC1
|
params.XonChar = 17 // DC1
|
||||||
params.XoffChar = 19 // C3
|
params.XoffChar = 19 // C3
|
||||||
if SetCommState(port.handle, params) != nil {
|
if setCommState(port.handle, params) != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
return nil, &PortError{code: InvalidSerialPort}
|
return nil, &PortError{code: InvalidSerialPort}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set timeouts to 1 second
|
// Set timeouts to 1 second
|
||||||
timeouts := &COMMTIMEOUTS{
|
timeouts := &commTimeouts{
|
||||||
ReadIntervalTimeout: 0xFFFFFFFF,
|
ReadIntervalTimeout: 0xFFFFFFFF,
|
||||||
ReadTotalTimeoutMultiplier: 0xFFFFFFFF,
|
ReadTotalTimeoutMultiplier: 0xFFFFFFFF,
|
||||||
ReadTotalTimeoutConstant: 1000, // 1 sec
|
ReadTotalTimeoutConstant: 1000, // 1 sec
|
||||||
WriteTotalTimeoutConstant: 0,
|
WriteTotalTimeoutConstant: 0,
|
||||||
WriteTotalTimeoutMultiplier: 0,
|
WriteTotalTimeoutMultiplier: 0,
|
||||||
}
|
}
|
||||||
if SetCommTimeouts(port.handle, timeouts) != nil {
|
if setCommTimeouts(port.handle, timeouts) != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
return nil, &PortError{code: InvalidSerialPort}
|
return nil, &PortError{code: InvalidSerialPort}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,12 @@
|
|||||||
|
|
||||||
package serial
|
package serial
|
||||||
|
|
||||||
import "unsafe"
|
import (
|
||||||
import "syscall"
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ unsafe.Pointer
|
||||||
|
|
||||||
var (
|
var (
|
||||||
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
|
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
|
||||||
@@ -16,7 +20,7 @@ var (
|
|||||||
procSetCommTimeouts = modkernel32.NewProc("SetCommTimeouts")
|
procSetCommTimeouts = modkernel32.NewProc("SetCommTimeouts")
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegEnumValue(key syscall.Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, value *uint16, valueLen *uint32) (regerrno error) {
|
func regEnumValue(key syscall.Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, value *uint16, valueLen *uint32) (regerrno error) {
|
||||||
r0, _, _ := syscall.Syscall9(procRegEnumValueW.Addr(), 8, uintptr(key), uintptr(index), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(nameLen)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(class)), uintptr(unsafe.Pointer(value)), uintptr(unsafe.Pointer(valueLen)), 0)
|
r0, _, _ := syscall.Syscall9(procRegEnumValueW.Addr(), 8, uintptr(key), uintptr(index), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(nameLen)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(class)), uintptr(unsafe.Pointer(value)), uintptr(unsafe.Pointer(valueLen)), 0)
|
||||||
if r0 != 0 {
|
if r0 != 0 {
|
||||||
regerrno = syscall.Errno(r0)
|
regerrno = syscall.Errno(r0)
|
||||||
@@ -24,7 +28,7 @@ func RegEnumValue(key syscall.Handle, index uint32, name *uint16, nameLen *uint3
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCommState(handle syscall.Handle, dcb *DCB) (err error) {
|
func getCommState(handle syscall.Handle, dcb *dcb) (err error) {
|
||||||
r1, _, e1 := syscall.Syscall(procGetCommState.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(dcb)), 0)
|
r1, _, e1 := syscall.Syscall(procGetCommState.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(dcb)), 0)
|
||||||
if r1 == 0 {
|
if r1 == 0 {
|
||||||
if e1 != 0 {
|
if e1 != 0 {
|
||||||
@@ -36,7 +40,7 @@ func GetCommState(handle syscall.Handle, dcb *DCB) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetCommState(handle syscall.Handle, dcb *DCB) (err error) {
|
func setCommState(handle syscall.Handle, dcb *dcb) (err error) {
|
||||||
r1, _, e1 := syscall.Syscall(procSetCommState.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(dcb)), 0)
|
r1, _, e1 := syscall.Syscall(procSetCommState.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(dcb)), 0)
|
||||||
if r1 == 0 {
|
if r1 == 0 {
|
||||||
if e1 != 0 {
|
if e1 != 0 {
|
||||||
@@ -48,7 +52,7 @@ func SetCommState(handle syscall.Handle, dcb *DCB) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetCommTimeouts(handle syscall.Handle, timeouts *COMMTIMEOUTS) (err error) {
|
func setCommTimeouts(handle syscall.Handle, timeouts *commTimeouts) (err error) {
|
||||||
r1, _, e1 := syscall.Syscall(procSetCommTimeouts.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(timeouts)), 0)
|
r1, _, e1 := syscall.Syscall(procSetCommTimeouts.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(timeouts)), 0)
|
||||||
if r1 == 0 {
|
if r1 == 0 {
|
||||||
if e1 != 0 {
|
if e1 != 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user