Unable To create COM port in Windows 7 professional 32 bit

Unable to create open COM port 32 by CreatFile() function of "windows.h". It works fine in Windows 7 ultimate 64 bit Windows XP 32 bit but unfortunatily not working on Windows 7 Professional 32 bit. Any one please guide me.

Following is code.
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
#include "ProjectSMS.h" /*Custom header file created by me. It includes windows.h and other standard header files. */
HANDLE hComport;


int ConnectWithModem(void) // Tested OK
{
    char COM[6]="COM";
    char numberOfComport[3];
    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("\nReturning while opening %s\n",COM);
        CloseHandle(hComport);
        return 1;
    }
    DCB dcbSerialParams = {0};
    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

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

        printf("\nReturning while opening device\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 while setcommState\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 while setcommtimeouts\n");
        CloseHandle(hComport);
        return 1;
    }

    return 0;
}

Topic archived. No new replies allowed.