2

In my scenario I have 4 devices sending serial data to my PC via RS232 connections. I am also using hardware to gather all the 4 data streams into one USB connection.

Now, this setup creates 4 COM ports in Windows and that's my issue because I have a ready-made software that reads from just one serial port.

I'm trying to create a tiny C++ software to grab the packets from my 4 RS232 serial ports and forward the stream into one serial port. Alternating the packets will work for sure. As I figured it out so far I'll need to create a virtual serial port to write the serialized stream and make sure that the ready-made software reads from that. Now I haven't tested this but I assume I'll not be able to do this without a bridge or something since I can't keep the port open to write the serialized data and open it with the ready-made software to read the data.

What I'd do then is

  • Setup a virtual port
  • Setup a bridge to that virtual port
  • Set the ready-made software to read from the copy of the virtual port
  • In my C++ software, open the 4 ports and the virtual one
  • Write my stuff into the virtual port (see underneath COMport_TARGET)

My questions

Considering the code example underneath,

  • Would you do the same way?
  • How else would you do it?
  • What software would you use to setup a virtual port and a bridge?

Here's a naive code that should do this:

unsigned char receiveBuffer[PACKET_LENGTH];

OpenCOMPort(COMport_A);
OpenCOMPort(COMport_B);
OpenCOMPort(COMport_C);
OpenCOMPort(COMport_D);

OpenCOMPort(COMport_TARGET); // This is the virtual COM Port to write to

while(true) {
    packetReadFromDevice(COMport_A);
    packetWriteToDevice(*(receiveBuffer), COMport_TARGET); // Write COM A data
    packetReadFromDevice(COMport_B);
    packetWriteToDevice(*(receiveBuffer), COMport_TARGET); // Write COM B data
    packetReadFromDevice(COMport_C);
    packetWriteToDevice(*(receiveBuffer), COMport_TARGET); // Write COM C data
    packetReadFromDevice(COMport_D);
    packetWriteToDevice(*(receiveBuffer), COMport_TARGET); // Write COM D data
    if(somethingHappens)
        break;
}

closeCOMPort(COMport_A);
closeCOMPort(COMport_B);
closeCOMPort(COMport_C);
closeCOMPort(COMport_D);

closeCOMPort(COMport_TARGET);
4

0 回答 0