From f82ec179c917d2e7c199d0fcccade70558ad40f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kontor=20Krist=C3=B3f?= Date: Tue, 6 Sep 2016 15:59:58 +0200 Subject: [PATCH] Adding extra open parameters: dataBits parity stopBits --- serial.go | 8 ++++---- serial_windows.go | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/serial.go b/serial.go index 1b62ac8..9e71dac 100644 --- a/serial.go +++ b/serial.go @@ -75,9 +75,9 @@ type Config struct { Name string Baud int - // Size int // 0 get translated to 8 - // Parity SomeNewTypeToGetCorrectDefaultOf_None - // StopBits SomeNewTypeToGetCorrectDefaultOf_1 + ByteSize byte // 0 get translated to 8 + Parity byte + StopBits byte // RTSFlowControl bool // DTRFlowControl bool @@ -91,7 +91,7 @@ type Config struct { // OpenPort opens a serial port with the specified configuration func OpenPort(c *Config) (io.ReadWriteCloser, error) { - return openPort(c.Name, c.Baud) + return openPort2(c) } // func Flush() diff --git a/serial_windows.go b/serial_windows.go index ce10681..43519bd 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -73,7 +73,11 @@ type structTimeouts struct { WriteTotalTimeoutConstant uint32 } -func openPort(name string, baud int) (rwc io.ReadWriteCloser, err error) { +func openPort2(c *Config) (rwc io.ReadWriteCloser, err error) { + return openPort(c.Name, c.Baud, c.ByteSize, c.Parity, c.StopBits) +} + +func openPort(name string, baud int, byteSize byte, parity byte, stopBits byte) (rwc io.ReadWriteCloser, err error) { if len(name) > 0 && name[0] != '\\' { name = "\\\\.\\" + name } @@ -95,7 +99,7 @@ func openPort(name string, baud int) (rwc io.ReadWriteCloser, err error) { } }() - if err = setCommState(h, baud); err != nil { + if err = setCommState(h, baud, byteSize, parity, stopBits); err != nil { return } if err = setupComm(h, 64, 64); err != nil { @@ -197,7 +201,7 @@ func getProcAddr(lib syscall.Handle, name string) uintptr { return addr } -func setCommState(h syscall.Handle, baud int) error { +func setCommState(h syscall.Handle, baud int, byteSize byte, parity byte, stopBits byte) error { var params structDCB params.DCBlength = uint32(unsafe.Sizeof(params)) @@ -208,7 +212,11 @@ func setCommState(h syscall.Handle, baud int) error { log.Println("Byte val of commstat flags[1]:", strconv.FormatInt(int64(params.flags[1]), 2)) params.BaudRate = uint32(baud) - params.ByteSize = 8 + params.ByteSize = byteSize + params.Parity = parity + params.StopBits = stopBits + + r, _, err := syscall.Syscall(nSetCommState, 2, uintptr(h), uintptr(unsafe.Pointer(¶ms)), 0) if r == 0 {