0
votes

Ok, I got a problem. I'm programming something that uses the microsoft flight simulator X SDK and Arduino. The application is supposed to send data over Serial port to the arduino board, and I am using this functions:

http://playground.arduino.cc/Interfacing/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. This is the way I call the function WriteData:

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

Being cadena the string I'm sending. The function WriteData is this:

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 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? 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...

1

1 Answers

0
votes

WriteFile() will stall when the transmit buffer is filled to capacity. 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. Or by disabling handshaking, SetCommState() function. Setting timeouts with SetCommTimeouts() is another decent strategy to add some minimal error recovery.

Not reading the data sent by the Arduino doesn't score a lot of points either. 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.