简体   繁体   English

串口通讯Arduino,C ++

[英]Serial port communication Arduino, C++

Ok, I got a problem. 好的,我遇到了问题。 I'm programming something that uses the microsoft flight simulator X SDK and Arduino. 我正在编写使用Microsoft Flight Simulator X SDK和Arduino的程序。 The application is supposed to send data over Serial port to the arduino board, and I am using this functions: 该应用程序应该通过串行端口将数据发送到arduino板,而我正在使用以下功能:

http://playground.arduino.cc/Interfacing/CPPWindows http://playground.arduino.cc/Interface/CPPWindows

The program works perfectly except that it suddenly stops working. 该程序可以正常运行,只是它突然停止工作。 The program is a loop while(1) that continously executes a function that asks for data to the simulator, and then sends a string through serial. 该程序是一个while(1)循环,它连续执行向模拟器请求数据的功能,然后通过串行发送字符串。 This is the way I call the function WriteData: 这就是我调用函数WriteData的方式:

WriteData((char *)cadena.c_str(),8);

Being cadena the string I'm sending. 作为cadena ,我正在发送的字符串。 The function WriteData is this: 函数WriteData是这样的:

bool Serial::WriteData(char *buffer, unsigned int nbChar)
{
    DWORD bytesSend;
    //Try to write the buffer on the Serial port
    if(!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
    {
        //In case it don't work get comm error and return false
        ClearCommError(this->hSerial, &this->errors, &this->status);

        return false;
    }
    else
        return true;
    }

I've seen that if I comment the whole if-else , so the function WriteFile is never called, the program doesn't stop working and goes on perfectly (except for the fact that the information isn't sent to the Arduino). 我已经看到,如果我对整个if-else注释,那么就不会调用函数WriteFile ,该程序不会停止工作并可以完美运行(除了信息未发送到Arduino的事实)。 If that line is executed, then the program stops after a minute or so. 如果执行了该行,则程序将在一分钟左右后停止。 And by stopping I don't mean crashing or anything, I just mean it stops,, the console is still there with all the messages, it just stops working. 而且停止不意味着崩溃或任何事情,我只是意味着它停止了,控制台仍然在其中存储所有消息,只是停止了工作。

What could be going on? 可能会发生什么?

EDIT: Ok, The arduino was also sending data continously that was never read by the program, could that be a problem? 编辑:好的,arduino还连续发送程序从未读取过的数据,这可能是一个问题吗? Could it be that the buffer was full and WriteFile was waiting for space to write on it?, because know that I don't write to the serial, it seems to work just fine... 可能是缓冲区已满,而WriteFile正在等待在其上写空间吗?,因为知道我没有写序列,所以看起来工作得很好...

WriteFile() will stall when the transmit buffer is filled to capacity. 当发送缓冲区已满时,WriteFile()将停止。 It will only empty when you take care of setting up the handshaking correctly. 仅当您注意正确设置握手时,它才会为空。 Either electrically, wiring the DSR and CTS signals, which tends to be skipped on an Arduino. 无论是电气上,还是连接DSR和CTS信号,这在Arduino上往往会被跳过。 Or by disabling handshaking, SetCommState() function. 或通过禁用握手SetCommState()函数。 Setting timeouts with SetCommTimeouts() is another decent strategy to add some minimal error recovery. 使用SetCommTimeouts()设置超时是增加一些最小错误恢复的另一种不错的策略。

Not reading the data sent by the Arduino doesn't score a lot of points either. 不读取Arduino发送的数据也不会得分很多。 It is certainly best to implement this incrementally so you can focus on solving one problem at a time. 当然,最好以增量方式实施此操作,以便您一次可以专注于解决一个问题。 It however doesn't normally prevent the PC from transmitting data, modulo a handshake wiring mistake. 但是,它通常不会阻止PC发送数据,以握手接线错误为模。

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

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