Search code examples
c++serial-communication

Cross talk with Serial communication.


So i have a lib object that I am using for a base serial communication object. In the other c++ application I create to instances of that object. One connect to com 4 and the other to com 6. When sending data to two different devices. I am seeing the data that i send out of one serial port appear on the other device. I am trying to see if this has something to do with my CreateFile settings or is there something else that can cause this to happen. I've tried changing settings and options in the createfile but the cross-talk still happens.

Here is the sample code I'm currently using. If anyone has any tips on where I can look it would be much appreciated.

    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);
            }
        }
    }

Solution

  • It seemed to be an issue with how i had the variable for the serial handled stored. It was a global var inside the cpp file. As soon as i put this within the realm of the class it worked just fine