Updated API on windows implementation
This commit is contained in:
@@ -203,16 +203,24 @@ const (
|
|||||||
TWOSTOPBITS = 2
|
TWOSTOPBITS = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
func (port windowsSerialPort) SetSpeed(baudrate int) error {
|
func (port windowsSerialPort) SetMode(mode *Mode) error {
|
||||||
params := DCB{}
|
params := DCB{}
|
||||||
if err := GetCommState(port.Handle, ¶ms); err != nil {
|
if err := GetCommState(port.Handle, ¶ms); err != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
return &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
|
return &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
|
||||||
}
|
}
|
||||||
params.BaudRate = uint32(baudrate)
|
if mode.Baudrate == 0 {
|
||||||
params.ByteSize = 8
|
params.BaudRate = 9600 // Default to 9600
|
||||||
params.StopBits = ONESTOPBIT
|
} else {
|
||||||
params.Parity = NOPARITY
|
params.BaudRate = uint32(mode.BaudRate)
|
||||||
|
}
|
||||||
|
if mode.DataBits == 0 {
|
||||||
|
params.ByteSize = 8 // Default to 8 bits
|
||||||
|
} else {
|
||||||
|
params.ByteSize = mode.DataBits
|
||||||
|
}
|
||||||
|
params.StopBits = mode.StopBits
|
||||||
|
params.Parity = mode.Parity
|
||||||
if err := SetCommState(port.Handle, ¶ms); err != nil {
|
if err := SetCommState(port.Handle, ¶ms); err != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
return &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
|
return &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
|
||||||
@@ -220,7 +228,7 @@ func (port windowsSerialPort) SetSpeed(baudrate int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func OpenPort(portName string, useTIOCEXCL bool) (SerialPort, error) {
|
func OpenPort(portName string, mode Mode) (SerialPort, error) {
|
||||||
portName = "\\\\.\\" + portName
|
portName = "\\\\.\\" + portName
|
||||||
path, err := syscall.UTF16PtrFromString(portName)
|
path, err := syscall.UTF16PtrFromString(portName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -247,6 +255,12 @@ func OpenPort(portName string, useTIOCEXCL bool) (SerialPort, error) {
|
|||||||
Handle: handle,
|
Handle: handle,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set port parameters
|
||||||
|
if err := port.SetMode(mode); err != nil {
|
||||||
|
port.Close()
|
||||||
|
return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
|
||||||
|
}
|
||||||
|
|
||||||
params := &DCB{}
|
params := &DCB{}
|
||||||
err = GetCommState(port.Handle, params)
|
err = GetCommState(port.Handle, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -272,6 +286,7 @@ func OpenPort(portName string, useTIOCEXCL bool) (SerialPort, error) {
|
|||||||
return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
|
return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set timeouts to 1 second
|
||||||
timeouts := &COMMTIMEOUTS{
|
timeouts := &COMMTIMEOUTS{
|
||||||
ReadIntervalTimeout: 0xFFFFFFFF,
|
ReadIntervalTimeout: 0xFFFFFFFF,
|
||||||
ReadTotalTimeoutMultiplier: 0xFFFFFFFF,
|
ReadTotalTimeoutMultiplier: 0xFFFFFFFF,
|
||||||
|
|||||||
Reference in New Issue
Block a user