Simple command line trick for sending characters to a serial port in Windows

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”:

set x="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:

Console serial port example

This entry was posted in Uncategorized and tagged , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

84 Responses to Simple command line trick for sending characters to a serial port in Windows

  1. Horaira says:

    How to recieve data through commands,
    In other way if connected device is sending data to PC where it will be stored ?

  2. batchloaf says:

    Hi Horaira,

    Firstly, sorry for the delay in responding.

    You could take a look at ComPrinter, which is a program I wrote to display data received via a serial port on the screen. You could probably also use it to redirect the data into a file. The method described in this article to send characters to a serial port can probably also be adapted to receive data in a similar way, but I haven’t tried that myself.

    Ted

  3. Dorian says:

    Thanks a lot. I like the simplicity of this. However after trying it to command a device I had trouble using the serial port with another program, e.g. Reaterm. Had to power cycle to clear. How do I inspect or clear the serial port status to make sure it is in pristine power on condition. (I may try your ComPrinter as the other program to see if it also hangs.) Inspecting and clearing a serial port without having to cycle power would be good to be able to do.

    • batchloaf says:

      Hi Dorian,

      That’s interesting – I haven’t run into that problem myself. As a matter of interest, what version of Windows are you running? In recent years, I just haven’t had these kind of problems because Windows seems to have got much better at cleaning up properly when a program that’s using a hardware device crashes. Also, what kind of serial port device is it you’re using? None of my machines have a built-in serial port anymore, so I’m generally using a USB-to-serial converter which could simply be plugged out and plugged back in again if it really hung.

      Writing a program to check which ports are open and clear them is something I thought about trying a long time ago when I used to have this kind of problem. However, I just forgot about doing it because the problem basically just stopped happening to me. I’m not sure how straightforward it would be to write such a program. If Windows think the device is already in use, it won’t ordinarily allow another program to open or otherwise change the state of the device. Maybe there’s some way to use Administrator privileges to override another program, but I don’t know how.

      Anyway, I’m curious to hear more details about your setup if you don’t mind sharing them?

      Ted

  4. Anne M. says:

    Thanks for this application, it’s really handy… !! However, is it possible to run it while the COM port is already opened and used by another program ? I would like to use ComPrinter, let’s say on COM 5, but without turning off a software that needs to constantly read the output of this COM5…

  5. Anne M. says:

    Sorry, I just realised I commented the wrong post… :/

  6. User123 says:

    Is there a way to do this but sending hex characters? I’m wondering if something like:

    SET /A “num3=0x1B4136D” >\\.\COM6

    would work?

  7. Aaron says:

    Nevermind – I just redownloaded serialsend.exe – this time I noticed the program was 94kb instead of 18kb. Maybe I had a corrupted download the first time. But regardless, now it works.

    • batchloaf says:

      Ok phew! I’m not sure why you were seeing that error. Perhaps the command line arguments were being parsed differently from how you intended. It sounds like you were trying to send those as characters, but that they were being interpreted as the number of a serial port.

      Anyway, that’s great that the updated version of the binary is working correctly for you!

      Ted

  8. Daniel Jeske says:

    Hello,
    really nice page. I’am new in batch programming and I could not fount a solution to read data from my usb-serial (rs232) divece. Writing with your command is no problem: set /p sendData=”SnP” \\.\com45
    but to read from… I found no solution the last three days. Nothing works.
    copy com45 test.txt did not work …

    Please, can anybody help me?

    best regards
    daniel

    • batchloaf says:

      Hi Daniel,

      Maybe it would be worth trying the following:

      copy \\.\COM45 test.txt
      

      Apparenty, you sometimes need to specify the special file names for higher numbered COM ports in full form (i.e. with those leading slashes and full stop) in order for Windows to interpret it correctly. I’m not sure why this is, but I know I’ve had to do it in the past.

      Also, to configure the serial parameters (e.g. baud rate) you may need to do one of the following:

      1. Go into the device settings in Windows Control panel to configure the default baud rate for your serial device.
      2. Run a command like “mode COM45 BAUD=38400 PARITY=n DATA=8” before you open the COM port to configure the baud rate, etc.

      You could also consider trying my program ComPrinter, which is what I use to display incoming serial data in the console.

      Hope that helps!

      Ted

      • Daniel Jeske says:

        Hello Ted,

        I’m really grateful for your help. But unfortunately it does not work. The command respond with “incorrect parameter” when I type: copy \\.\com45 test.txt.
        I spend more then three afternoons to find your page and solve 50% of my problem (to send and receive data with the special file name – sending works now). I normally use C, C++ or C# and sending or receiving data is no problem. A colleague show me his batch files and since them I’am really interested to learn more about these stuff. So thank you very much for your printer code… but I like to solve the receiving problem with a batch file. If you have another idea… I like to test ist. When I find a solution, I’ll post it.
        Many greetings from Mönchengladbach, Germany
        Daniel

      • Daniel Jeske says:

        Hello Ted,

        is it possible to cancel my second name? I think Daniel is enought :-).

        Best regards Daniel

        “ad hocumentati

      • batchloaf says:

        Hi Daniel,

        Firstly, I’m not sure how to hide your surname. I agree there’s no need to display it, but WordPress displays that automatically – I don’t think it’s under my control. Maybe you can change what’s displayed by changing your own wordpress/gravatar profile settings? Anyway, I’ll double check in case it’s something at my end. If you can’t change it yourself, feel free to delete the messages (or I can do it) once I’ve read them.

        Now, back to the problem: Sorry it took a while to try this on my own computer because things are very busy in work. However, I’ve just tested it with the “type” command and I was able to output the incoming data straight to the console, as shown in this screenshot:

        https://batchloaf.files.wordpress.com/2014/06/screenshot.png

        As you can see, I ran ComPrinter first, which configured the baudrate to 38400, which is what my connected device was transmitting at. I just used a dsPIC microcontroller to send in a sequence of integers.

        Could you give that a try?

        Ted

  9. Basu says:

    Hi

    Could you please let me know how to send Enter command also, as i need to multiple commands one after another to serial port.

    • batchloaf says:

      Hi Basu,

      When I need to send arbitrary bytes, I use my application SerialSend, which lets you send arbitrary hex bytes to the serial port. It’s just a simple exe file that you can drop into whatever folder you’re working out of. Here’s the link (you’ll find instructions there too for sending hex bytes):

      https://batchloaf.wordpress.com/serialsend/

      However, it’s probably possible to do what you want by placing the commands you want to send into a text file and then using a command like “type” to output the complete contents of the file to the serial port. Something like this…

      type mycommands.txt >\\.\COM4
      

      …which assumes your commands are in “mycommands.txt” and that you’re sending them to COM4.

      You can probably even just use the “echo” command to send the commands one at a time. For example, to send two commands to COM4, you could try…

      echo your_first_command >\\.\COM4
      echo your_second_command >\\.\COM4
      

      The “echo” command automatically adds a new line at the end whatever it prints.

      Ted

  10. altonmazin says:

    Hello,
    im trying to send a ascii or hex serial command to turn on tv. but unable to do so. here is my code

    serialsend.exe /hex “x6b\x61\x20\x30\x31\x20\x30\x31\x0d” HEX
    serialsend.exe ka 01 01\r ASCII

    can you please help

    thanks

    • batchloaf says:

      Hi Altonmazin,

      I think you probably just need to use these slightly modified commands:

      serialsend.exe /hex "\x6b\x61\x20\x30\x31\x20\x30\x31\x0d"
      serialsend.exe /hex "ka 01 01\r"
      

      As far as I recall, I made it so that you can only use the “\r” and “\r” escape sequences with SerialSend when you specify the “/hex” option. See full instructions here.

      The two commands above could be combined into a single command, by the way. SerialSend allows you to mix normal printable characters and hex bytes when the “/hex” option is used. Only “\x” followed by two digits will be treated as a hex value; other characters should just be treated as how they appear on screen. For example,

      serialsend.exe /hex "\x6b\x61\x20\x30\x31\x20\x30\x31\x0dka 01 01\r"
      

      …should send a total of 18 characters, including the two spaces and the carriage return.

      If there’s some kind of documentation for the command protocol for your TV link, I can check that the above commands send the correct bytes.

      Regards,
      Ted

  11. Michael says:

    Hello Ted,
    first of all thank you for this helpful information. According to this I now manage to talk via com25 to my microcontroller. However I am running in the same problem as Daniel above: receiving data from COM25 is still a problem:
    “type \\.\com25” reveals the error message: “The syntax for the filename… is wrong”
    “type com25” results in “The system cannot find the file” ( The exact wording of the error messages may be a little different since I translated them from my german windows)
    I saw in the thread from Daniel your screenshot, but unfortunately it does not work here. SInce Daniel did not send an answer I don’t know wether he found a work around.
    Best regards michael
    Btw: I tried to find out some information on this “funny” notation \\.\ but without success. Do you know were i can find some additional information to fully understand what these slashes an full stop mean?

    • Michael says:

      Hello Ted,
      just an update to my question above:
      I tried your ComPrinter Program an this can receive the data from my microcontroller. So may be the problem can be reduced to the question wether comPrinter.exe can be automatically finished after receiving one line of Input?
      This sounds a little strange so it might be useful to describe what I want to do:
      My microcontroller waits for the charakter “m” then it starts a measuring cycle and outputs the data on the serila port. On the PC-side I thik for a batch file that will send “m” to the microcontroller, recive the measured data and append them to a file on the harddisc. The whole thing is surrounded by some commands that crate a new file every day and include a header line as first line in the logfile. This batch file will be started at fixed times as a scheduled task
      So this is the idea for the batch: file
      MODE COM25 BAUD=9600 PARITY=N DATA=8 STOP=1 rts=off dtr=off
      for /f %%a in (‘date /t’) do set dat=%%a
      if exist “c:\Temp\Heizung\Logfile_%dat%.csv” goto existiert
      if NOT exist “c:\Temp\Heizung\Logfile_%dat%.csv” goto erzeugen
      :existiert
      echo m>\\.\COM25
      type COM25 >>”c:\Temp\Heizung\Logfile_%dat%.csv”
      goto ende
      :erzeugen
      type “c:\Temp\Heizung\Header.csv” > “c:\Temp\Heizung\Logfile_%dat%.csv”
      goto existiert
      :ende

      Unfortunately the “type com25 >>logfile” does not work as explained above. On the other hand if I could stop ComPrinter after one line ( symbolized by the /STOP1LINE parameter) I could replace the “type com25″ command by the two following lines:

      ComPrinter.exe /quiet /devnum 25 /baudrate 9600 /STOP1Line >> Daten.txt
      type Daten.txt >>”c:\Temp\Heizung\Logfile_%dat%.csv”

      Best regards
      michael

      • batchloaf says:

        Hello again Michael,

        I’ve just been reading around a bit and I have a different suggestion you might try:

        • Stick to using the type command, but specify the full device file name for the COM port, because COM ports above COM9 apparently require the full name (“\\.\COM25” in this case) to be specified, as mentioned somewhere on this MSDN page. i.e.
        type \\.\COM25 >> "c:\Temp\Heizung\Logfile_%dat%.csv"
        
        • Program your microcontroller to send a Ctrl-Z character at the end of the transmission to simulate “end of file”. As far as I know, Ctrl-Z is ASCII character 26 decimal (0x1A in hex).
        • My hope is that the type command will interpret the Ctrl-Z as the end of the “file” and exit at that point. The reason I think that is because of what I read here.

        I suggest updating the microcontroller code first and then checking whether the Ctrl-Z trick works by typing in the ECHO and TYPE commmands manually at the command line. If the type command doesn’t finish running when it should, then apparently Ctrl-Z is not the right terminating character. To test (once you’ve updated the microcontroller to send Ctrl-Z), type in something like this at the command line:

        echo m > \\.\COM25
        type \\.\COM25 >> "test.csv"
        

        By the way, that echo command may send a couple of additional characters after the “m”, such as a line feed or whatever, so make sure your microcontroller code is prepared to receive (and presumably ignore) other characters that might be transmitted.

        Ok, give that a try and see what it does! I’m kind of guessing at the solution here, but I’m sure it should be possible to make something like that work.

        Ted

      • Michael says:

        Hi Ted,
        thank you for your engagement,
        The fact that the echo command sends a little mor than solely “m” is no problem: th e microcontroler does what he is expected to do after the “echo m > \\.\COM25” command

        Unfortunately I have already tried what you suggested. I also tried different “end-codes” like CTRL-D , but the problem is that the command
        type \\.\COM25 >> “test.csv”
        results in an error message that the “syntax for filename, foldername or volume is wrong”
        There must be a problem with the name: if I test the command with \\.\com1 instead of \\.\com25 the same error message occurs. However “type COM1 >> “test.csv”” waits for input from the com port as it should.

        michael

      • batchloaf says:

        I see. What about putting the device file name in inverted commas, as follows:

        type "\\.\COM25" >> "test.csv"
        

        Have you already tried that?

    • batchloaf says:

      Hi Michael,

      Sorry, this reply will be incomplete because I’m rushing out the door, but here’s a link where you can find out a little bit more about that double slash notation for special device files in Windows:

      http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx

      To be honest, I’m still quite confused about it myself! Look at the section of that page entitled “Communications Resources”.

      Ted

  12. Michael says:

    Hi Ted,
    inverted commas also do not change the situation.
    However I noticed somthing additionally strange:
    I put the tho commands
    echo m>\\.\COM25
    type “\\.\COM25” > Daten.txt
    in a batch file without suppressing echos on the scree via the @-operator.
    What is written on the screen is this:

    c:\> echo m 1>\\.\COM25
    c:\> type “\\.\COM25” 1> Daten.txt
    Die Syntax für den Dateinamen, Verzeichnisnamen ode die Datenträgerbezeichnung ist falsch

    In the case of the first command “echo m>\\.\COM25” everything works fine but
    “type “\\.\COM25″ > Daten.txt” reveals the error message.( sorry it is in german )
    Looking on the echoed code an additional “1” is echoed on the screen before the “>” operators. I am not so familiar with batch files, but is this normal?
    michael

    • batchloaf says:

      Hi Michael,

      I’ve been hunting around for more information online and I’ve discovered several examples that include a colon character after the filename of the COM port device. For example:

      type COM1: > test.txt
      

      I’m not sure exactly what that colon means, but it’s in quite a few of the examples I found, so it’s definitely worth a try.
      Maybe one of the following will work?

      c:\> type \\.\COM25: > Daten.txt
      

      …or this…

      c:\> type "\\.\COM25:" > Daten.txt
      

      …or this…

      c:\> type "\\.\COM25": > Daten.txt
      

      Here are a couple of the links where I found the examples I mentioned:

      http://stackoverflow.com/questions/1052741/how-can-i-redirect-windows-com-port-output-to-a-file
      http://superuser.com/questions/129447/how-can-i-pipe-the-output-of-the-serial-port-to-a-file-in-windows

      By the way, can I just check that you’re typing in these commands into the command window, rather than cutting and pasting? The reason I ask is that the inverted comma characters are sometimes converted into different (but similar looking) characters from the normal keyboard inverted commas character. In some of the comment on this page for example, the inverted commas characters have been magically transformed into ’66’ and ’99’ style inverted commas, rather than the plain old inverted commas character you’ll find on your keyboard.

      Ted

      • Michael says:

        Hi Ted,
        first of all. yes I’m typing all characters in th ekeyborad. The reason is that my testcomputer has no netwrok connection so I have to “read and type” instead of “copy and paste” .
        Concerning the colon, I have allready tried all combinations with and withot colon, inverted commas and so on but all results in the cited error message. I fear it is only a small bit that has to be modified but how to know. i meanwhile have opened a question on the microsoft site my be there comes the solution…
        Best regards
        michael

      • batchloaf says:

        Ok, well hopefully your question on the MS site will produce a result! I’m afraid I’m running out of ideas myself. If you do manage to get it working, please let me know what the solution was.

        Ted

  13. Michael says:

    Hello Ted,
    unfortunately my question in the microsoft forum was not even read by anybody.
    So I tried to come up with some, admittedly not very elegant, workarounds to solve my problem.
    I managed to do so with the following code in a batch file:

    serialsend /devnum 25 /baudrate 9600 “m”
    start “COMREAD” cp.bat
    sleep 3
    set pid=LEER
    for /F “tokens=1,2,3,4,5,6,7,8,9,10,11 delims= ” %%a in (‘TASKLIST /V’) do IF “%%a”==”cmd.exe” IF “%%k”==”COMREAD” SET pid=%%b
    Echo Beende CMD.exe (PID: %pid%)
    taskkill /pid %pid%

    set stempel=%dat% %zeit%,
    set /p =%stempel%>”c:\Temp\Heizung\Logfile_%dat%.csv”
    type “Daten.txt” >> “c:\Temp\Heizung\Logfile_%dat%.csv”
    echo. >>”c:\Temp\Heizung\Logfile_%dat%.csv”

    cp.bat consists of one line only:
    comprinter /baudrate 9600 /devnum 25/quiet >Daten.txt

    As I mentionned earlier your program comprinter can access the com port and I can redirect its output to a file. The problem was that comprinter does not read only one line from the serial port but stays listening there “ad infinitum” and so no further communication via this port was possible. This problem was solved with the aid of a colleague who had the idea to use the “Start” command which starts a new instance of cmd.exe and running comprinter there while the original batch job contioues and after waiting three seconds stops comprinter via taskkill. Unfortunately the first attempt via “start Comprinter” and afterwards killing the comprinter process or its correponding cmd instance, resulted in an empty data file. However starting a batch fle in a new cmd instance and killing this new cmd instance results in a data file containing the information read from the serial port. The rest is just file handling to copy together the measured date and the time stamp and appending them to the log-file.
    Only one special thing is required additionally: with this brute termination of comprinter the data file always contained only the data-string but not the CR/LF at the end; even if multiply CR/LFs were sent by the mircocontroller. So I had to insert such a CR/LF by hand via the “echo. “command in the last line.
    Using the “echo “m” >\\.\com25″ command worked, but I feared that it might happen that the unnecessary control characters that are transmitted by this command too, might cause a buffer overrun after multiple repetition of the whole job.So I now use also your “serialsend” because with this I a am sure that only the “m” is sent.

    So with the aid of your tools I could solve my problem finally. Thank you very much. Another positive aspect of using cour programs is that they also work with lower com port numbers, so there is a chance to use the whole thing in combination with hardware com ports, too.

    Finally I just have one last question: what are the OS-requirements for your serialsend and comprinter programs? I.e. what is the oldest windows version under which the will work? Or might they even run under Win3.11 or MS-DOS? The queston is motivated by the fact that with this whole thing I would like to monitor the heating and warm water supply system of a friend and of course I don’t want to leave a very modern computer in his cellar infinitely running this batch jobs….

    So thank you once again for your help

    michael

    • batchloaf says:

      Hi Michael,

      I’m actually not 100% sure which operating systems this program will work with, but I would be very surprised if it works in Win 3.1 or DOS because it’s basically a Win32 program and hence 32-bit, whereas those operating systems are 16-bit.

      If I was going to use an old PC to do something like this, I’d probably go for Linux rather than an old MS operating system. Something to keep in mind though is that older PCs tend to be much less energy efficient, so leaving it running constantly could use a significant amount of energy in the long term. Maybe you could use a Raspberry Pi or something similar? Doing a bit of serial communication automation using Python on the Raspberry Pi would be nice way to do this!

      Ted

      • Michael says:

        Hi Ted,
        to be honest mentioning Dos or Win3.XX was an, admittedly not too good, joke since they do not support USB. However I seriously thought about a WinME but meanwhile it seems as I could arrange an old WinXP notebook, so everything should work. Concerning the energy consumption you are right of cours. The present Idea is to use the arrangement during this heating period until next spring with the old notebook. If my friend then is still fond of the data “flood” we will change to a Raspberry Pi. For me this would have the advantage to have a real project urging me to get familiar with this nice thing. For me there is the problem that if there is no real application I’m a little bit missing the drive to deal with these things just for fun…
        So in the end for my problem I didn’t really find an elegant solution but at least some workaround that I will use until next year. When switching to a Raspberry Pi the situation might be somewhat easier. i strolled a little bit around in the web and found some pages dealing with combining a Pi and an arduino, what is exactly what I will need.
        Best regards
        michael

      • batchloaf says:

        Great! Well, I hope everything works out well. Best of luck with it.

        Ted

  14. hemant ingle says:

    Hello sir, I am happy to see this post but I am searching a simple c program that can transmit a single character on serial port In windows 7 64 bit os to my microcontroller based designed. Will you please help me regarding this issue.??

    • batchloaf says:

      I have published a free program called SerialSend, which can do what you need:

      https://batchloaf.wordpress.com/serialsend/

      For example, to send a the character ‘A’ to COM4, you would do this:

      SerialSend.exe /devnum 4 "A"
      

      Of course, it doesn’t need to be just a single character – you can send a whole string if you like. Also, SerialSend can handle hex values for non-printable characters, as explained on the SerialSend page.

      Ted

  15. Daniel says:

    Hello Everybody,

    Sorry for not giving an answer. It is so great to see
    in which wise you are continuing your work.

    I stopped writing batch-files. But now I’ll spend my
    time to find a good way to read the rs232.
    Unfortunately I didn’t found an answer in the past.

    best regards
    Daniel

  16. Francis Garcia says:

    My old code is as follows:

    mode com1:9600,n,8,1
    copy %1 com1
    set newname=%1
    set newname=%newname:”=%.printed
    mode %1 “%newname%”

    it used to print to an attached serial thermal printer… now we upgraded our printer… its still thermal but plugged via USB… how do I make this work?

  17. hossam zayed says:

    we are a team of 4 engineers from Egypt working on a Flight Simulation Rig that rotates 360 degrees on 2 axis. We managed to build the simulation Rig and test it with a game (HAWX). it performs pretty well but after a few min of playing a time shift between the simulation and the rig appears.
    So we decided that we need feedback from the game it self with the plane exact angle of rotation on both x an y axis so we can compare it with the cabin orientation using the gyro on it , and the best way to do that is build a simple flight sim game using Cry engine and take the needed readings from it.
    So here is the question :
    Is it possible to send the plane angles of rotation data over serial bus in real time to our micro controller ?
    And if it is , can some one point us towards a similar project or a guide for how to do it.
    We really appreciate your feed back, it would really help us a lot. and best regards

  18. duyung says:

    Thank you Bro. Now i can write the LD without the vendor’s software.

  19. Hi Bud.
    how i get a list of serial ports actives on prompt? it’s possible do that?
    tkx (ps i’m from brazil, excuse errors on my english)

  20. Mina Edwar says:

    Dear Sir
    I need a command prompt or (a batch file) that can continuously send the ASCII code of a keyboard key when pressed to a specific serial port.
    Also if it can prompt the user to enter the serial port number before starting that would be great.

    Thank you

  21. rodrigo says:

    thanks a lot
    ive been searching so long & finally i found the answer to my problem
    now i can control my arduino uno plug&play thanks

  22. Emma Mailman says:

    There’s an interesting webpage that has just been deleted, but thanks to the miracle of hibernation, it’s still open in Firefox. The problem is, when I save it, it seems to attempt to retrieve the page from its source on the internet, which no longer exists. Is there any way I can configure Firefox to save the locally-held files instead?.

  23. Thanks a lot dude…..this handy little exe is great for techies!!!!!!!
    it also tells you which port is being used ( in case you weren’t sure )
    Thumbs up

  24. Vladimir says:

    Why just not using some more advances serial port terminal program. 🙂
    I use https://docklight.de/
    It has all the options you can need in these kind of testing. and free version is enough for all.

    • batchloaf says:

      Thanks for the suggestion Vladimir.

      I’ve never heard of Docklight before, but since it’s an additional piece of proprietary software that you need to install (or at least download?) onto the machine, rather than just using functionality that’s already built into the Windows terminal (as described above), it doesn’t seem directly comparable to me. For example, I sometimes find myself working on someone else’s machine where it’s not really appropriate for me to install additional software, but I want to send data to a serial device. In that situation, it’s nice to be able to just send a few bytes from the Windows terminal. Nevertheless, it does seem likely that something like Docklight will be better suited to some users’ needs.

      Ted

      • schutki says:

        It makes sense to use hyper-terminal for just sending some string (only ASCII visible characters and not binary data) with no additional software. But for anything more than that.. docklight or similar program would be nice to have

      • batchloaf says:

        Hyperterminal is no longer included with Windows. It hasn’t been included for a while now.

  25. schutki says:

    Forgot to say, docklight homepage is
    http://docklight.de/

  26. johns says:

    Hi,
    I want to execute some dbus commands after receiving eg: “reached……” at the end of line of teraterm window. Is it possible to execute dbus commands automatically every time?

    • batchloaf says:

      Hi Johns,

      It sounds like you’re trying to do the opposite of what’s shown in this example. i.e. you’re receiving data from a serial device (rather than sending data to a device) and you want to trigger a certain action each time a specific string is received.

      I have no idea whether teraterm can help you automate this, but maybe it’s possible to set up a scripting solution using that (or another terminal / serial debugging program). Personally, I recommend installing Python and PySerial if you want an easy and flexible way to automate interaction with a serial-connected device. That’s what I use when I need to set up something quick that requires two way interaction. In your case, you would just need to write a short Python program to open the serial device and read incoming strings, checking each one to see if it matches the required one. When you get a string that matches, you would just run whatever action you want.

      Ted

  27. On Windows 7 (at least): Try typing `chgport /?` and see if you have that program. If you do, `chgport COM#=COM##` will map a double digit port to a single digit port (assuming it isn’t already in use. e.g. use mode to see what ports exist and pick a single digit one that doesn’t.) So, for example `chgport COM5=COM50` when then allow `type COM5:`. The trick is that unless you have hardware handshaking, the data is already gone before the PC can read it. For example, I have a board the sends back a full message like “ABCDEFGHIJKLMNOPQRSTUVWXYZ” in response to a “?” and if I send `echo ?>COM5: && type COM5:` I get about “HIJKLMNOPQRSTUVWXYZ”. It might work ok with xon=on but my board doesn’t support that… yet.

  28. Pingback: Serial über Kommandozeile | wer bastelt mit?

  29. LHouse says:

    Ted,

    Very helpful thread! I have been using it to operate a power generator through Command Prompt instead of PuTTy like I had been doing.
    As you had suggested before, I have been using:
    type mycommands.txt >\\.\COM4
    to control my power generator through a USB serial connection. When I want to set the voltage to 2 V, I put the generator-specific command VSET1:2 in mycommands.txt and everything works fine. However if I want to set the voltage to 2.2 V, I need to use the command VSET1:2.2 which does not work when entered into mycommands.txt (even though the command works fine if going through PuTTy instead).

    Is there an easy way for me to use commands with decimal places from text files and thereby operate my generator with the resolution I need?

  30. Ashish Kumar Gupta says:

    Hello Mate,

    How to send data to comport through command prompt?
    Currently i’m doing Patlite device programming.
    I read the Patlite pdf file in this they mention how to send the command to Patlite device.
    I follow the document but not getting success yet.
    You can dowload the document from this url(https://drive.google.com/file/d/1ZVIWYLJz28DEeN6-NSsLExEpJI4ZJ4pq/view?usp=sharing)

    mode COM5 BAUD=9600 PARITY=n DATA=8 using this command i get the response from comport.
    set /p x=”1″ \\.\COM5 using this commad i turn of Patlite device with red color.

    But not able to understand which kind of command format need to send.

    I tried below commad but no commad work for me.

    set /p Header=”40H” Command=”30H” Data=”32″ EndCode=”21H” \\.\COM5

    set /p Header=”40H” Command=”30H” Data=”0011001100110010″ EndCode=”21H” \\.\COM5

    set /p Header=”40H” Id=”” Command=”30H” Data=”0011001100110010″ EndCode=”21H” \\.\COM5

    set /p format=”@??1S32!” \\.\COM5

    set /p format=”40H3FH3FH30H33H32H21H” \\.\COM5

    set /p Header=”@” Id=”??” Command=”1″ Data=”32″ EndCode=”!” \\.\COM5
    set /p Header=”40H” Id=”3FH3FH” Command=”30H” Data=”33H32H” EndCode=”21H” \\.\COM5

    Please Please Please help me.
    Please reply me ASAP

    • Ashish Kumar Gupta says:

      Can you send me which formate i need to use.

    • batchloaf says:

      If you’re doing anything more complicated than sending simple single commands, I would recommend installing Python (with the PySerial) module and using that instead. It will allow you to write a simple script to send all the commands you need and receive responses if required. You will be able to run your script from the command line. Mode and Set are useful for very simple things, but Python is free to download, easy to install, and will do all you need and more with a lot less effort.

      For something as long as what you described above, I would use Python.

      Ted

  31. Mike says:

    THIS IS AWESOME! I needed to create an application to sent chars to a COM port and this was perfect for that. I am MOST grateful for this. Thank you.

  32. fsfarimani says:

    to list all the COM ports in one line:

    For /F “tokens=4 Delims=: ” %A In (‘Mode^|FindStr “COM[0-9]*”‘)Do @(@Echo|@Set /p=”%A “)

  33. Benjah says:

    Hi Ted,

    Nice work on this application by the way. I’m struggling a little in my usage. I have a lighting controller that needs commands to be sent as HEX by the looks of it for example C0000023FF0101DCC0

    when I run serialsend /hex C0000023FF0101DCC0

    Using a com port viewer I can see the data being sent via serialsend as the hex conversion of that text eg 43 30 30 30 30 30 32 33 46 46 30 31 30 31 44 43 43 30 0d 0a not the hex command itself

    I had some limited success converting it to ascii and sending À.. À however I come unstuck trying to send the 00/NULL part of that string.

    Any ideas?

    Thanks in advance from serial noob.

  34. JAINUL says:

    mode COM4 BAUD=9600 Parity=n DATA=8
    set /p x=CO= 0.05,CO2=10.80,O2=6.21,,,,\\.\com4
    while i am sending this data to com;3
    i am getting this error(access denied )

    but sending data com4 to com3 is working perfectly .

    help me with this sir,,,,

  35. hagai001@gmail.com says:

    How can I send binary to a com port, not only ascii?

    • batchloaf says:

      Hi Hagai. Sorry, I forgot to reply. Probably no longer relevant to you, but in case it is you can try using my SerialSend application with the “/hex” option that allow arbitrary byte values to be sent to a serial device. Here’s the link:

      SerialSend

      Ted

  36. Kian says:

    Hi, I’m going to send a string value to a printer (these values are instructions). How do I do this snake with cmd?

  37. allannossa says:

    Hi, I would like to express my eternal thanks, I grated for days until I found your page, if it wasn’t for her I would be screwed… THANK YOU VERY MUCH!

  38. allannossa says:

    If someone can help me with a final question, I need to receive a status of a relay, if it is on or off, through the serial port and show me on cmd, is this possible?

  39. AB says:

    Hi,
    Thanks for this write up from nearly 10 years ago, not much online anywhere else like it. Well as far as I know. Question:
    This partially works for me:

    echo ^Aÿÿÿÿ^E > com3

    But not this:

    set /p x=^Aÿÿÿÿ^E \\.\COM3

    Are they not both sending the same thing, but without the extra return characters at the end for the second one… Its sending code to a lighting board. The first one works partially in that something lights on the board, but not all the correct lights, but the second one, does not light anything.

    Also, how would I send a line return char also.

    Thanks,
    AB.

    • batchloaf says:

      Hi AB,

      You need to redirect the output to COM3 using the > character. Also, when used this way, the set command expects user input, so the way you wrote it will probably make it wait for something to be typed. In the example I used in the article, user input was simulated using “<nul" which meant that the command should terminate without waiting for anything to be typed. So, in summary, this might be worth trying:

      set /p x=^Aÿÿÿÿ^E <null >\\.\COM3
      

      On the other hand, I suppose it’s possible that you already included those parts in your command, but WordPress filtered them out of your comment because it looked like a HTML tag – is that what happened?

      Ted

      • AB says:

        Hi,

        thanks for getting back so quick.
        yes WP filtered them out.
        Plus its single “l” in “nul” right?

        I did resolve my issue, if not in the most elegant way.
        If I did the same thing using 232analyser or a USB Serial software,
        In DEC I would send 1,255,255,255,255,5,
        It works fine.

        What happened is when Windows10 upgraded to Windows11, my PHP script stopped working, it turned COM calls in to files for some strange reason.

        What I worked out is if I send it either way using “echo” or “set”.
        It did the same thing, so both ways work from command line…

        But the command generated was messing up – as characters are changed, and it was also not sending 0 correctly, so what I ended up doing is writing the command to a file, then sending the call as per an example in the comments… that way it works as maintains the command characters.
        Also, If I need to send from PHP, I need to remove the // escape characters from the COM call.

        e.g. in PHP this worked, in case it helps anyone!

        <?php
        $scall = chr(1).chr(255).chr(0).chr(255).chr(255).chr(5);
        file_put_contents(“scom3.txt”, $scall);
        `mode com3: BAUD=38400 PARITY=N data=8 stop=1 xon=off`;
        `type “scom3.txt” > com3`;
        ?>

        Although I may end up using SerialSend.exe at a later date.
        Thanks Ted.

      • batchloaf says:

        Thanks AB, very useful PHP tip there!! Anyway, glad to hear you got things working.
        Ted

  40. Pingback: Resolved: Can Powershell Replace PuTTY/Thonny/Arduino IDE as a Serial Terminal? - Daily Developer Blog

  41. Nina Sweeney says:

    Hi Ted,

    First, thanks for your code and for providing a way for users to exchange information.

    While hard to believe, I am running Windows XP. I have two XP laptops that communicate with each other via modems attached to each of their serial ports. The COM ports for the modems and the laptops are configured identically and correctly.

    When I run a small C program to send a string from one laptop to the other, it works fine. However, when I tried typing

    echo hello > COM1

    on the sending laptop, nothing happened on the receiving laptop. Further, when I run a small C program to do this with the line

    system (“echo hello > COM1”);

    I get an “Access is denied” message.

    Any insight as to why this is occurring and how to work around it will be very much appreciated!

    Thank you,
    Nina Sweeney

Leave a comment