#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20

    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 the code.
    The code works fine in other Computer.

    Code:
    #include "ProjectSMS.h" /* Custom header file created by me.It has windows.h and other standard headers. */
    
    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;
    }
    
    int DissconnectWithModem(void) //Tested OK
    {
        if(CloseHandle(hComport)) {
            return 0;
        }
        else {
            return 1;
        }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    What error messages are you getting? Are you sure that the Com port you're trying to open exists?

    Where and how is comportNumber defined?

    Jim
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Yes. I have conformed the port number is "COM32"(Conformed through Device Manager of Windows).
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Error I am getting "ERROR_FILE_NOT_FOUND"
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,378
    Rep Power
    1871
    > result = GetLastError();
    Why don't you do this for ALL the API calls that you make?

    Why don't you print out the actual error code you see?

    The system can give you a lot of information, but you need to write the code to get it, and improve your understanding of why it's failing.

    WHY don't you tell us which of the many exit paths in the code snippet you posted is actually taken!?

    Even if we were able, willing and curious enough to try your code, the fact that it works on 2 out of 3 systems means that the most likely outcome is going to be "well it works for me", which doesn't do you any good at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Here...was some thing I have edited.
    Last edited by jaysinhp; June 3rd, 2013 at 01:41 AM.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Originally Posted by salem
    >
    The system can give you a lot of information, but you need to write the code to get it, and improve your understanding of why it's failing.
    Is there any other way to get error from system ? If so suggest me.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Finally Got The Answer. I found below line from the documentation of CreateFile on MSDN.com.

    To specify a COM port number greater than 9, use the following syntax: "\\.\COM10". This syntax works for all port numbers and hardware that allows COM port numbers to be specified.

IMN logo majestic logo threadwatch logo seochat tools logo