multithreading with timer in visual C++

hi,
I use class form visual c++ 2010 for controling robot and receive information from sensor in the robot.

I want to send the command every 0,05 mikrosecond and read the sensor every 1 second. and I want to do it in the same time.

the command I send I use keyboard, if I press w the robot will move forward and s will move backward. the problem is, I do not know/I am not sure, if I press the 'w' for a long time, the reading of the sensor will be done by the program properly.

here is my program

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const int cTimer1 = 1;
const int cTimer2 = 1;

 void CENVSConfigDlg::OnBnClickedButton1()
{
SetTimer(cTimer1,1000,NULL);
}

 void CENVSConfigDlg::OnBnClickedButton2()
{
KillTimer(cTimer1);
}

 void CENVSConfigDlg::OnBnClickedButton3()
{   
SetTimer(cTimer2,10,NULL);  
}

void CENVSConfigDlg::OnBnClickedButton4()
{
    KillTimer(cTimer2);
}

void CENVSConfigDlg::OnTimer(UINT_PTR ID){
if(ID==cTimer1){

    char buffer[30],tempStr[5];
    int sensor[6];

    DWORD nbytes;

    MessageBeep(0);

    //Read Sensors

    if(!WriteFile( hnd_serial, "1", 1, &nbytes, NULL )){KillTimer(cTimer1);MessageBox(L"Write Com Port fail!");return;}
    Sleep(30);
    if(!ReadFile( hnd_serial, buffer, 30, &nbytes, NULL )){KillTimer(cTimer1);MessageBox(L"Read Com Port fail!");return;}
    Sleep(300);



if(ID==cTimer2)
{
    if(GetAsyncKeyState(0x53) != 0)
 {
 DWORD nbytes;
if(!WriteFile( hnd_serial, "s", 1, &nbytes, NULL )){MessageBox(L"Write Com Port fail!");return;}
    Sleep(30);
 tampil.SetWindowText( (LPCTSTR)"s" ); //backward
 }

else if(GetAsyncKeyState(0x57) != 0)
{
    DWORD nbytes;
if(!WriteFile( hnd_serial, "w", 1, &nbytes, NULL )){MessageBox(L"Write Com Port fail!");return;}
    Sleep(30);
    tampil.SetWindowText( (LPCTSTR)"w" );//forward
}


}

}


}
Topic archived. No new replies allowed.