Вопрос

Can the Windows COM port be used by two programs running on the same PC for inter process communication? Our ultimate goal is to write a program which communicates over COM port to control a hardware. Since the hardware is not available we want to emulate the hardware in a program which reads from a virtual COM port and responds.

A trivial emulator would be a program which waits for bytes to be read from the COM port and responds by writing on to the COM port. To start with the values of these messages can be hardcoded.

So what I do is I have two programs written in C/C++ using the ReadFile, WriteFile functions given by windows to read and write over the file. This approach will not work because of access conflict. Hence I was wondering if our approach for the emulator itself is wrong.

I am new to Windows based programming though I have programming experience in other areas. Any help would be greatly appreciated.

Update:

Solution:

  • Installed Virtual Serial Port Driver 7.1 by Eltima Software (trial version)
  • Added a pair of virtual ports (e.g. COM1 and COM2)
  • The controller program communicates through COM1
  • The emulator program communicates through COM2
  • Eltima software takes care of transferring the data between the two ports
Это было полезно?

Решение

You can open a file with sharing parameters that allow both of the processes to read/write to it. But that's not a good solution, because reading/writing serial port has a number of differences with reading and writing to a file.

Instead I recommend to get a virtual COM port driver (like com0com.sourceforge.net, or select another from here en.wikipedia.org/wiki/COM_port_redirector). Install it, create two COM ports and connect them virtually. That way you will be sure that your program will work on real COM port too.

If that solution is not acceptable look into using pipes rather than a file

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top