Sending AT commands to USR modem in C

只谈情不闲聊 提交于 2021-01-28 11:50:40

问题


I was wondering if any of you know what I'm doing wrong here? So I have this program in C, that sends AT commands to a modem. These commands have been tested on hyperterminal and work fine, but when sendind them through the modem I get, first, and "OK" for the first "AT" command, which is good, but then, upon sending the next command, the modem answers with "AT+CC"...which I have no idea what it means. Any help is appreciated.

Source:

void sendSMS(const char* port, const char* number, const char* baud)
{
    HANDLE hComm;
    DCB dcb;
    BOOL fSuccess;

    hComm = CreateFile(port,                //port name
        GENERIC_READ | GENERIC_WRITE, //Read/Write
        0,                            // No Sharing
        NULL,                         // No Security
        OPEN_EXISTING,// Open existing port only
        0,            // Non Overlapped I/O
        NULL);        // Null for Comm Devices

    if (hComm == INVALID_HANDLE_VALUE)
    {
        printf("ERROR: Cannot open serial port\r\n");
        return;
    }
    else
        printf("STATUS: Serial port opened\r\n");


    // Configure PORT
    //  Initialize the DCB structure.
    SecureZeroMemory(&dcb, sizeof(DCB));
    dcb.DCBlength = sizeof(DCB);

    //  Build on the current configuration by first retrieving all current
    //  settings.
    fSuccess = GetCommState(hComm, &dcb);

    if (!fSuccess)
    {
        //  Handle the error.
        printf("GetCommState failed with error %d.\n", GetLastError());
        return;
    }
                               //  Fill in some DCB values and set the com state: 
                               //  57,600 bps, 8 data bits, no parity, and 1 stop bit.
    dcb.BaudRate = atoi(baud);     //  baud rate
    dcb.ByteSize = 8;             //  data size, xmit and rcv
    dcb.Parity = NOPARITY;      //  parity bit
    dcb.StopBits = ONESTOPBIT;    //  stop bit
    dcb.fOutxCtsFlow = FALSE;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fOutX = FALSE;

    fSuccess = SetCommState(hComm, &dcb);

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

    //  Get the comm config again.
    fSuccess = GetCommState(hComm, &dcb);

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

    // End Config

    sendATCommands(hComm, number, "This is NOT a test.");
    CloseHandle(hComm);//Closing the Serial Port

}

void sendATCommands(HANDLE hComm, const char* number, const char message[])
{
    char str[256];

    if (!writeToPort(hComm, "AT\r\n")) //Hello modem
        return;

    if (!readFromPort(hComm)) // Must be OK
        return;

    if (!writeToPort(hComm, "AT+CMGF=1\r\n")) //Modem, prepare to send text messages
        return;

    if (!readFromPort(hComm)) // Must be OK again
        return;

    memset(str, 0, 256); 
    strcpy_s(str, "AT+CMGS=\"");
    strcat_s(str, 256, number);
    strcat_s(str, 256, "\"\r\n");

    if (!writeToPort(hComm, str)) //Modem, here's the number to send the message to
        return;

    if (!readFromPort(hComm)) // Must be ">" 
        return;

    memset(str, 0, 256); 
    strcpy_s(str, message);
    strcat_s(str, 256, "^Z");

    if (!writeToPort(hComm, str)) //Modem, communicate this to the number I gave you.
        return;

    if (!readFromPort(hComm)) // Must be CMGS: ##
        return;
}


int writeToPort(HANDLE hComm, const char lpBuffer[])
{
    DWORD dNoOFBytestoWrite;         // No of bytes to write into the port
    DWORD dNoOfBytesWritten = 0;     // No of bytes written to the port
    dNoOFBytestoWrite = sizeof(lpBuffer);

    int Status = WriteFile(hComm,        // Handle to the Serial port
                        lpBuffer,     // Data to be written to the port
                dNoOFBytestoWrite,  //No of bytes to write
                &dNoOfBytesWritten, //Bytes written
                            NULL);

    if (Status == FALSE)
    {
        printf("ERROR: Cannot write to serial port\r\n");
    }
    else
        printf("STATUS: Command %s \n written to port.\r\n", lpBuffer); 

    return Status;
}

int readFromPort(HANDLE hComm)
{
    char TempChar; //Temporary character used for reading
    char SerialBuffer[256];//Buffer for storing Rxed Data
    DWORD NoBytesRead = 0;
    int i = 0;
    int status;

    memset(SerialBuffer, 0, 256);
    printf("STATUS: Waiting response...\r\n");

    do
    {
        status = ReadFile(hComm,           //Handle of the Serial port
            &TempChar,       //Temporary character
            sizeof(TempChar),//Size of TempChar
            &NoBytesRead,    //Number of bytes read
            NULL);

        if (!status)
        {
            printf("ERROR: Cannot read from serial port\r\n");
            break;
        }

        SerialBuffer[i] = TempChar;// Store Tempchar into buffer
        i++;
    }
    while (NoBytesRead > 0);

    if (status)
        printf("PORT RESPONSE: %s \r\n", SerialBuffer);

    return status;
}

来源:https://stackoverflow.com/questions/49057262/sending-at-commands-to-usr-modem-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!