簡體   English   中英

串口通訊-PuTTY仿真

[英]Serial Port communication - PuTTY emulation

我正在編寫一個簡單的用戶界面,以與在線串行編程器通信。 目的是消除最終用戶通過PuTTY鍵入一打以上的密碼命令的需要,並且實際上消除了完全鍵入的需要,因為用戶不可避免地戴着鍵盤不友好的手套。 該過程需要與用戶進行交互,因此簡單的批處理腳本不可行。

我可以找到正確的COM端口並成功打開它。 我可以發送數據,但響應僅相當於“未知命令”。

我將避免發布整個代碼,因為沒有人能夠重現我的情況。 但是,如有必要,我總是可以添加所有內容。

我使用CreateFile()打開通訊,並使用WriteFile()ReadFile()進行通信。 例如:

if (!WriteFile(hSerial, "r rc.all\r\n", 10, &bytesRead, NULL))
    cout << "Error sending message (" << GetLastError() << ")" << endl;
if (!ReadFile(hSerial, msgBuffer, 15, &bytesRead, NULL))
    cout << "No message received" << endl
else
{
    cout << "Bytes rcvd = " << bytesRead << endl;
    for (int x=0; x<bytesRead; x++)
        cout << (unsigned int) msgBuffer[x] << "  ";
}

無論我發送什么消息(“ r rc.all”或“ foobar”),我總是得到相同的響應:

Bytes rcvd = 3
62  13  10

這是>\\r\\n 我嘗試減慢字符的發送速度以模擬正在鍵入的字符,但這會調用ICSP的相同響應:

bool serialSend(LPCSTR MESSAGE, PHANDLE hSERIAL)
{
    DWORD   bytesWritten;
    char    writeBuff[2];    

    writeBuff[1] = '\0';

    for (UINT x = 0; x <= strnlen(MESSAGE, 64); x++)
    {
        cout << MESSAGE[x];
        writeBuff[0] = MESSAGE[x];
        if (!WriteFile(*hSERIAL, writeBuff, 1, &bytesWritten, NULL))
            cout << "\t\tERROR! (character '" << MESSAGE[x] << "', error " << GetLastError() << ")" << endl;
        Sleep(100);
    }

    writeBuff[0] = '\n';
    if (!WriteFile(*hSERIAL, writeBuff, 1, &bytesWritten, NULL))
        cout << "\t\tERROR! (character 'LF', error " << GetLastError() << ")" << endl;
    Sleep(100);
    writeBuff[0] = '\r';
    if (!WriteFile(*hSERIAL, writeBuff, 1, &bytesWritten, NULL))
        cout << "\t\tERROR! (character 'CR', error " << GetLastError() << ")" << endl;

    cout << endl;

    return true;
}

我已經設置了串行連接的參數以匹配PuTTY中的設置-字節長度,停止位,奇偶校驗,流控制等。我得到響應的事實表明連接沒有問題。

怎么了?

問題出在郵件末尾是\\r\\n組合。

僅發送\\r或僅發送\\n無效。 但是,發送(char) 13可以-即使應該與\\r相同。

每個字符的發送之間也需要暫停。 1ms就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM