

Set device parameters (38400 baud, 1 start bit,ĭcbSerialParams.DCBlength = sizeof(dcbSerialParams) OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ) Open the highest available serial port numberįprintf(stderr, "Opening serial port.") Define the five bytes to send ("hello")

To compile with cl, the Microsoft compiler: Written by Ted Burke - last updated 13-2-2013

Either way, the resulting program “serial.exe” works the exact same.īy the way, for a simpler way to send arbitrary characters to a serial port without compiling anything at all, see my previous post. I show an example of each approach in the screenshots at the end of this post. In the former case, the file should be saved as “serial.cpp” and compiled as C++, whereas in the latter case, it can simply be saved as “serial.c” and compiled as plain C. This program can be compiled using cl.exe (the C++ compiler used in Visual C++ / Visual Studio) or using gcc (the C compiler in MinGW). In this case, I’m sending the five characters “hello” via COM22 at 38400 baud, but of course the program can easily be modified to send a different string, or to use a different serial port or baudrate. This is a (relatively) simple example of a C program to send five bytes to a serial port in Windows.
