I have the task of monitoring and controlling the UPS ARS, connected to a remote PC via the RS232 COM port. I know how to work with a COM port on a local PC and to monitor and control the ARS UPS. And also I know how to connect (via WMI or Telnet) to a remote PC and run and run the task and execute it in console mode. On a remote PC, I cannot install my software, i.e. I need to use standard Windows tools. And on a PC, where monitoring is carried out (local) I am the master, so my task is to find out how to manipulate the COM port (and therefore the APC UPS) with a standard PC using standard Windows tools, and to receive and process the results on a local PC (where monitoring is).

I heard that it is possible to remotely manipulate a COM port through WMI, but I don’t know how.

  • ru.stackoverflow.com/questions/413530 . I already answered you - ArchDemon
  • one
    I cannot install my software on a remote PC, so I can use only standard Windows tools !!! - Alex_Kot
  • No The remote computer should cooperate , that is, either your program or someone else’s program (in fact, the server) should run there, which, at your request, will perform the necessary actions. - VladD
  • one
    It's not obligatory. First option: You can hook up via WMI to a remote PC (after running the corresponding service, see Andrei Popov, E. Shikin. And administer Windows using WMI and WMIC) and make WMI requests and then process them on a local PC. The second option: via Telnet and execute console commands (CMD) and receive the results of command execution, and then process them on the local PC (after running the Telnet Server service). Those. the servers on the remote PC will be WMI Server or Telnet Server !!! - Alex_Kot

2 answers 2

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

  • Starting with the seven , powershell is installed by default on the system. - VladD
  • And do not tell me what is the best way to write scripts? In VBScript or JScript. I have half the PC under Windows XP. - Alex_Kot
  • By the way, yes! I forgot about them completely. Write in the language that is closer to you, there is no difference in possibilities. I will try to outline an example and supplement the answer. - velikodniy
  • Thank you very much for your advice !!! Sorry for the audacity, but could throw the script (PowerShell) as for Telnet? - Alex_Kot
  • It's my pleasure! Unfortunately I can not. I am not an expert in PowerShell, and even now it is not at hand. Just before the ports had to deal with. Try asking another question. - velikodniy

Through WMI you can check if the COM port is open

WMI Explorer - This is what can help for WMI. It is important to understand that WMI is available only under certain conditions.

WMI example

It is worth looking in the direction of \\.\ROOT\WMI\ms_409:MSSerial_CommInfo there is an IsBusy parameter.

 IsBusy - Boolean Qualifiers: CIMTYPE, Description, DisplayName The IsBusy property determines whether the serial port is busy 
  • Thanks I will try !!! - Alex_Kot