Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Unable to send AT commands to the COM3 port. plz help.

Unable to send AT commands to the COM3 port. plz help.

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
19 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A AprNgp

    I have configured the COM3 (modem) port with the help of CreateFile() function. But when i m using the WriteFile() function to send the AT command strings to them modem, its not working ... i mean nothing is happening .... plz help... how to send a string to a port ... ?

    Apurv

    P Offline
    P Offline
    Perspx
    wrote on last edited by
    #2

    Does WriteFile() fail or succeed? Regards, --Perspx

    "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

    A 1 Reply Last reply
    0
    • P Perspx

      Does WriteFile() fail or succeed? Regards, --Perspx

      "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

      A Offline
      A Offline
      AprNgp
      wrote on last edited by
      #3

      WriteFile() succeeds ....

      Apurv

      A 1 Reply Last reply
      0
      • A AprNgp

        WriteFile() succeeds ....

        Apurv

        A Offline
        A Offline
        AprNgp
        wrote on last edited by
        #4

        my code is as follows ...

        DCB dcb;
        HANDLE hCom;
        BOOL fSuccess;
        TCHAR *pcCommPort = TEXT("COM3");
        char DataBuffer[] = "AT+FCLASS=8";
        DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
        char Data2[]="ATDW2289759";

        DWORD dwBytesWritten = 0;
        DWORD dwBytesRead = 0;
        char ReadBuffer\[BUFFER\_SIZE\] = {0};
        

        hCom = CreateFile( pcCommPort,
        GENERIC_READ | GENERIC_WRITE,
        0, // must be opened with exclusive-access
        NULL, // default security attributes
        OPEN_EXISTING, // must use OPEN_EXISTING
        0, // not overlapped I/O
        NULL // hTemplate must be NULL for comm devices
        );

        if (hCom == INVALID_HANDLE_VALUE)
        {
        // Handle the error.
        printf ("CreateFile failed with error %d.\n", GetLastError());
        return (1);
        }

        // Build on the current configuration, and skip setting the size
        // of the input and output buffers with SetupComm.

        // SecureZeroMemory(&dcb, sizeof(DCB));
        dcb.DCBlength = sizeof(DCB);
        fSuccess = GetCommState(hCom, &dcb);

        if (!fSuccess)
        {
        // Handle the error.
        printf ("GetCommState failed with error %d.\n", GetLastError());
        return (2);
        }

        dcb.BaudRate = CBR\_2400;     // set the baud rate
        dcb.ByteSize = 8;             // data size, xmit, and rcv
        dcb.Parity = NOPARITY;        // no parity bit
        dcb.StopBits = ONESTOPBIT;    // one stop bit
        

        fSuccess = SetCommState(hCom, &dcb);

        if (!fSuccess)
        {
        // Handle the error.
        printf ("SetCommState failed with error %d.\n", GetLastError());
        return (3);
        }

        _tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);

        //WriteFile(hCom,"AT+FCLASS=8",sizeof("AT+FCLASS=8"),&len,0);
        

        if( FALSE == WriteFile(hCom, // open file handle
        DataBuffer + dwBytesWritten, // start of data to write
        dwBytesToWrite - dwBytesWritten, // number of bytes to write
        &dwBytesWritten, // number of bytes that were written
        NULL)) // no overlapped structure
        printf("Could not write %s to file (error %d)\n",DataBuffer, GetLastError());
        else
        _tprintf(TEXT("Wrote %d bytes successfully.\n"), dwBytesWritten);
        if( FALSE == ReadFile(hCom, ReadBuffer, BUFFER_SIZE-2, &dwBytesRead,

        S V 2 Replies Last reply
        0
        • A AprNgp

          my code is as follows ...

          DCB dcb;
          HANDLE hCom;
          BOOL fSuccess;
          TCHAR *pcCommPort = TEXT("COM3");
          char DataBuffer[] = "AT+FCLASS=8";
          DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
          char Data2[]="ATDW2289759";

          DWORD dwBytesWritten = 0;
          DWORD dwBytesRead = 0;
          char ReadBuffer\[BUFFER\_SIZE\] = {0};
          

          hCom = CreateFile( pcCommPort,
          GENERIC_READ | GENERIC_WRITE,
          0, // must be opened with exclusive-access
          NULL, // default security attributes
          OPEN_EXISTING, // must use OPEN_EXISTING
          0, // not overlapped I/O
          NULL // hTemplate must be NULL for comm devices
          );

          if (hCom == INVALID_HANDLE_VALUE)
          {
          // Handle the error.
          printf ("CreateFile failed with error %d.\n", GetLastError());
          return (1);
          }

          // Build on the current configuration, and skip setting the size
          // of the input and output buffers with SetupComm.

          // SecureZeroMemory(&dcb, sizeof(DCB));
          dcb.DCBlength = sizeof(DCB);
          fSuccess = GetCommState(hCom, &dcb);

          if (!fSuccess)
          {
          // Handle the error.
          printf ("GetCommState failed with error %d.\n", GetLastError());
          return (2);
          }

          dcb.BaudRate = CBR\_2400;     // set the baud rate
          dcb.ByteSize = 8;             // data size, xmit, and rcv
          dcb.Parity = NOPARITY;        // no parity bit
          dcb.StopBits = ONESTOPBIT;    // one stop bit
          

          fSuccess = SetCommState(hCom, &dcb);

          if (!fSuccess)
          {
          // Handle the error.
          printf ("SetCommState failed with error %d.\n", GetLastError());
          return (3);
          }

          _tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);

          //WriteFile(hCom,"AT+FCLASS=8",sizeof("AT+FCLASS=8"),&len,0);
          

          if( FALSE == WriteFile(hCom, // open file handle
          DataBuffer + dwBytesWritten, // start of data to write
          dwBytesToWrite - dwBytesWritten, // number of bytes to write
          &dwBytesWritten, // number of bytes that were written
          NULL)) // no overlapped structure
          printf("Could not write %s to file (error %d)\n",DataBuffer, GetLastError());
          else
          _tprintf(TEXT("Wrote %d bytes successfully.\n"), dwBytesWritten);
          if( FALSE == ReadFile(hCom, ReadBuffer, BUFFER_SIZE-2, &dwBytesRead,

          S Offline
          S Offline
          SandipG
          wrote on last edited by
          #5

          Probably you are communicating with some mobile or modem. Append \r\n at the end of every AT Command. Then only you will get the response from the device.

          Regards, Sandip.

          modified on Friday, August 29, 2008 2:32 PM

          A 1 Reply Last reply
          0
          • S SandipG

            Probably you are communicating with some mobile or modem. Append \r\n at the end of every AT Command. Then only you will get the response from the device.

            Regards, Sandip.

            modified on Friday, August 29, 2008 2:32 PM

            A Offline
            A Offline
            AprNgp
            wrote on last edited by
            #6

            STILL no call is made by the modem, even though same commands are working well with HyperTerminal ... what shall i do ? :(

            Apurv

            S 1 Reply Last reply
            0
            • A AprNgp

              STILL no call is made by the modem, even though same commands are working well with HyperTerminal ... what shall i do ? :(

              Apurv

              S Offline
              S Offline
              SandipG
              wrote on last edited by
              #7

              you are reading as soon as you write try adding some sleep in between write and read. I hope it will help you. But don't forget to add "\r\n" may be you can try "\n\r" i am not sure about the exact order.

              Regards, Sandip.

              A 1 Reply Last reply
              0
              • S SandipG

                you are reading as soon as you write try adding some sleep in between write and read. I hope it will help you. But don't forget to add "\r\n" may be you can try "\n\r" i am not sure about the exact order.

                Regards, Sandip.

                A Offline
                A Offline
                AprNgp
                wrote on last edited by
                #8

                i have tried Sleep(1000*10); and Sleep(1000*20); the o/p of reading is the earlier command + OK (in the next line)

                Apurv

                S 1 Reply Last reply
                0
                • A AprNgp

                  i have tried Sleep(1000*10); and Sleep(1000*20); the o/p of reading is the earlier command + OK (in the next line)

                  Apurv

                  S Offline
                  S Offline
                  SandipG
                  wrote on last edited by
                  #9

                  OK is nothing but response from the modem. what is not working then for you???:confused:

                  Regards, Sandip.

                  A 1 Reply Last reply
                  0
                  • S SandipG

                    OK is nothing but response from the modem. what is not working then for you???:confused:

                    Regards, Sandip.

                    A Offline
                    A Offline
                    AprNgp
                    wrote on last edited by
                    #10

                    it is not dialing the number i provided .... ATDW2289759\r\n

                    Apurv

                    S 1 Reply Last reply
                    0
                    • A AprNgp

                      my code is as follows ...

                      DCB dcb;
                      HANDLE hCom;
                      BOOL fSuccess;
                      TCHAR *pcCommPort = TEXT("COM3");
                      char DataBuffer[] = "AT+FCLASS=8";
                      DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
                      char Data2[]="ATDW2289759";

                      DWORD dwBytesWritten = 0;
                      DWORD dwBytesRead = 0;
                      char ReadBuffer\[BUFFER\_SIZE\] = {0};
                      

                      hCom = CreateFile( pcCommPort,
                      GENERIC_READ | GENERIC_WRITE,
                      0, // must be opened with exclusive-access
                      NULL, // default security attributes
                      OPEN_EXISTING, // must use OPEN_EXISTING
                      0, // not overlapped I/O
                      NULL // hTemplate must be NULL for comm devices
                      );

                      if (hCom == INVALID_HANDLE_VALUE)
                      {
                      // Handle the error.
                      printf ("CreateFile failed with error %d.\n", GetLastError());
                      return (1);
                      }

                      // Build on the current configuration, and skip setting the size
                      // of the input and output buffers with SetupComm.

                      // SecureZeroMemory(&dcb, sizeof(DCB));
                      dcb.DCBlength = sizeof(DCB);
                      fSuccess = GetCommState(hCom, &dcb);

                      if (!fSuccess)
                      {
                      // Handle the error.
                      printf ("GetCommState failed with error %d.\n", GetLastError());
                      return (2);
                      }

                      dcb.BaudRate = CBR\_2400;     // set the baud rate
                      dcb.ByteSize = 8;             // data size, xmit, and rcv
                      dcb.Parity = NOPARITY;        // no parity bit
                      dcb.StopBits = ONESTOPBIT;    // one stop bit
                      

                      fSuccess = SetCommState(hCom, &dcb);

                      if (!fSuccess)
                      {
                      // Handle the error.
                      printf ("SetCommState failed with error %d.\n", GetLastError());
                      return (3);
                      }

                      _tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);

                      //WriteFile(hCom,"AT+FCLASS=8",sizeof("AT+FCLASS=8"),&len,0);
                      

                      if( FALSE == WriteFile(hCom, // open file handle
                      DataBuffer + dwBytesWritten, // start of data to write
                      dwBytesToWrite - dwBytesWritten, // number of bytes to write
                      &dwBytesWritten, // number of bytes that were written
                      NULL)) // no overlapped structure
                      printf("Could not write %s to file (error %d)\n",DataBuffer, GetLastError());
                      else
                      _tprintf(TEXT("Wrote %d bytes successfully.\n"), dwBytesWritten);
                      if( FALSE == ReadFile(hCom, ReadBuffer, BUFFER_SIZE-2, &dwBytesRead,

                      V Offline
                      V Offline
                      Vaclav_
                      wrote on last edited by
                      #11

                      See documentation for SetCommTimeouts. After that verify your AT command string. As debugging tool, I would turn on modem speaker if you have one. Also verify that COM3 is valid and unused by other application. " When reading from a communications device, the behavior of ReadFile is governed by the current communication time-outs as set and retrieved using the SetCommTimeouts and GetCommTimeouts functions. Unpredictable results can occur if you fail to set the time-out values. For more information about communication time-outs, see COMMTIMEOUTS. "

                      A 1 Reply Last reply
                      0
                      • V Vaclav_

                        See documentation for SetCommTimeouts. After that verify your AT command string. As debugging tool, I would turn on modem speaker if you have one. Also verify that COM3 is valid and unused by other application. " When reading from a communications device, the behavior of ReadFile is governed by the current communication time-outs as set and retrieved using the SetCommTimeouts and GetCommTimeouts functions. Unpredictable results can occur if you fail to set the time-out values. For more information about communication time-outs, see COMMTIMEOUTS. "

                        A Offline
                        A Offline
                        AprNgp
                        wrote on last edited by
                        #12

                        THANX Vaclav_Sal .... will check it soon ....

                        Apurv

                        A 1 Reply Last reply
                        0
                        • A AprNgp

                          THANX Vaclav_Sal .... will check it soon ....

                          Apurv

                          A Offline
                          A Offline
                          AprNgp
                          wrote on last edited by
                          #13

                          :doh: how to set the timeouts ?? what should be the second parameter to SetCommTimeouts() function ?

                          Apurv

                          1 Reply Last reply
                          0
                          • A AprNgp

                            it is not dialing the number i provided .... ATDW2289759\r\n

                            Apurv

                            S Offline
                            S Offline
                            SandipG
                            wrote on last edited by
                            #14

                            Does this command work in HyperTerminal BTW:this link explains the AT commands might help you.. Modem commands[^]

                            Regards, Sandip.

                            A 1 Reply Last reply
                            0
                            • S SandipG

                              Does this command work in HyperTerminal BTW:this link explains the AT commands might help you.. Modem commands[^]

                              Regards, Sandip.

                              A Offline
                              A Offline
                              AprNgp
                              wrote on last edited by
                              #15

                              SandipG :) wrote:

                              Does this command work in HyperTerminal

                              yes ... the number is getting dialed thro' HyperTerminal ... it is working perfectly there ...

                              Apurv

                              S A 2 Replies Last reply
                              0
                              • A AprNgp

                                SandipG :) wrote:

                                Does this command work in HyperTerminal

                                yes ... the number is getting dialed thro' HyperTerminal ... it is working perfectly there ...

                                Apurv

                                S Offline
                                S Offline
                                SandipG
                                wrote on last edited by
                                #16

                                What does your program do after read buffer??

                                Regards, Sandip.

                                1 Reply Last reply
                                0
                                • A AprNgp

                                  SandipG :) wrote:

                                  Does this command work in HyperTerminal

                                  yes ... the number is getting dialed thro' HyperTerminal ... it is working perfectly there ...

                                  Apurv

                                  A Offline
                                  A Offline
                                  AprNgp
                                  wrote on last edited by
                                  #17

                                  in the HyperTerminal, when the command ATDW2289759 is given the response is VCON, but in my code the response is OK

                                  Apurv

                                  A 1 Reply Last reply
                                  0
                                  • A AprNgp

                                    in the HyperTerminal, when the command ATDW2289759 is given the response is VCON, but in my code the response is OK

                                    Apurv

                                    A Offline
                                    A Offline
                                    AprNgp
                                    wrote on last edited by
                                    #18

                                    there is a while loop as follows :

                                    while(1)
                                    {
                                    if(kbhit())
                                    {
                                    key=getch();
                                    if(key=='x')
                                    {
                                    CloseHandle(hCom);
                                    exit(0);
                                    }
                                    switch(key)
                                    {
                                    case '1':
                                    // code to issue command ATDW2289759\r\n
                                    }
                                    }
                                    }

                                    Apurv

                                    A 1 Reply Last reply
                                    0
                                    • A AprNgp

                                      there is a while loop as follows :

                                      while(1)
                                      {
                                      if(kbhit())
                                      {
                                      key=getch();
                                      if(key=='x')
                                      {
                                      CloseHandle(hCom);
                                      exit(0);
                                      }
                                      switch(key)
                                      {
                                      case '1':
                                      // code to issue command ATDW2289759\r\n
                                      }
                                      }
                                      }

                                      Apurv

                                      A Offline
                                      A Offline
                                      AprNgp
                                      wrote on last edited by
                                      #19

                                      well... its done ! the problem was, i was sending the command as a complete string, the modem needed the command in the pieces ... i mean character-by-character .... the problem is solved ... thanx to all of u .... :)

                                      Apurv

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups