Skip to content

Commit

Permalink
add serial port APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbrainman committed Nov 13, 2015
1 parent a0313f4 commit 2657113
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
51 changes: 51 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package winapi

const (
ONESTOPBIT = 0
TWOSTOPBITS = 2

NOPARITY = 0
ODDPARITY = 1
EVENPARITY = 2
MARKPARITY = 3
SPACEPARITY = 4
)

type DCB struct {
DCBlength uint32
BaudRate uint32
Flags uint32
_ uint16
XonLim uint16
XoffLim uint16
ByteSize byte
Parity byte
StopBits byte
XonChar byte
XoffChar byte
ErrorChar byte
EofChar byte
EvtChar byte
_ uint16
}

type COMMTIMEOUTS struct {
ReadIntervalTimeout uint32
ReadTotalTimeoutMultiplier uint32
ReadTotalTimeoutConstant uint32
WriteTotalTimeoutMultiplier uint32
WriteTotalTimeoutConstant uint32
}

//sys GetCommState(handle syscall.Handle, dcb *DCB) (err error)
//sys SetCommState(handle syscall.Handle, dcb *DCB) (err error)
//sys GetCommTimeouts(handle syscall.Handle, timeouts *COMMTIMEOUTS) (err error)
//sys SetCommTimeouts(handle syscall.Handle, timeouts *COMMTIMEOUTS) (err error)
//sys SetupComm(handle syscall.Handle, inqueue uint32, outqueue uint32) (err error)
//sys SetCommMask(handle syscall.Handle, mask uint32) (err error)
2 changes: 1 addition & 1 deletion winapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package winapi

//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zsyscall_windows.go winapi.go
//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zsyscall_windows.go winapi.go serial.go

type MEMORYSTATUSEX struct {
Length uint32
Expand Down
78 changes: 78 additions & 0 deletions zsyscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ var (
procGlobalMemoryStatusEx = modkernel32.NewProc("GlobalMemoryStatusEx")
procGetProcessHandleCount = modkernel32.NewProc("GetProcessHandleCount")
procGetVersionExW = modkernel32.NewProc("GetVersionExW")
procGetCommState = modkernel32.NewProc("GetCommState")
procSetCommState = modkernel32.NewProc("SetCommState")
procGetCommTimeouts = modkernel32.NewProc("GetCommTimeouts")
procSetCommTimeouts = modkernel32.NewProc("SetCommTimeouts")
procSetupComm = modkernel32.NewProc("SetupComm")
procSetCommMask = modkernel32.NewProc("SetCommMask")
)

func GlobalMemoryStatusEx(buf *MEMORYSTATUSEX) (err error) {
Expand Down Expand Up @@ -50,3 +56,75 @@ func GetVersionEx(versioninfo *OSVERSIONINFOEX) (err error) {
}
return
}

func GetCommState(handle syscall.Handle, dcb *DCB) (err error) {
r1, _, e1 := syscall.Syscall(procGetCommState.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(dcb)), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func SetCommState(handle syscall.Handle, dcb *DCB) (err error) {
r1, _, e1 := syscall.Syscall(procSetCommState.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(dcb)), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func GetCommTimeouts(handle syscall.Handle, timeouts *COMMTIMEOUTS) (err error) {
r1, _, e1 := syscall.Syscall(procGetCommTimeouts.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(timeouts)), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func SetCommTimeouts(handle syscall.Handle, timeouts *COMMTIMEOUTS) (err error) {
r1, _, e1 := syscall.Syscall(procSetCommTimeouts.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(timeouts)), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func SetupComm(handle syscall.Handle, inqueue uint32, outqueue uint32) (err error) {
r1, _, e1 := syscall.Syscall(procSetupComm.Addr(), 3, uintptr(handle), uintptr(inqueue), uintptr(outqueue))
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func SetCommMask(handle syscall.Handle, mask uint32) (err error) {
r1, _, e1 := syscall.Syscall(procSetCommMask.Addr(), 2, uintptr(handle), uintptr(mask), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

0 comments on commit 2657113

Please sign in to comment.