Although the arrangement shown in section 7 also works on the Windows NT series operating systems, that is Windows NT, 2000, and XP, the timing action is unpredictable, and it is therefore difficult to make transmission and reception pauses as needed. Fortunately, these operating systems provides another, native, mechanism for the serial communications.
Like Unix, the serial ports in NT are treated much like files, with Open, Read, Write, and Close actions, and of course set and read settings (CommParam) functions replacing ioctl() and select(). Again, settings are different. As it turns out, we have a possibility to turn RTS and DTR on or off by setting proper values of two flags called fDtrControl and fRtsControl. Most of these have been treated in a manner consistent with the original purpose of an RS-232 port: as a means of connecting to a modem.
The serial port is opened with a call to CreateFile(). Then the handle returned from this is used to identify the port. Eventually it is closed using a CloseHandle() function. To set baud rate etc. the GetCommState() and SetCommState() functions are used. Here is where the two flags mentioned above are found. To write data out, the WriteFile() (or WriteFileEx()) function is used.
We can also use ReadFile() (or ReadFileEx()) to read the port. Here are some code examples that work on 9600 Baud on COM6: that do blocking reads and writing:
serin.c -- Read a serial port
setrace.c -- Write to a serial port
sconv.c -- Write a text to a serial port, then read some reply back
makefile.w32 -- MSVC makefile for the
above three example programs.
All of these open and set up the serial port, then writes and/or reads and then closes it. In many cases, the open and setup function calls may be separated out, so the port is allowed to stay open and in use for some time. These are just examples.
To achieve non-blocking reads (so the system will not sit and hang around for an actual character to come along to be read), what Windows NT calls "Overlapped" IO, the FileIOCompletionRoutine() function referred to in the ReadFileEx() call can be used to notify the rest of the program when there has been something (a character, a string or whatever) read. This is actually quite similar to the interrupt function in the DOS program shown or inside a UNIX device driver.
See the
documentation on MSDN, , notably the pages under
MSDN Home > MSDN Library > Win32 and COM > Development > System Services > Device I/O
for more details. I have not yet tried out this
reading and multitasking bit.
To 7. Programming on DOS, including DOS boxes under Windows.
To 9. Programming on Unix-systems: Xenix, HP-UX, BSD, and Linux