与串行通信的串扰。

Cross talk with Serial communication.

所以我有一个用于基本串行通信对象的库对象。在另一个 c++ 应用程序中,我创建了该对象的实例。一个连接到 com 4,另一个连接到 com 6。向两个不同的设备发送数据时。我看到我从一个串行端口发出的数据出现在另一台设备上。我想看看这是否与我的 CreateFile 设置有关,或者是否有其他原因会导致这种情况发生。我试过更改 createfile 中的设置和选项,但串扰仍然发生。

这是我目前使用的示例代码。如果有人对我可以在哪里查看有任何提示,将不胜感激。

    ser_disconnect();
    //We're not yet connected
    connected = false;

    //std::wstring stemp(L"\\.\COM");
    std::wstring stemp(L"COM");
    stemp = stemp + std::to_wstring(Port);
    LPCWSTR sw = stemp.c_str();
    //Try to connect to the given port throuh CreateFile
    hSerial = CreateFile(sw,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL, 
        CREATE_NEW,//OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    //Check if the connection was successfull
    if (hSerial == INVALID_HANDLE_VALUE)
    {
        //If not success full display an Error
        if (GetLastError() == ERROR_FILE_NOT_FOUND) {
            //Print Error if neccessary
            set_Last_Error("ERROR: Handle was not attached. Reason: Port not available.\n", errMsg);
            return 0x01;
        }
        else
        {
            set_Last_Error("Serial Handel ERROR!!!", errMsg);
            return 0x02;
        }
    }
    else
    {
        //If connected we try to set the comm parameters
        DCB dcbSerialParams = { 0 };

        //Try to get the current
        if (!GetCommState(hSerial, &dcbSerialParams))
        {
            //If impossible, show an error
            set_Last_Error("failed to get current serial parameters!", errMsg);
            return 0x03;
        }
        else
        {
            //Define serial connection parameters for the arduino board
            dcbSerialParams.BaudRate = BaudRate;
            dcbSerialParams.ByteSize = Data;
            dcbSerialParams.StopBits = StopBits;
            dcbSerialParams.Parity = Parity;
            //Setting the DTR to Control_Enable ensures that the Arduino is properly
            //reset upon establishing a connection
            dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;

            //Set the parameters and check for their proper application
            if (!SetCommState(hSerial, &dcbSerialParams))
            {
                set_Last_Error("ALERT: Could not set Serial Port parameters", errMsg);
                return 0x04;
            }
            else
            {
                //If everything went fine we're connected
                connected = true;
                //Flush any remaining characters in the buffers 
                PurgeComm(hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
            }
        }
    }

这似乎是我如何存储串行处理变量的问题。它是 cpp 文件中的全局变量。只要我把它放在 class 的范围内,它就可以正常工作