The Windows command line allows you to access the COM port by the name of the COMx type, where x is the port number. This feature came from MS DOS.
For example, to send the text “123” to a device, you can use the command
echo 123 > COM1
Bytes will be sent to the port containing the character codes from the echo command parameter.
To specify the port settings, you can use the mode command. Example of use:
mode COM1 BAUD=9600 PARITY=n DATA=8
This means that the COM1 port operates at a speed of 9600 baud, transmits data at 8 bits and does not use parity checking.
Judging by the tags, you can use C #. In this case, just use the SerialPort class from the System.IO.Ports namespace. However, this class allows you to work with the port only on the local machine.
If you require complex behavior, you can remotely create a file with a C # program from the command line and compile it with the csc.exe utility provided that the remote machine has .NET installed (the console compiler is included in its delivery package).
Perhaps there are other means for working remotely with the equipment of the machine that will suit you best. For example, if PowerShell is installed, you can take advantage of its capabilities.
Record:
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:
PS> $port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one PS> $port.Open() PS> $port.ReadLine()
An example of working with a port from VBScript (unfortunately, it was not possible to check on real hardware).
Record example:
Dim port, fso ' Константы для режимов работы с файлом Const ForAppending = 8 ' Объект для работы с файлами Set fso = CreateObject("Scripting.FileSystemObject") ' Открываем порт для записи Set port = fso.OpenTextFile("COM4:9600,N,8,1", ForAppending) ' Запись строки port.Write "test" ' См. также WriteLine ' Закрываем порт port.Close
Reading example:
Dim port, fso ' Константы для режимов работы с файлом Const ForReading = 1 ' Объект для работы с файлами Set fso = CreateObject("Scripting.FileSystemObject") ' Открываем порт для чтения Set port = fso.OpenTextFile("COM4:9600,N,8,1", ForReading) ' Если не конец потока данных if not port.AtEndOfStream then ' Выводим строку msgbox port.ReadAll ' См. также .Read и .ReadLine end if ' Закрываем порт port.Close
Learn more about working with a COM port from the command line.
SerialPort class documentation