Arduino Serial Connection in Ported Program from Windows

I'm porting a C++ program from Windows Visual Studio 2010 to Ubuntu 12.04. It uses this library on Windows to communicate with an Arduino:

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

I've eliminated almost all Windows-specific function calls in my main program, except this one:

1
2
3
4
5
// read a raw byte
    if (SP->ReadData(incomingData + readResult,1) == 1)
        readResult++;
    else
        continue;


Right now, I'm trying to rewrite the ReadData function to be Linux Compatable. Here it is in the Windows format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int Serial::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;

//Use the ClearCommError function to get status info on the Serial port
ClearCommError(this->hSerial, &this->errors, &this->status);
//Check if there is something to read
if(this->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(this->status.cbInQue>nbChar)
    {
        toRead = nbChar;
    }
    else
    {
        toRead = this->status.cbInQue;
    }
    //Try to read the require number of chars, and return the number of read bytes on success
    if(ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0)
    {
        return bytesRead;
    }
}
//If nothing has been read, or that an error was detected return -1
return -1;
}


How can I replace the calls to ClearCommError and ReadFiles in Linux? Thanks! Using a library or different function would also work great.

Here is the original function in full (Windows):
http://pastebin.com/T1TYkvEh

And here's the one that I've been trying to rewrite for Linux:
http://pastebin.com/TwnSpdSb
Last edited on
I think your problem might be with the value of readResult. You initialize it to zero, then test for readResult - 1, which is 0xFFFFFFFF (-1).

1
2
3
readResult = 0;
...
while (readResult <= 0 || incomingData[readResult-1] != '\n')
Topic archived. No new replies allowed.