Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
16 changes: 12 additions & 4 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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))

Expand All @@ -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(&params)), 0)
if r == 0 {
Expand Down