Getting AT commands in Actual message with Configured SMS mobile number

I’m getting these AT commands along with the actual message in SMS…
AT
AT+CPIN?
AT+CMGF=1
AT+CMGD=1,4
AT+CSCS=“GSM”
AT+CSCA=“919849087001”,145
ATE0
AT+CPIN?
AT+CMGS=“+917981134448”
Hello from GSM!!

int main()

{

/All initialize of UART and stuff/

send4GSMS(“Hello from GSM!!”);

}

void send4GSMS(uint8_t *string)

{

//Enter CTRL+Z after sending the data, then the message will be sent

unsigned char asciiValue = 0x1A;

//Command to send a short message from TE to network

uint8_t sendMsg[30] = “AT+CMGS="+917981134448"”; //Configured mobile number

//sends the message from TE to network

sendATCommand(sendMsg);

LL_mDelay(1);

// Send the data

while (*string != ‘\0’) {

// Transmit one character at a time

LL_USART_TransmitData8(USART3, *string++);

// Wait until transmit is complete

while (!LL_USART_IsActiveFlag_TC(USART3));

// Clear the TC flag for the next byte transmission

LL_USART_ClearFlag_TC(USART3);

}

// Send carriage return and line feed

LL_USART_TransmitData8(USART3, CR);

while (!LL_USART_IsActiveFlag_TC(USART3));

LL_USART_TransmitData8(USART3, LF);

while (!LL_USART_IsActiveFlag_TC(USART3));

LL_USART_TransmitData8(USART3, asciiValue);

while (!LL_USART_IsActiveFlag_TC(USART3));

LL_mDelay(10);

}

void MX_USART3_UART_Init(void)

{

LL_USART_InitTypeDef USART_InitStruct = { 0 };

LL_GPIO_InitTypeDef GPIO_InitStruct = { 0 };

LL_RCC_SetUSARTClockSource(LL_RCC_USART234578_CLKSOURCE_PCLK1);

/* Peripheral clock enable */

LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART3);

LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOD);

/**USART3 GPIO Configuration

PD8 ------> USART3_TX

PD9 ------> USART3_RX

*/

GPIO_InitStruct.Pin = LL_GPIO_PIN_8 | LL_GPIO_PIN_9;

GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;

GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;

GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;

GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;

GPIO_InitStruct.Alternate = LL_GPIO_AF_7;

LL_GPIO_Init(GPIOD, &GPIO_InitStruct);

/* USART3 interrupt Init */

NVIC_SetPriority(USART3_IRQn,

NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));

NVIC_EnableIRQ(USART3_IRQn);

USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;

USART_InitStruct.BaudRate = 115200;

USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;

USART_InitStruct.StopBits = LL_USART_STOPBITS_1;

USART_InitStruct.Parity = LL_USART_PARITY_NONE;

USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;

USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;

USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;

LL_USART_Init(USART3, &USART_InitStruct);

// USART3->BRR = 0x35B;

LL_USART_SetTXFIFOThreshold(USART3, LL_USART_FIFOTHRESHOLD_1_8);

LL_USART_SetRXFIFOThreshold(USART3, LL_USART_FIFOTHRESHOLD_1_8);

LL_USART_DisableFIFO(USART3);

LL_USART_ConfigAsyncMode(USART3);

LL_USART_Enable(USART3);

/* Polling USART3 initialisation */

while ((!(LL_USART_IsActiveFlag_TEACK(USART3)))

|| (!(LL_USART_IsActiveFlag_REACK(USART3)))) {

}

/* Initializing the USART3 Receive Interrupt */

LL_USART_ReceiveData8(USART3);

LL_USART_EnableIT_RXNE_RXFNE(USART3);

}

void sendATCommand(const uint8_t *command)

{

// Clearing the rxBuffer before sending a new command

//(void) memset(&uart3_Buf, 0, sizeof(uart3_Buf));

// Send the command

while (*command != ‘\0’) {

// Transmit one character at a time

LL_USART_TransmitData8(USART3, *command++);

// Wait until transmit is complete

while (!LL_USART_IsActiveFlag_TC(USART3));

// Clear the TC flag for the next byte transmission

LL_USART_ClearFlag_TC(USART3);

}

// Send carriage return and line feed

LL_USART_TransmitData8(USART3, CR);

while (!LL_USART_IsActiveFlag_TC(USART3));

LL_USART_TransmitData8(USART3, LF);

while (!LL_USART_IsActiveFlag_TC(USART3));

LL_mDelay(1);

}

Thanks for sharing. Seems everything is fine.

But why am I receiving those AT commands along with actual message in SMS, I need only message

I cannot directly test your code.
Here is an example code I tested.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <sys/select.h>

#define DEBUG_PRINTF    
#ifdef    DEBUG_PRINTF
    #define DEBUGFMT "%s(%d)-%s: "
    #define DEBUGARGS    __FILE__,__LINE__,__FUNCTION__

#define SERIALDBG(format, ...) \
    printf(DEBUGFMT "debug: " format, DEBUGARGS, ## __VA_ARGS__)


#else /* DEBUG_PRINTF */
#define SERIALDBG(format, ...)
#endif

static struct termios uartsave;
static struct termios uartset;
static struct termios uartnew;

static int open_serial(char *dev) {
    int fd;
    fd = open(dev, O_RDWR|O_NOCTTY|O_NDELAY, 0);
    if ( fd < 1 ) {
        fprintf(stderr, "open <%s> error %s\n", dev, strerror(errno));
        return -1;
    }

    return fd;
}

static void close_serial(int pf) {
    /* restore original terminal settings on exit */
    tcsetattr(pf, TCSANOW, &uartset);
    close(pf);
}

static void termios_setup(int pf,speed_t speed_param) {
    tcgetattr(pf, &uartsave);
    uartset = uartsave;
    cfsetospeed(&uartset, speed_param);
    cfsetispeed(&uartset, speed_param);

    // Space calibration
    uartset.c_cflag &= ~ CSIZE;      // Countless according to a bit mask
    uartset.c_cflag |= CS8;            // 8 bits of data
    uartset.c_cflag &= ~ CSTOPB;     // If set CSTOPB, 2 stop bits (do not set is a stop bits)
    uartset.c_cflag &= ~ PARODD;    // even parity is used
    uartset.c_cflag |= PARENB;        // enable calibration
    //uartset.c_cflag &= ~ PARENB;    // no calibration
    uartset.c_cflag &= ~ CRTSCTS;   // No flow control
    //uartset.c_cflag |= CRTSCTS;
    
    // if not development, just like serial port terminal, 
    // without the need for data transmission serial port to handle, 
    // then use the original Mode (Raw Mode) way to communication, 
    // setting as follows:

    // will CR mapped to litton precision; 
    // Start exports hardware flow control; 
    // Start entrance software flow control;
    // Allow characters restart flow control
    uartset.c_iflag &= ~ ( ICRNL | IXON | IXOFF | IXANY );    
    uartset.c_lflag &= ~ ( ICANON | ECHO | ECHOE | ISIG );
    uartset.c_cc[VMIN] = 0;
    uartset.c_cc[VQUIT] = 0;
    uartset.c_cflag |= CREAD; // enable receiving

    tcsetattr(pf, TCSANOW, &uartset);
    tcgetattr(pf, &uartnew);
}

/* send len bytes data in buffer */
static int serial_write(int fd, const void *buf, int len, struct timeval *write_tv) {
    int     count;
    int     ret;
    fd_set    output;
    struct timeval timeout;

    ret = 0;
    count = 0;

    timeout.tv_sec = write_tv->tv_sec;
    timeout.tv_usec = write_tv->tv_usec;

    FD_ZERO(&output);
    FD_SET(fd, &output);
    
    do{    /* listen */    
        ret = select(fd + 1, NULL, &output, NULL, &timeout);
        if (ret == -1) { /* error */
            perror("select()");
            return -1;
        } else if (ret) { /* write buffer */
            ret = write(fd, (char *) buf + count, len);
            if (ret < 1) {    
                fprintf(stderr, "write error %s\n", strerror(errno));
                return -1;
            }
            count += ret;
            len -= ret;
        }    
        else { /* timeout */
            SERIALDBG("time out.\n");
            return -EAGAIN;
        }
    } while (len > 0);

    SERIALDBG("write count=%d\n",count);

    return count;
}

/* read one char from serial */
int serial_read_ch(int fd, char *c, struct timeval *read_tv) {
    int        ret;
    fd_set    input;
    struct timeval timeout;

    ret = 0;

    timeout.tv_sec = read_tv->tv_sec;
    timeout.tv_usec = read_tv->tv_usec;

    FD_ZERO(&input);
    FD_SET(fd, &input);
    
    /* listen */
    ret = select(fd + 1, &input, NULL, NULL, &timeout);
    if (ret == -1) { /* error */
        perror("select()");
        return -1;
    }
    else if (ret) { /* read */
        ret = read(fd, c, sizeof(char));
        if (ret < 1) {    
            fprintf(stderr, "read error %s\n", strerror(errno));
            return -2;
        }
    }    
    else { /* timeout */
        SERIALDBG("time out.\n");
        return -3;
    }
    
    return 0;
}

/* wait until the first "OK" string come from serial */
static int wait_ok_string(int fd, int timeout_sec) {
    int i;
    int ret = 0;
    char ch = '?';
    char last = '?'; 
    struct timeval timeout;

    timeout.tv_sec = timeout_sec;
    timeout.tv_usec = 0;

    for (i = 0; i < 1024; i++) {
        /* read one char */
        ret = serial_read_ch(fd, &ch, &timeout);
        if (ret < 0)
            break;

        /* display the char */
        putchar(ch);

        /* check "ok" */
        if (last == 'O' && ch == 'K') {
            ret = 1;
            break;
        }

        last = ch;
    }

    return ret;
}

int main(int argc, char **argv) {
    int fd;
    char sms[128]; 
    char data[128];
    struct timeval tv;

    /* command line */
    if (argc != 4) {
        printf("usage: ./send_sms <<device>> <<phonenumber>> <<text>> \n"
               "eg: ./send_sms /dev/ttyUSB1 10086 message \n");
        return -1;
    }
    
    /* open serial */
    fd = open_serial(argv[1]);
    if(fd < 1)
        return -1;
    
    termios_setup(fd, B115200);
    tcflush(fd, TCIOFLUSH);

    tv.tv_sec = 5;
    tv.tv_usec = 0;

    // set as text mode
    const char *text_mode = "AT+CMGF=1\r";
    serial_write(fd, text_mode, strlen(text_mode), &tv);
    if (wait_ok_string(fd, 5) != 1) {
        printf("something error while setting text mode. exit!\n");
        goto err;
    }
        
    /* send sms */
    snprintf(sms, sizeof(sms), "AT+CMGS=\"%s\"\r", argv[2]);
    serial_write(fd, sms, strlen(sms), &tv);
    sleep(2);
    snprintf(data, sizeof(data), "%s\x1A", argv[3]);
    serial_write(fd, data, strlen(data), &tv);
    
    sleep(10);

    /* release */
    close_serial(fd);
    return 0;

err: 
    close_serial(fd);
    return -1;
}

As I guess,

  1. You did not disable the serial port echo function;
    2.After executing AT+CMGS, your serial port sends the above AT command again.