Windows和嵌入式系统(STM32)(C / C ++)之间的串行通信数据问题

时间:2019-01-17 11:29:17

标签: windows serial-port microcontroller stm32 serial-communication

我目前正在尝试在Windows程序和µC之间建立通信。 我将向您展示初始化端口的代码:

int serialCommunication::serialInit(void){

//non overlapped communication
hComm = CreateFile( gszPort.c_str(),
                    GENERIC_READ | GENERIC_WRITE,
                    0,
                    0,
                    OPEN_EXISTING,
                    0,
                    0);

if (hComm == INVALID_HANDLE_VALUE){
    cout << "Error opening port." << endl;
    return 0;
}
else{
    cout << "Opened Port successfully." << endl;
}

if (SetCommMask(hComm, EV_RXCHAR) == FALSE){
    cout << "Error setting communications mask." << endl;
    return 0;
}
else{
    SetCommMask(hComm, EV_RXCHAR);
    cout << "Communications mask set successfully." << endl;
}

if (GetCommState(hComm, &dcbSerialParams) == FALSE){
    cout << "Error getting CommState." << endl;
    return 0;
}
else{
    GetCommState(hComm, &dcbSerialParams);
    cout << "CommState retrieved successfully" << endl;
}

dcbSerialParams.BaudRate = CBR_115200;    // Setting BaudRate = 115200
dcbSerialParams.ByteSize = 8;             // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT;    // Setting StopBits = 1
dcbSerialParams.Parity = NOPARITY;        // Setting Parity = None

if (SetCommState(hComm, &dcbSerialParams) == FALSE){
    cout << "Error setting CommState" << endl;
    return 0;
}
else{
    SetCommState(hComm, &dcbSerialParams);
    cout << "CommState set successfully" << endl << endl;
    cout << "+---CommState Parameters---+" << endl;
    cout << "Baudrate = " << dcbSerialParams.BaudRate << endl;
    cout << "ByteSize = " << static_cast<int>(dcbSerialParams.ByteSize) << endl; //static Cast, um int auszugeben und kein char
    cout << "StopBits = " << static_cast<int>(dcbSerialParams.StopBits) << endl; //static Cast, um int auszugeben und kein char
    cout << "Parity = " << static_cast<int>(dcbSerialParams.Parity) << endl; //static Cast, um int auszugeben und kein char
    cout << "+--------------------------+" << endl;
}

/*------------------------------------ Setting Timeouts --------------------------------------------------*/

timeouts.ReadIntervalTimeout         = 50;
timeouts.ReadTotalTimeoutConstant    = 50;
timeouts.ReadTotalTimeoutMultiplier  = 10;
timeouts.WriteTotalTimeoutConstant   = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;

if (SetCommTimeouts(hComm, &timeouts) == FALSE){
    cout << "Error setting timeouts" << endl;
    return 0;
}
else{
    SetCommTimeouts(hComm, &timeouts);
    cout << "Timeouts set successfully." << endl;
    cout << "+--------------------------+" << endl;
    return 1;
}

“我的阅读”功能如下:

void serialCommunication::serialRead(void){
bool readStatus;
bool purgeStatus = 0;
bool correctData = 0;

cout << "Waiting for Data..." << endl; // Programm waits and blocks Port (like Polling)
readStatus = WaitCommEvent(hComm, &dwEventMask, 0);
if (readStatus == FALSE){
    cout << "Error in setting WaitCommEvent." << endl;
}
else{
    cout << "Data received." << endl;
    do{
        readStatus = ReadFile(hComm, &TempChar, sizeof(TempChar), &NoBytesRead, 0);
        SerialBuffer += TempChar; // add tempchar to the string
    }while (NoBytesRead > 0);
SerialBuffer.pop_back(); // Delete last sign in buffer, otherwise one "0" too much shows up, for example "23900" instead of "2390"
cout << endl << SerialBuffer << endl;
SerialBuffer = ""; // Reset string
}

因此,在某些情况下,我的µC在初始化某些内容后会发送字符串“ Init complete ...!\ r \ n”。这很好用。Init complete proof

此后,通信产生错误。我正在获取我不应该收到的数据。如果PC发送了特定的字符串,则µC只能发送数据。在调试时,我可以检测到µC从未接收到此特定的字符串,因此从不发送数据。在下面的图片中,我向您展示了我不断收到的胡言乱语。 Receiving Gibberish / EDIT:我经常收到相同的胡言乱语

有趣的是,当µC完全关闭时(即使仍连接串行电缆),我什至收到了这些数据。因此,端口上必须有一些数据,只是不会被删除。我也尝试重新启动PC,但这也无济于事。

我还将向您展示我在PC上的while循环:

while (testAbbruch != 1){
    pointer = acMessung(anzahlMessungen, average); // measurement with external multimeter
    cout << endl;
    cout << "Average: " << average << endl << endl;

    if (average >= 30){
        testAbbruch = 1; // there won't be a next while iteration
        befehl = "stopCalibration\r\n";
        serialTest.serialWrite(befehl);
        serialTest.serialRead();
    }
    else{
        cout << "Aktion: ";
        std::getline (cin, befehl);
        befehl = "increment"; //for debugging

        if (befehl == "increment"){
            befehl.append("\r\n"); // adding it, so the µC can detect the string correctly
            serialTest.serialWrite(befehl);
            serialTest.serialRead(); // µC has to answer
        }
        else if(befehl == "decrement"){
            befehl.append("\r\n"); // adding it, so the µC can detect the string correctly
            serialTest.serialWrite(befehl);
            serialTest.serialRead(); // µC has to answer
        }
        befehl = ""; // string leeren für nächsten Aufruf
    }
}

我知道我的程序还远远不够完美,但是如果我正确地理解了与Windows的串行通信,则在读取时将删除缓冲区。 你有什么线索可以给我吗?

EDIT //我刚刚编写了一个程序,该程序期望两个输入之一:一个输入称为“递增”,另一个输入称为“递减”。这些输入通过串行通信端口发送到µC。每当我尝试从端口读取时,立即发送“增量”,然后立即从this picture.接收奇怪的数据。现在,每次尝试发送“递减”时,立即从端口读取端口,我从that picture收到了奇怪的数据。 //

所以我的猜测是数据以某种方式被更改然后又返回到PC?但是为什么以及如何?!

0 个答案:

没有答案