10

Is there a Windows command for opening serial ports, say COM3 via the command prompt in Windows 7? For example:

OPEN "COM6" AS #1

I cannot use pyserial or any other utilities that are not distributed with Windows 7.

Preferred solution Opening a COM port in QBasic on Windows 7

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Olumide
  • 5,397
  • 10
  • 55
  • 104

2 Answers2

23

Maybe you can use the Powershell? It's included in Win7...

code taken from here http://blogs.msdn.com/b/powershell/archive/2006/08/31/writing-and-reading-info-from-serial-ports.aspx

Writing to a Serial Port

PS> [System.IO.Ports.SerialPort]::getportnames()
COM3
PS> $port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
PS> $port.open()
PS> $port.WriteLine("Hello world")
PS> $port.Close()

Reading from a Serial Port

PS> $port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
PS> $port.Open()
PS> $port.ReadLine()
Max
  • 7,408
  • 2
  • 26
  • 32
  • Thanks. I was hoping to do so using plain old BASIC 'tho. – Olumide Oct 09 '12 at 15:23
  • 1
    I use this command with PHP and the shell_exec() function. I have an Arduino connected that I use with the Blink sketch (if you're familiar) that I modified and instead of blinking it prints "Led On" and "Led Off".But sometimes when I refresh my php script I get unreadable strings, like empty Or "?? ?On" or just "Led", incomplete. Is there a way to fix this issue? Does it need a buffer or something? – droplet Mar 24 '13 at 20:52
  • @Petsoukos I'm sorry, but I know nothing about PHP or Arduino. anyway, if the output is garbled, maybe there is something that interfere... maybe it's some other software/process writing to the same COM port? or maybe something terminating the script (in the shell_exec) before it reach it's natural ending? Maybe you can add some debug log/trace code/message to the start/end of your PHP script , and your shell_exec script to trace it them reach the end correctly, or if they get interrupted by something – Max Mar 25 '13 at 08:09
  • @Max $port.open() Access to the port 'COM16' is denied. Is there a way to fix this issue? – Kiran Kumar Kotari Sep 13 '16 at 04:41
  • @kirankumarkotari maybe that the COM16 is already in use by another software (or used by another instance of your software/script). So you can try to: close the other software that is using the COM16, or try to reboot the computer, or try to disable/enable the COM port in the device manager, or try to use Sysinternal PortMon to get of a better understanding of what's happening. It may also be that COM port access right are locked down, so you need administrative right to open the port. – Max Sep 13 '16 at 07:13
2

To send contents of a file to a serial port:

copy file.bin com1 /b

To send a few characters to a serial port:

echo hello > com1
david
  • 2,435
  • 1
  • 21
  • 33