CreateFile Windows COM1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sevak316
    New Member
    • Sep 2008
    • 73

    CreateFile Windows COM1

    Trying to open COM1 in XP using CreateFile and I keep getting INVALID_HANDLE_ VALUE.

    here is the code I am using:

    //main
    .............
    .....
    ....
    ..
    .
    Code:
    HANDLE serialhCom = CreateFile("COM1",            
                           GENERIC_READ | GENERIC_WRITE,
                           0,                    
                           NULL,                 
                           OPEN_EXISTING,        
                           FILE_FLAG_OVERLAPPED, 
                           NULL);                
          
         
         if (serialhCom != INVALID_HANDLE_VALUE) 
         {
           SetupComm(serialhCom, inLen, outLen);
           serialSetTimeouts(serialhCom, MAXDWORD, 0, 0, 0, 0);
         }
         else
         {
           printf ("Could not open COM1\n");
         }
    .
    ..
    ...
    ......
    .............
    I used GetLastError to get more details. and I received error # 123?

    I have searched Google and looked at MSDN and could not info to help me solve the problem. There are dozens of articles explaining CreateFile and how one should use it, but no real substance. I have checked my device manager and my COM1 is active with no conflicts. I have checked my registry and have confirmed my COM1 in memory.

    Please advise.

    Thanks,
    Sevak
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Try adding a colon to the filename: "COM1:".

    Comment

    • sevak316
      New Member
      • Sep 2008
      • 73

      #3
      no good. I have even tried (recommended by some) ""////.//COM1" :(

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Error 123... you can look up error code in winerror.h where you would find

        Originally posted by winerror.h
        //
        // MessageId: ERROR_INVALID_N AME
        //
        // MessageText:
        //
        // The filename, directory name, or volume label syntax
        // is incorrect.
        //
        #define ERROR_INVALID_N AME 123L // dderror
        You may have tried "////.//COM1" but did you try it with the correct slash "\\\\.\\COM 1"?

        Actually I have to say that "COM1" has always worked for me I assume you computer has a COM1?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Oh and 1 final thought, COM1 isn't already in use by some other program is it? Only one process can have access to the COM port at a time (in fact I would question if you can have more than 1 handle open to the COM port).

          Comment

          • sevak316
            New Member
            • Sep 2008
            • 73

            #6
            weird!

            I tried the following:

            1. char str[8];
            //port was passed in through the function parameter
            sprintf(str, "COM%d", port);

            // "str" was used as the first parameter in CreateFile
            2. "COM1"
            3. "com1"
            4. "COM1:"
            5. "\\\\.\\COM 1"

            and finally....

            6. "TEXT("COM1 ") which is the one that finally went through and did not give me an "INVALID_HANDLE _VALUE".

            All of these methods (2,3,4,5) were found on google. The first method was working on Borland C and did not have any problems. I moved the same file which compiled on my other machine to the new computer that has Visual Studio 2008 and that's when the problem started.

            I havent tried to see if my program spits anything out of the serial port yet, but at least I am passing through the section which was failing before.

            I do have COM1 and no other program uses it. I have tried COM1 on windows Terminal software and it does communicate with my other computer.

            -Sevak

            Comment

            • sevak316
              New Member
              • Sep 2008
              • 73

              #7
              just for kicks:

              I tried to open COM1 with 2 different handles and the error code for that is "5"

              which is "ERROR_ACCESS_D ENIED"

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by sevak316
                and finally....

                6. "TEXT("COM1 ")
                Damn, sorry yes that would be required to turn it into a mu;tibyte string which is now the default for windows.

                Comment

                • sevak316
                  New Member
                  • Sep 2008
                  • 73

                  #9
                  I am not sure what that is? (multibyte)

                  Oh and by the way, it works. I am getting data out of COM1.

                  My question now is, how do I make this dynamic. Can I pass a var to TEXT()?
                  It doesn't look like I can because it takes in a "quote"

                  How can I do this?

                  -Sevak

                  Comment

                  • ianinini
                    New Member
                    • Mar 2009
                    • 2

                    #10
                    Dynamic use of the TEXT script

                    I have the same question:

                    Does anyone know how to generate the output of TEXT("COM1") in a dynamic way?

                    Thanks,
                    Ian.

                    Comment

                    • bmillerqw
                      New Member
                      • May 2009
                      • 1

                      #11
                      Originally posted by ianinini
                      I have the same question:

                      Does anyone know how to generate the output of TEXT("COM1") in a dynamic way?

                      Thanks,
                      Ian.

                      You shouldn't have to. You could use the simpler macro _T("COM1") to achive the same results. This way if the application is compiled using UNICODE then the string expands to unicode else it remains a regular ASCII string.

                      If you are compiling an ASCII application and wish to create a UNICODE (wide) string I use MultiByteToWide Char(), try this function:

                      Code:
                      // create a WIDE character string from an ASCII string
                      // wlen is the size of the WIDE character buffer -- it must be at least 2*(strlen(src)+1)
                      BOOL wstrcpy(LPWSTR wdest, LPCSTR src, int wlen)
                      {
                      	return (MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,src, strlen(src)+1, wdest, wlen) != 0);
                      }
                      
                      // I use it as follows
                      
                      WCHAR wszString[100]; 
                      wstrcpy(wszString, "ascii string here", 90);

                      Comment

                      • ianinini
                        New Member
                        • Mar 2009
                        • 2

                        #12
                        Thanks

                        Thanks bmillerqw. I've actually managed to make something work using MultiByteToWide Char().

                        I don't think you can use TEXT() or_T() when for example "COM1" is generated at
                        std::string s("COM1");
                        TEXT(s.str());

                        However the MultiByteToWide Char() type methods work fine!

                        Thanks again!

                        Comment

                        Working...