简体   繁体   English

串口通讯-PuTTY仿真

[英]Serial Port communication - PuTTY emulation

I am writing a simple user interface to communicate with an In-Circuit Serial Programmer. 我正在编写一个简单的用户界面,以与在线串行编程器通信。 The intention is to remove the need for the end-user to type over a dozen cryptic commands via PuTTY, and in fact to remove the need for typing altogether as the user is inevitably wearing keyboard-unfriendly gloves. 目的是消除最终用户通过PuTTY键入一打以上的密码命令的需要,并且实际上消除了完全键入的需要,因为用户不可避免地戴着键盘不友好的手套。 The process requires interaction with the user, so a simple batch script is not feasible. 该过程需要与用户进行交互,因此简单的批处理脚本不可行。

I can find the correct COM port and successfully open it. 我可以找到正确的COM端口并成功打开它。 I can send data, but the response is only ever the equivalent of "unknown command". 我可以发送数据,但响应仅相当于“未知命令”。

I shall refrain from posting the whole code as nobody will be able to recreate my circumstances. 我将避免发布整个代码,因为没有人能够重现我的情况。 However, I can always add everything if necessary. 但是,如有必要,我总是可以添加所有内容。

I open comms using CreateFile() and use WriteFile() or ReadFile() to communicate. 我使用CreateFile()打开通讯,并使用WriteFile()ReadFile()进行通信。 For example: 例如:

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] << "  ";
}

No matter what message I send (either "r rc.all" or "foobar") I always get the same response: 无论我发送什么消息(“ r rc.all”或“ foobar”),我总是得到相同的响应:

Bytes rcvd = 3
62  13  10

Which is >\\r\\n . 这是>\\r\\n I have tried slowing down the sending of characters to simulate them being typed, but this invokes the same response from the ICSP: 我尝试减慢字符的发送速度以模拟正在键入的字符,但这会调用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;
}

I have set the parameters of the serial connection to match the settings in PuTTY - Byte length, stop bit, parity, flow control, etc. The fact that I get a response at all suggests the connections is not at fault. 我已经设置了串行连接的参数以匹配PuTTY中的设置-字节长度,停止位,奇偶校验,流控制等。我得到响应的事实表明连接没有问题。

What is wrong? 怎么了?

The problem turned out to be the \\r\\n combination sent at the end of the message. 问题出在邮件末尾是\\r\\n组合。

Sending just \\r or just \\n does not work. 仅发送\\r或仅发送\\n无效。 However, sending (char) 13 does - even though that should be the same as \\r . 但是,发送(char) 13可以-即使应该与\\r相同。

There also needs to be a pause between the sending of each character; 每个字符的发送之间也需要暂停。 1ms is sufficient. 1ms就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM