My program can't detect when the device from COM port is disconnected.

Dear Friends, I'm writing one program in C which is communicating to Virtual Comport to send the commands to USB modem(to send SMS by AT commands). It works fine when the device is connected with the computer.But it is unable to handle the errors when someone dissconnect the device from the computer. I mean when there is no device on COM port the program is not indicating any errors not even notifications(May be i don't know how to get notifications !). Here my code indicates clear error befor connecting to the device. If it is not connected I am getting error. But after taking HANDLE by creating file(CreateFile function) When the device is disconnected(Forcly removed from the computer) I am unable to find the error. Even when I check from GetLastError() it gives me no error. Kindly suggest me what to do ?
Following is the file in which I am dealing with COM port functions.

Don't confuse yourself with "ProjectSMS.h". It is header created by me. And the "windows.h" is included in that header.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "ProjectSMS.h"
HANDLE hComport;

int ConnectWithModem(void) // Tested OK
{
    char COM[]="COM";
    char numberOfComport[2];
    unsigned long result;

    itoa(comportNumber,numberOfComport,10);
    strcat(COM,numberOfComport);



    hComport = CreateFile(COM,
                        GENERIC_READ | GENERIC_WRITE,
                        0,
                        0,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        0);

    result = GetLastError();

    if(hComport == INVALID_HANDLE_VALUE ||
       result == ERROR_FILE_NOT_FOUND ||
       result == ERROR_DEVICE_NOT_CONNECTED) {

        printf("%lu",result);
        printf("\nReturning from first if\n");
        CloseHandle(hComport);

        return 1;
    }

    DCB dcbSerialParams = {0};

    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

    if(!GetCommState(hComport, &dcbSerialParams)) {

        printf("\nReturning from second if\n");
        CloseHandle(hComport);

        return 1;
    }

    dcbSerialParams.BaudRate = CBR_9600; // This has to be programmed for user input.
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;

    if(!SetCommState(hComport, & dcbSerialParams)) {

        printf("\nReturning from third if\n");
        CloseHandle(hComport);

        return 1;
    }

    COMMTIMEOUTS timeouts = {0};

    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 10;

    if(!SetCommTimeouts(hComport, &timeouts)) {

        printf("\nReturning from third if\n");
        CloseHandle(hComport);

        return 1;
    }

    return 0;
}

int DissconnectWithModem(void) //Tested OK
{
    if(CloseHandle(hComport)) {
        return 0;
    }
    else {
        return 1;
    }
}

int SendDataToModem(const char * data) //Tested OK
{
    int lengthOfData = strlen(data);

    DWORD dwBytesRead = 0;

    if(!WriteFile(hComport,data,lengthOfData, &dwBytesRead,NULL)) {

       CloseHandle(hComport);
        return 1;
    }

    printf("LAST ERROR While Sending : %lu",GetLastError());
    getch();

    return 0;
}

int ReceiveDataFromComport(char *receivedData, int numberOfCharacter) //Tested OK but Confusing in size of the character
{
    DWORD dwBytesRead = 0;
    if(!ReadFile( hComport, receivedData, numberOfCharacter, &dwBytesRead,NULL)) {

        CloseHandle(hComport);
        return 1;
    }

    printf("LAST ERROR While Receiving : %lu",GetLastError());
    getch();

    return 0;
}
Have you had any luck elsewhere?

In particular, did you find this thread?

Distinguishing device disconnects from timeouts
http://stackoverflow.com/questions/6748207/distinguishing-device-disconnects-from-timeouts

Andy
Topic archived. No new replies allowed.