Skip to content

Commit 1a50d97

Browse files
twpayneodeke-em
authored andcommitted
windows: add serial comm functions
Serial ports are still widely used to communicate with a large range of devices. This change adds the remaining functions described in "Serial Communications in Win32", enabling Go applications and libraries to be written that support the full set of serial port functionality on Windows. x/sys/unix already has equivalent functionality through termios. See https://learn.microsoft.com/en-us/previous-versions/ms810467(v=msdn.10). Change-Id: I57f9ed6b7dbcc2331f740bd95b6483f141b0ad6f GitHub-Last-Rev: 0a5a744 GitHub-Pull-Request: #187 Reviewed-on: https://go-review.googlesource.com/c/sys/+/572295 Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
1 parent 95f07ec commit 1a50d97

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

windows/syscall_windows.go

+81
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,19 @@ func NewCallbackCDecl(fn interface{}) uintptr {
349349
//sys SetProcessPriorityBoost(process Handle, disable bool) (err error) = kernel32.SetProcessPriorityBoost
350350
//sys GetProcessWorkingSetSizeEx(hProcess Handle, lpMinimumWorkingSetSize *uintptr, lpMaximumWorkingSetSize *uintptr, flags *uint32)
351351
//sys SetProcessWorkingSetSizeEx(hProcess Handle, dwMinimumWorkingSetSize uintptr, dwMaximumWorkingSetSize uintptr, flags uint32) (err error)
352+
//sys ClearCommBreak(handle Handle) (err error)
353+
//sys ClearCommError(handle Handle, lpErrors *uint32, lpStat *ComStat) (err error)
354+
//sys EscapeCommFunction(handle Handle, dwFunc uint32) (err error)
355+
//sys GetCommState(handle Handle, lpDCB *DCB) (err error)
356+
//sys GetCommModemStatus(handle Handle, lpModemStat *uint32) (err error)
352357
//sys GetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error)
358+
//sys PurgeComm(handle Handle, dwFlags uint32) (err error)
359+
//sys SetCommBreak(handle Handle) (err error)
360+
//sys SetCommMask(handle Handle, dwEvtMask uint32) (err error)
361+
//sys SetCommState(handle Handle, lpDCB *DCB) (err error)
353362
//sys SetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error)
363+
//sys SetupComm(handle Handle, dwInQueue uint32, dwOutQueue uint32) (err error)
364+
//sys WaitCommEvent(handle Handle, lpEvtMask *uint32, lpOverlapped *Overlapped) (err error)
354365
//sys GetActiveProcessorCount(groupNumber uint16) (ret uint32)
355366
//sys GetMaximumProcessorCount(groupNumber uint16) (ret uint32)
356367
//sys EnumWindows(enumFunc uintptr, param unsafe.Pointer) (err error) = user32.EnumWindows
@@ -1835,3 +1846,73 @@ func ResizePseudoConsole(pconsole Handle, size Coord) error {
18351846
// accept arguments that can be casted to uintptr, and Coord can't.
18361847
return resizePseudoConsole(pconsole, *((*uint32)(unsafe.Pointer(&size))))
18371848
}
1849+
1850+
// DCB constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-dcb.
1851+
const (
1852+
CBR_110 = 110
1853+
CBR_300 = 300
1854+
CBR_600 = 600
1855+
CBR_1200 = 1200
1856+
CBR_2400 = 2400
1857+
CBR_4800 = 4800
1858+
CBR_9600 = 9600
1859+
CBR_14400 = 14400
1860+
CBR_19200 = 19200
1861+
CBR_38400 = 38400
1862+
CBR_57600 = 57600
1863+
CBR_115200 = 115200
1864+
CBR_128000 = 128000
1865+
CBR_256000 = 256000
1866+
1867+
DTR_CONTROL_DISABLE = 0x00
1868+
DTR_CONTROL_ENABLE = 0x01
1869+
DTR_CONTROL_HANDSHAKE = 0x02
1870+
1871+
RTS_CONTROL_DISABLE = 0x00
1872+
RTS_CONTROL_ENABLE = 0x01
1873+
RTS_CONTROL_HANDSHAKE = 0x02
1874+
RTS_CONTROL_TOGGLE = 0x03
1875+
1876+
NOPARITY = 0
1877+
ODDPARITY = 1
1878+
EVENPARITY = 2
1879+
MARKPARITY = 3
1880+
SPACEPARITY = 4
1881+
1882+
ONESTOPBIT = 0
1883+
ONE5STOPBITS = 1
1884+
TWOSTOPBITS = 2
1885+
)
1886+
1887+
// EscapeCommFunction constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-escapecommfunction.
1888+
const (
1889+
SETXOFF = 1
1890+
SETXON = 2
1891+
SETRTS = 3
1892+
CLRRTS = 4
1893+
SETDTR = 5
1894+
CLRDTR = 6
1895+
SETBREAK = 8
1896+
CLRBREAK = 9
1897+
)
1898+
1899+
// PurgeComm constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-purgecomm.
1900+
const (
1901+
PURGE_TXABORT = 0x0001
1902+
PURGE_RXABORT = 0x0002
1903+
PURGE_TXCLEAR = 0x0004
1904+
PURGE_RXCLEAR = 0x0008
1905+
)
1906+
1907+
// SetCommMask constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setcommmask.
1908+
const (
1909+
EV_RXCHAR = 0x0001
1910+
EV_RXFLAG = 0x0002
1911+
EV_TXEMPTY = 0x0004
1912+
EV_CTS = 0x0008
1913+
EV_DSR = 0x0010
1914+
EV_RLSD = 0x0020
1915+
EV_BREAK = 0x0040
1916+
EV_ERR = 0x0080
1917+
EV_RING = 0x0100
1918+
)

windows/types_windows.go

+24
Original file line numberDiff line numberDiff line change
@@ -3380,3 +3380,27 @@ type BLOB struct {
33803380
Size uint32
33813381
BlobData *byte
33823382
}
3383+
3384+
type ComStat struct {
3385+
Flags [4]uint8
3386+
CBInQue uint32
3387+
CBOutQue uint32
3388+
}
3389+
3390+
type DCB struct {
3391+
DCBlength uint32
3392+
BaudRate uint32
3393+
Flags [4]uint8
3394+
wReserved uint16
3395+
XonLim uint16
3396+
XoffLim uint16
3397+
ByteSize uint8
3398+
Parity uint8
3399+
StopBits uint8
3400+
XonChar byte
3401+
XoffChar byte
3402+
ErrorChar byte
3403+
EofChar byte
3404+
EvtChar byte
3405+
wReserved1 uint16
3406+
}

windows/zsyscall_windows.go

+99
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)