Serial Communication - Creating Asynchronous IO

Hello and thank you all!

I'm attempting to open serial communication between my computer and an Arduino Uno microcontroller.

I was hoping whether someone could provide an explanation (or a link to an explanation) for multi-threaded processes and event-driven programming. It has to be for complete noobs. I need to know the following things:
- What an event is.
- How to monitor for an event
- How to create a thread
- Explanation of what the Overlapped structure is
- What waitForSingleObject does

Of course I've consulted the MSDN docs.

I've also been following this tutorial: http://www.codeproject.com/Articles/2682/Serial-Communication-in-Windows

It is rather good but it whizes past all this stuff because I suppose it assumes one already knows.

All I want to do (at the superficial level) is set an "actionListener" only once which will call a function to process my data when data arrives. (I am an not newbie but still beginner C++ programmer coming from java)
Last edited on
I attempted to do communication without setting up a new thread and event but I am still experiencing an error. I send one byte of data (an '8') but my program prints two "8"'s.
Here are the applicable parts of my program:

1
2
3
4
5
6
7
8
9
//Arduino Side
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.write("8");
  delay(1000);
}


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
33
34
35
36
37
38
39
40
    //Buffer
    BYTE byte;

    //Time structure. Set to return even when a character isn't present
    timeOutsStruct.ReadIntervalTimeout = MAXDWORD;
    timeOutsStruct.ReadTotalTimeoutMultiplier = 0;
    timeOutsStruct.ReadTotalTimeoutConstant = 0;
    timeOutsStruct.WriteTotalTimeoutMultiplier = 0;
    timeOutsStruct.WriteTotalTimeoutConstant = 0;

bool Port::readPort()
{
    if(ReadFile(hSerial, &byte, sizeof(byte), &bytesRead, NULL))
        {
            cout << "Read Successful\n";
            return true;
        }
            cout << "Read Failed\n";
            cout << GetLastError();
            return false;
}

int main()
{
    Port myPort;
    DWORD event;

    while(true)
        {
            SetCommMask(myPort.hSerial, EV_RXCHAR);
            WaitCommEvent(myPort.hSerial, &event, NULL);
            if(myPort.readPort())
            {
                cout << myPort.byte << "\n";
            }
        }

    return 0;
}


Output
Read Successful
8
Read Successful
8

(One second later)
Read Successful
8
Read Successful
8

(One second later)
Repeat
Last edited on
Topic archived. No new replies allowed.