Skip to content

Commit 31cfda2

Browse files
committed
x/sys/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).
1 parent 95f07ec commit 31cfda2

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

windows/syscall_windows.go

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

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

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

0 commit comments

Comments
 (0)