locked
AT Commands sent to USB Modem RRS feed

  • Question

  • Hi,

    I am trying to send AT commands to a USB modem using VC++. I found the code below online and have tried using it. I do not get any errors but it doesn't appear to be working.

    I used Putty to set AT+CMGF= 0. However after I run this code, when I check the value of AT+CMGF it is still 0, so although no error is being thrown up, it doesn't seem to be sending correctly.

    Any help would be great thanks.

    #include <iostream>
    #include <Windows.h>
    #include <string.h>

    using std::string;


    int main() {

    std::string data = "AT+CMGF=1";


     DCB dcb; 
     DWORD byteswritten; 
     HANDLE hPort = CreateFile( 
     
     "COM3", //"\\\\.\\COM10"
     GENERIC_WRITE | GENERIC_READ, 
     FILE_SHARE_READ| FILE_SHARE_WRITE, 
     NULL, 
     OPEN_EXISTING, 
     0, 
     NULL 
     ); 
     DWORD dwerror = GetLastError();
     if (GetCommState(hPort,&dcb) == 0) {
     
    return false; }
     dcb.BaudRate = CBR_9600; //9600 Baud 
     dcb.ByteSize = 8; //8 data bits 
     dcb.Parity = NOPARITY; //no parity 
     dcb.StopBits = ONESTOPBIT; //1 stop 
     if (!SetCommState(hPort,&dcb)) 
     return false; 


     bool retVal = WriteFile(hPort,&data,200,&byteswritten,NULL);

     dwerror = GetLastError();

      
     CloseHandle(hPort); //close the handle 
     return retVal; 




    }


    • Moved by May Wang - MSFT Wednesday, September 10, 2014 6:53 AM out of C++ support range
    Thursday, September 4, 2014 11:27 AM

Answers

  • To make it work, adjust your solved code. Replace

        WriteFile(hPort,&mode,mode.capacity(), . . .

    with

        WriteFile( hPort, mode.c_str(), mode.length(), . . .

    • Marked as answer by prograplex Thursday, September 11, 2014 2:19 PM
    Thursday, September 11, 2014 10:00 AM

All replies

  • SOLVED: just needed to add \r\n after my commands

    Okay, not solved! The first command, setting CMGF=1 works, nothing else seems to go through though. i.e. if CMGF is 0 and i run this code setting it to 1, it changes to 1 when I check the value in putty. 

    I'm not sure if im doing something wrong in the way I send the CMGS and text message commands.

    No errors come up...but the text message isn't sent. I send the CMGF and CMGS commands followed by a carriage return and the text message content followed by the SUB (CTRL+Z). doesn't seem to work. If I try reading the serial port using ReadFile() it just hangs without setting a timeout. Any help would be appreciated. 

    #include <iostream>
    #include <Windows.h>
    #include <string.h>

    using namespace std;


    int main() {


    string CR;
    CR = 13;

    string SUB;
    SUB = 26;

    string mode = "AT+CMGF=1";  //+ CR; // \r";
    mode.append(CR);



    string phone = "AT+CMGS=\"0430554519\"" ; // \r";
    phone.append(CR);


    string message = "> Test C++ message " ;
    message.append(SUB);




     DCB dcb; 
     DWORD byteswritten1; 
     DWORD byteswritten2; 
     DWORD byteswritten3; 
     DWORD byteswritten4; 
     DWORD byteswritten5; 
     DWORD byteswritten6; 

     HANDLE hPort = CreateFile( 
     
     "COM3",
     GENERIC_WRITE | GENERIC_READ, 
     FILE_SHARE_READ| FILE_SHARE_WRITE, 
     NULL, 
     OPEN_EXISTING, 
     0, 
     NULL 
     ); 
     DWORD dwerror = GetLastError();
     if (GetCommState(hPort,&dcb) == 0) {
     
    return false; }
     dcb.BaudRate = CBR_9600; //9600 Baud 
     dcb.ByteSize = 8; //8 data bits 
     dcb.Parity = NOPARITY; //no parity 
     dcb.StopBits = ONESTOPBIT; //1 stop 
     if (!SetCommState(hPort,&dcb)) 
     return false; 

      char UitBuff1[25];
    DWORD dwBytesRead;

     bool retVal = WriteFile(hPort,&mode,mode.capacity(),&byteswritten1,NULL);
      retVal = WriteFile(hPort,&CR,CR.capacity(),&byteswritten2,NULL); 

      
     bool retVal1 = WriteFile(hPort,&phone,phone.capacity(),&byteswritten3,NULL);
      retVal = WriteFile(hPort,&CR,CR.capacity(),&byteswritten4,NULL);


     bool retVal2 = WriteFile(hPort,&message,message.capacity(),&byteswritten5,NULL);
      retVal = WriteFile(hPort,&CR,CR.capacity(),&byteswritten6,NULL);
     
     


      dwerror = GetLastError();

      
    CloseHandle(hPort); //close the handle 
     return retVal; 




    }

    • Proposed as answer by May Wang - MSFT Friday, September 5, 2014 6:38 AM
    • Edited by prograplex Friday, September 5, 2014 3:13 PM
    • Unproposed as answer by prograplex Friday, September 5, 2014 3:13 PM
    Thursday, September 4, 2014 12:09 PM
  • you should use std::endl; which is a better practice with C++11


    Corsair Carbide 300R with TX850V2
    Asus M5A99FX PRO R2.0 CFX/SLI
    AMD Phenom II 965 C3 Black Edition @ 4.0 GHz
    G.SKILL RipjawsX DDR3-2133 8 GB
    EVGA GTX 660 Ti FTW Signature 2 (GK104 Kepler)
    Asus PA238QR IPS LED HDMI DP 1080p
    ST2000DM001 & Windows 8.1 Professional x64
    Microsoft Wireless Desktop 2000 & Wacom Bamboo CHT470M

    Place your rig specifics into your signature like I have, makes it 100x easier to understand!

    Hardcore Games Legendary is the Only Way to Play!

    Thursday, September 4, 2014 12:11 PM
  • Get a null modem cable and a 2nd PC (or a 2nd serial port), and see what is actually received on the other side. --pa

    I have a USB modem, it works fine with recent Windows and it recognizes the AT commands

    I use it for fax server role on my server


    Corsair Carbide 300R with TX850V2
    Asus M5A99FX PRO R2.0 CFX/SLI
    AMD Phenom II 965 C3 Black Edition @ 4.0 GHz
    G.SKILL RipjawsX DDR3-2133 8 GB
    EVGA GTX 660 Ti FTW Signature 2 (GK104 Kepler)
    Asus PA238QR IPS LED HDMI DP 1080p
    ST2000DM001 & Windows 8.1 Professional x64
    Microsoft Wireless Desktop 2000 & Wacom Bamboo CHT470M

    Place your rig specifics into your signature like I have, makes it 100x easier to understand!

    Hardcore Games Legendary is the Only Way to Play!

    Saturday, September 6, 2014 2:49 PM
  • im using a usb modem. i dont think my laptop has an rs232 port. can you get a usb null modem cable?
    Sunday, September 7, 2014 1:02 PM
  • did you program it in C? did any of the commands you used required the SUB (CTRL+Z) command to be sent? and a series of commands to be sent, like when sending an sms and requiring to submit number and then message after that?

    the problem i think is in sending the AT+CMGS="number"  and then sending the message after that.


    Sunday, September 7, 2014 1:05 PM
  • check to see if the USB modem accepted the command, if there is an error message it has to be dealt with

    this requires a full duplex message system

    so polling in a loop is the usual architecture


    Corsair Carbide 300R with TX850V2
    Asus M5A99FX PRO R2.0 CFX/SLI
    AMD Phenom II 965 C3 Black Edition @ 4.0 GHz
    G.SKILL RipjawsX DDR3-2133 8 GB
    EVGA GTX 660 Ti FTW Signature 2 (GK104 Kepler)
    Asus PA238QR IPS LED HDMI DP 1080p
    ST2000DM001 & Windows 8.1 Professional x64
    Microsoft Wireless Desktop 2000 & Wacom Bamboo CHT470M

    Place your rig specifics into your signature like I have, makes it 100x easier to understand!

    Hardcore Games Legendary is the Only Way to Play!

    Monday, September 8, 2014 1:12 AM
  • I just got some free virtual com port software and created a bridge between two ports. I used Putty to see what happened on the other side. The messages I sent from my code, and the responses I received are shown below. I used http://www.hhdsoftware.com/free-virtual-serial-ports.

    I sent:

    AT+CMGF=1

    I received in putty:

    ▒M=AT+CMGF=1

    I sent:

    AT+CMGS=\"0430554519\"

    I received:

    ▒N=O=▒▒▒▒▒▒▒▒▒▒▒▒▒▒

    I sent:

     Test C++ message

    I received:

    N=hN=▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

    I tried another program to bridge the com ports with the same kind of output. The only difference is the output had u's instead of ='s.

    Thursday, September 11, 2014 6:27 AM
  • To make it work, adjust your solved code. Replace

        WriteFile(hPort,&mode,mode.capacity(), . . .

    with

        WriteFile( hPort, mode.c_str(), mode.length(), . . .

    • Marked as answer by prograplex Thursday, September 11, 2014 2:19 PM
    Thursday, September 11, 2014 10:00 AM
  • To make it work, adjust your solved code. Replace

        WriteFile(hPort,&mode,mode.capacity(), . . .

    with

        WriteFile( hPort, mode.c_str(), mode.length(), . . .

    you are my hero! :)

    thanks for all the help guys. 

    Thursday, September 11, 2014 2:19 PM