Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 串口读取文件接收重复数据_C++_Windows_Winapi_Serial Port - Fatal编程技术网

C++ 串口读取文件接收重复数据

C++ 串口读取文件接收重复数据,c++,windows,winapi,serial-port,C++,Windows,Winapi,Serial Port,我正在开发一个多线程程序来读取和写入串行端口。如果我用油灰测试我的应用程序,一切都很好。但是当我用创建的.exe文件测试它时,它不起作用。(我在VS2017中启动程序,然后启动.exe文件) 例如:我的输入:“Test”,另一个窗口中的输出:“teeeeeessssttttt” 发送数据的我的代码: void SendDataToPort() { for (size_t i = 0; i <= strlen(line); i++) // loop through the array unt

我正在开发一个多线程程序来读取和写入串行端口。如果我用油灰测试我的应用程序,一切都很好。但是当我用创建的.exe文件测试它时,它不起作用。(我在VS2017中启动程序,然后启动.exe文件)

例如:我的输入:“Test”,另一个窗口中的输出:“teeeeeessssttttt”

发送数据的我的代码:

void SendDataToPort()
{
for (size_t i = 0; i <= strlen(line); i++) // loop through the array until 
 every letter has been sent
{
    try
    {
        if (i == strlen(line))  // If ever letter has been sent                 
        {                       // end it with a new line
            c = '\n';           // a new line
        }
        else
        {
            c = line[i];
        }
        WriteFile(serialHandle, &c, 1, &dwBytesWrite, NULL); // Send letter to serial port
    }
    catch (const exception&)
    {
        cout << "Error while writing.";
    }
}
cout << endl << "Sent." << endl;
}
int newLineCounter = 0;
unsigned char tmp;
while (!endCurrentRead)
{
    ReadFile(hSerial, &tmp, 1, &bytesRead, NULL); // Get new letter

    if (tmp != '\n')
    {
        newLineCounter = 0;
        if (tmp >= 32 && tmp <= 126)
        {
            output += tmp; // Add current letter to the output
        }
    }
    else
    {
        newLineCounter++;
        if (newLineCounter < 2) // If there are more than 2 '\n' it doesn't write it down
        {
            output += tmp;
        }
        else if (newLineCounter == 2)
        {
            Print(output);
            output = receiverName;
            endCurrentRead = true;
        }
    }
}

为什么会发生这种情况?为什么只有在我使用.exe文件而不是Putty进行测试时才会发生这种情况?

多亏了@quetzalcatl的帮助,我才能够解决这个问题。我必须检查
bytesRead
是否大于0

解决方案:

int newLineCounter = 0;
DWORD dwCommModemStatus;
unsigned char tmp;
while (!endCurrentRead)
{
    ReadFile(hSerial, &tmp, 1, &bytesRead, NULL); // Get new letter
    if (bytesRead > 0)
    {
        if (tmp != '\n')
        {
            newLineCounter = 0;
            if (tmp >= 32 && tmp <= 126)
            {
                output += tmp; // Add current letter to the output
            }
        }
        else
        {
            output += tmp;
            Print(output);
            output = receiverName;
            endCurrentRead = true;
        }
    }
}
int newLineCounter=0;
德沃德·德沃德·德克莫德姆斯塔特斯;
无符号字符tmp;
而(!endCurrentRead)
{
ReadFile(hSerial,&tmp,1,&bytesRead,NULL);//获取新字母
如果(字节读取>0)
{
如果(tmp!='\n')
{
newLineCounter=0;

如果(tmp>=32&&tmp)您没有检查
ReadFile
bytesRead
的返回值,Davis所说的非常重要,因为您可以得到一个返回值,告诉您没有读取任何内容(接收缓冲区为空,我们没有等待新数据)您的代码不会注意到,并且会假设它获得了与以前相同的数据,因此,@quetzalcatl
ReadFile()
返回一个BOOL,而不是一个HRESULT。如果失败,则
GetLastError()返回一个错误代码整数(仍然不是HRESULT)
.before?如果您还没有到邮局去取货,您想如何检查您的箱子是否已满?您能读懂未来吗?调用ReadFile将写入该
bytesRead
变量。您必须事后检查帮助:)很高兴您写了一个后续响应,谢谢!您应该更多地关注ReadFile()失败的原因。在我看来,超时设置得太低了,请查看您的SetCommTimeouts()打电话。或者如果你忘了使用它就添加。@HansPassant我的代码中有超时,但我还没有将它添加到我的问题/答案中。但是谢谢你的建议。
serialHandle = CreateFile(LcomPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
int newLineCounter = 0;
DWORD dwCommModemStatus;
unsigned char tmp;
while (!endCurrentRead)
{
    ReadFile(hSerial, &tmp, 1, &bytesRead, NULL); // Get new letter
    if (bytesRead > 0)
    {
        if (tmp != '\n')
        {
            newLineCounter = 0;
            if (tmp >= 32 && tmp <= 126)
            {
                output += tmp; // Add current letter to the output
            }
        }
        else
        {
            output += tmp;
            Print(output);
            output = receiverName;
            endCurrentRead = true;
        }
    }
}