#350682 - 08/03/2012 15:32
How to send a text string to a serial COM port from a batch script
|
carpal tunnel
Registered: 20/12/1999
Posts: 31600
Loc: Seattle, WA
|
I've got a piece of code running on an Arduino board that will accept ASCII text commands on its serial port, and then will switch some relays based on which text commands you send it.
I need to be able to run an automation script or batch file (on Windows XP and later only) that will switch the relays under certain conditions during an overnight test run.
I have this all working, in that I can connect a serial terminal program to the COM155 port that the Arduino is connected to, and type the commands into the terminal program, and it will accept the commands and will work.
Is there a super-simple way to send these commands from the DOS prompt or from a DOS batch file or a vbscript? I've tried COPY and ECHO... those just create a file on the hard disk called "COM155" when I try it.
Uncle google isn't helping me here. I do searches, and the results always talk about using PUTTY and typing things live. I want to automate this. Or, the search results say to use COPY or ECHO, and of course, I tried that and it just makes a hard disk file named COM155.
I know that I could just compile an EXE file to use as a stub program that will handle the necessary details of opening the COM port, sending the command, and closing the COM port. But I'm trying for a quicker, simpler solution here without having to get into an extra layer of C code just to get this one simple thing done.
|
Top
|
|
|
|
#350683 - 08/03/2012 15:34
Re: How to send a text string to a serial COM port from a batch script
[Re: tfabris]
|
pooh-bah
Registered: 27/02/2004
Posts: 1919
Loc: London
|
Can't you send text output directly to a port? Like dir > lpt1?
I think you can send a txt file to a port too
ETA Not sure if that's any good to you though
Edited by tahir (08/03/2012 15:35)
|
Top
|
|
|
|
#350685 - 08/03/2012 15:41
Re: How to send a text string to a serial COM port from a batch script
[Re: tahir]
|
carpal tunnel
Registered: 20/12/1999
Posts: 31600
Loc: Seattle, WA
|
Right, that's exactly what I want to do. Problem is, I tried that. When I do:
ECHO relayon > COM155
Then it just creates a file on the hard disk called "COM155" and doesn't send anything to the COM port. COPY, same thing as ECHO.
|
Top
|
|
|
|
#350686 - 08/03/2012 15:41
Re: How to send a text string to a serial COM port from a batch script
[Re: tahir]
|
carpal tunnel
Registered: 13/07/2000
Posts: 4180
Loc: Cambridge, England
|
Isn't it "COM155:" with a colon? And do you really have 155 COM ports? if so, my COM155erations...
Peter
|
Top
|
|
|
|
#350687 - 08/03/2012 15:46
Re: How to send a text string to a serial COM port from a batch script
[Re: peter]
|
carpal tunnel
Registered: 20/12/1999
Posts: 31600
Loc: Seattle, WA
|
Isn't it "COM155:" with a colon? Same result. File on hard disk called "COM155" is created. And do you really have 155 COM ports? if so, my COM155erations... Hah, very clever. Yes, this computer has tested many different cellular modems and external GPS units. Each time I plug a new one in, the numbers go up. A given cellular modem might create 3 or 4 new COM ports on the system: One or more control ports, a data port, and a GPS NMEA port.
|
Top
|
|
|
|
#350688 - 08/03/2012 15:47
Re: How to send a text string to a serial COM port from a batch script
[Re: tfabris]
|
pooh-bah
Registered: 27/02/2004
Posts: 1919
Loc: London
|
What if the string was in a file which you sent to the port:
relayon.txt > com155
|
Top
|
|
|
|
#350689 - 08/03/2012 15:48
Re: How to send a text string to a serial COM port from a batch script
[Re: tfabris]
|
carpal tunnel
Registered: 20/12/1999
Posts: 31600
Loc: Seattle, WA
|
CRAP. Poking at it, I figured it out.
ECHO relayon > \\.\com155:
That works.
|
Top
|
|
|
|
#350690 - 08/03/2012 15:51
Re: How to send a text string to a serial COM port from a batch script
[Re: tfabris]
|
pooh-bah
Registered: 27/02/2004
Posts: 1919
Loc: London
|
|
Top
|
|
|
|
#350691 - 08/03/2012 18:07
Re: How to send a text string to a serial COM port from a batch script
[Re: tahir]
|
addict
Registered: 24/07/2002
Posts: 618
Loc: South London
|
I got here too late, you've already figured it out. For any COM port over 9 you have to use the \\.\COM name convention. Obviously that's also fine for ports lower than 10 as well.
|
Top
|
|
|
|
#350693 - 08/03/2012 18:13
Re: How to send a text string to a serial COM port from a batch script
[Re: sn00p]
|
addict
Registered: 24/07/2002
Posts: 618
Loc: South London
|
The ever increasing USB COM port numbers is either a blessing or an irritation depending on how you look at it. Windows creates a new USB device port for each unique vid, pid and serial number that it sees.
This has the advantage of the com port number being "sticky" for a particular device, but becomes a problem if you are just testing devices to make sure they function correctly, you can soon end up at stupidly high COM port numbers that never actually get seen again.
If the device does not have a serial number or all devices have the same serial number, then they'll all have the same COM port number, unless more than one of them is plugged in at the same time, in which case it will be random.
And don't get me started on the "Microsoft tax" which you have to pay if you want a CDC device to be enumerated without the need for an INF file, even though you're using the built in windows and standar USB device class.....
Edited by sn00p (08/03/2012 18:16)
|
Top
|
|
|
|
#350706 - 09/03/2012 17:43
Re: How to send a text string to a serial COM port from a batch script
[Re: sn00p]
|
carpal tunnel
Registered: 20/12/1999
Posts: 31600
Loc: Seattle, WA
|
If the device does not have a serial number or all devices have the same serial number, then they'll all have the same COM port number, unless more than one of them is plugged in at the same time, in which case it will be random. It's interesting. Some of our tests involve swapping the devices to different USB ports on the computer. Some devices, new COM ports are created when I swap to a different USB port, and other devices, they keep the same COM port number when I swap. What's funny is that I've got some USB GPS units which are made by Pharos but which are microsoft-branded. We've got both sets here: The original Pharos units as well as the Microsoft-rebranded ones. The MS ones have a slightly different USB ID and run on different drivers, but the hardware is identical, right down to every detail on the packaging and labeling. They both use a Prolific chipset for USB-to-serial conversion. The Microsoft ones are the ones that stay the same but the Pharos ones make a new COM port when I swap.
|
Top
|
|
|
|
#350707 - 09/03/2012 17:45
Re: How to send a text string to a serial COM port from a batch script
[Re: sn00p]
|
carpal tunnel
Registered: 20/12/1999
Posts: 31600
Loc: Seattle, WA
|
For any COM port over 9 you have to use the \\.\COM name convention. AHA. That is spectacularly good information, was precisely the root of my problem, and which never came up in any of my Google searches. FASCINATING. We already all know this, but I just have to say it again: This is the best BBS with the best people, ever. Don't ever change, you guys.
|
Top
|
|
|
|
#350713 - 09/03/2012 18:43
Re: How to send a text string to a serial COM port from a batch script
[Re: tfabris]
|
addict
Registered: 24/07/2002
Posts: 618
Loc: South London
|
I think you can see the USB serial number in the device manager, my guess is that the ones that are sticky (and generate new port numbers) will have serial numbers and the ones that reuse old port numbers don't have a serial number set.
Although you've also reminded me that port numbers will also change if the device doesn't have a serial number but is plugged into a different port....
Personally, I'd prefer windows to only use the physical USB port as the method for allocating COM ports, but then again I end up plugging in a lot of devices that behave as USB serial ports which probably isn't what most users do!
I too love the knowledge on this BBS! No question to big or small, somebody seems to have knowledge or an answer.
|
Top
|
|
|
|
|
|