3

So that's what I want to do. I already have some functions, for example this one to write data to the serial port, which works perfectly:

bool WriteData(char *buffer, unsigned int nbChar)
{
    DWORD bytesSend;

    //Try to write the buffer on the Serial port
    if(!WriteFile(hSerial, (void *)buffer, nbChar, &bytesSend, 0))
    {
        return false;
    }
    else
        return true;
}

The reading function is like this:

int ReadData(char *buffer, unsigned int nbChar)
{
//Number of bytes we'll have read
DWORD bytesRead;
//Number of bytes we'll really ask to read
unsigned int toRead;

ClearCommError(hSerial, NULL, &status);
//Check if there is something to read
if(status.cbInQue>0)
{
    //If there is we check if there is enough data to read the required number
    //of characters, if not we'll read only the available characters to prevent
    //locking of the application.
    if(status.cbInQue>nbChar)
    {
        toRead = nbChar;
    }
    else
    {
        toRead = status.cbInQue;
    }

    //Try to read the require number of chars, and return the number of read bytes on success
    if(ReadFile(hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0)
    {
        return bytesRead;
    }

}

//If nothing has been read, or that an error was detected return -1
return -1;

}

And no matter what I do with the arduino, this function always returns -1, I even tried loading a code that constantly writes a character to the serial port, but nothing.

I got the functions from here: http://playground.arduino.cc/Interfacing/CPPWindows

so my functions are basically the same. I just copied them into my code instead of using them as classes objects, but more than that it's the same.

So that's my problem, I can write data to the serial but I can't read, what can I try?

7
  • What does your line look like that opens the serial port? And what's in GetLastError? Are you sure the device is actually giving you something to read? Commented May 1, 2013 at 12:30
  • @MatsPetersson hSerial = CreateFile(wText, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); hSerial is a variable defined as HANDLE hSerial. Is that what you mean? Commented May 1, 2013 at 12:32
  • @MatsPetersson I don't know how to isolate where the problem is happening. The device is definitely sending data, if I open the serial monitor of the arduino IDE I can see it. The program can send data too. The problem is with reading it. The variable: status.cbInQue is 0, which should be the number of bytes to read. Commented May 1, 2013 at 12:34
  • You do realize, I suppose that you have ommitted the line that fetches the status in your read function, which probably means that the if-statement checking cbInQue is going to fail? Commented May 1, 2013 at 12:36
  • @MatsPetersson mmm... Ok that's weird. I don't remember at all deleting that line. I now get -1 again, but cbInQue is different from 0 (around 2000, which I guess it's the number of character arduino sends before the line is executed. The variable bytesRead ends up being 0 too. Commented May 1, 2013 at 12:44

1 Answer 1

2

For anyone interested, I already solved it and it was a silly mistake. I programmed the Arduino so it would wait for a serial input before sending anything. The computer program writes and sends one line of code after another, and I guess a i7 is faster than the Atmel... and obviously the data takes some time.

Adding a Sleep(10); before reding the port from the computer was enough to finally read the data.

Thanks to @Matts for his help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.