I often need to send short character strings to a serial port in Windows. The sort of thing I might use this for is sending a command to a robot or other microcontroller-based device that I’m building using a USB-to-serial converter. I actually developed a small command line utility called SerialSend for doing exactly this, but in many cases you can get away without using any special programs at all. Instead you can just use Windows’ built-in echo, set and mode commands.
The simplest case is something like the following which sends the string “hello” to COM1 (the first serial port). You can just type this command into a normal Windows console:
echo hello > COM1
The echo command is typically used to display a string in the console. Here however, its output is redirected (using the “>” character) to the special filename “COM1”, which is actually a serial port rather than a file on disk. So the string “hello” gets sent to the serial port rather than to the screen.
There are a couple of potential snags though:
- You need to know the number of the COM port you want to send to. If you’re using a USB-to-serial converter, this number may change over time, especially if you plug the device into different USB sockets. (SerialSend provides an easy alternative method of sending strings to whatever the highest numbered available COM port is, which can be very useful.)
- The string that gets sent in the above example is actually 8 bytes long because it includes the trailing space character after the word “hello” as well as carriage return and line feed characters. I tested this myself by capturing the transmitted bytes using a microcontroller and then echoing their numerical values back to the screen – the values were: “104 101 108 108 111 32 13 10”. The first five byte values in the sequence are just the letters of the word “hello”, but the last three are the space, carriage return and line feed characters. An alternative method of sending the string without these trailing characters using the set command is shown below.
- The serial port you’re using may not already be set to the baudrate you desire. In this case, an additional mode command can be used to configure the baudrate (and/or other serial parameters).
- Higher numbered COM ports may not be recognised when written this way, but a workaround is shown below.
The following example is a more robust version of the command shown above:
set /p x="hello" <nul >\\.\COM22
This probably looks a bit confusing, so let’s break it down.
The set command is normally used to set the value of an environment variable. For example, the following command could be used to set an environment variable called x equal to the string “sunshine”:
When used with the “/p” switch, the set command prompts the user to enter a value for the environment variable. The prompt displayed on screen is the string provided in the command. For example,
set /p x="Enter a value for x: "
Of course, we’re actually not interested in setting the value of x at all – it’s just a means to a different end. All we really want is a way of outputting the string “hello” without any carriage return and line feed characters, and the set command just happens to provide a convenient way of doing it.
We don’t want the set command to sit around waiting for the user to enter a value for x, so the input to the set command is redirected from “nul” (i.e. no input at all, rather than input from the console which would normally be the case). This means that the command finishes immediately rather than waiting for something to be typed in. Input redirection is performed using the “<" character.
set /p x="Enter a value for x: " <nul
Now that we’re outputting the correct characters (and only the correct characters), we need to redirect the output to COM22. To ensure that the COM port name is recognised when redirecting the output, its special filename is written in its full form, "\\.\COM22".
set /p x="hello" <nul >\\.\COM22
Finally, to configure the baudrate before sending a string to the serial port, the mode command can be used. For example, to set COM22 to 38,400 baud with 8 data bits and no parity checking:
mode COM22 BAUD=38400 PARITY=n DATA=8
Here’s a complete example as it appeared in my console window: