-1

This code sends some of SMS messages. But it gives error for some of the messages. It may about some wrong character that GSM module does not recognize like `(. Is there any one can help to fix the code. I am checking if GSM device is attached to ttyUSB2. This is C++ code.

int sendSms(UsbPort &device, string phoneNumber, string message) {

    char buf[MAXBUF] = {0};
    int n;

    if(device.fildes > 0) {
        int max_check = 0;

        if (write(device.fildes, "ATZ\r", 4) < 4) {
            printf("ATZ write error - %s \n", strerror(errno));
            return -1;
        }

        this_thread::sleep_for(chrono::seconds(1));

        if (write(device.fildes, "AT+CMGF=1\r", 10) < 10) {
            printf("AT+CMGF=1 write error - %s \n", strerror(errno));
            return -1;
        }

        if (tcdrain(device.fildes) != 0) {
            perror("tcdrain() error");
            return -1;
        }

        this_thread::sleep_for(chrono::seconds(1));
        bzero(buf, sizeof(buf));
        read(device.fildes, buf, MAXBUF - 1);
        cout << "Buffer-1 = " << buf << endl;

        if (write(device.fildes, "ATE+CSMS=1\r", 11) < 11) {
            printf("ATE+CSMS=1 write error - %s \n", strerror(errno));
            return -1;
        }

        if (tcdrain(device.fildes) != 0) {
            perror("tcdrain() error");
            return -1;
        }

        this_thread::sleep_for(chrono::seconds(1));
        bzero(buf, sizeof(buf));

        if ((n = read(device.fildes, buf, MAXBUF - 1)) > -1) {
            int length = strlen(buf);
            cout << "length = " << length << endl;
            buf[length] = '\0';
            cout << "Buffer-2 = " << buf << endl;
            if (strstr(buf, "+CSMS:") != NULL) { // Ready to send SMS
                cout << "Can send SMS: " + string(buf) << endl;
                // Send SMS
                string data1 = "AT+CMGS=\"" + phoneNumber + "\"\r";
                if (write(device.fildes, data1.c_str(), data1.length()) < data1.length()) {
                    printf("AT+CMGS write error - %s \n", strerror(errno));
                    return -1;
                }
                else {
                    this_thread::sleep_for(chrono::seconds(1));
                    string data2 = message + "\x1A";
                    if (write(device.fildes, data2.c_str(), data2.length()) < data2.length()) {
                        printf("ATE+CSMS=1 write error - %s \n", strerror(errno));
                        return -1;
                    }

                    if (tcdrain(device.fildes) != 0) {
                        perror("tcdrain() error");
                        return -1;
                    }

                    this_thread::sleep_for(chrono::seconds(1));
                    bzero(buf, sizeof(buf));
                    if ((n = read(device.fildes, buf, MAXBUF - 1)) > -1) {
                        int length = strlen(buf);
                        cout << "length = " << length << endl;
                        buf[length] = '\0';
                        cout << "Buffer-2 = " << buf << endl;
                        if (strstr(buf, "OK") != NULL) {
                            cout << message + " sent to : " + telNo + " successfully." << endl;
                            return 0;
                        }
                    }
                    else {
                        cout << message + " sent to : " + telNo + " unsuccessful!" << endl;
                        return -1;
                    }
                }
            }
            else {
                cout << "Error: buf = " + string(buf) << endl;
                return -1;
            }
        }
    }
    return -1;
}

this is the example of which it cannot send sms.

The message that it does not send

FSA
  • 67
  • 9
  • Actually `(` and `)` characters do belong to GSM charset. Could you please edit your question so that the output log is in text form instead of an image? – Roberto Caboni Feb 09 '21 at 15:11
  • Please, post the entire output of `sendSMS` function. And, please, specify also: 1) how is `MAX_BUF` defined? Could you print separately the contents of `message` and also the value of `data2.length()`? What happens if you insert a shorted message, let's say the substring before `Alarm`? – Roberto Caboni Feb 09 '21 at 15:26
  • Finally: could you post the response of the command `AT+CLAN?`. – Roberto Caboni Feb 09 '21 at 15:36
  • We found the problem. It is not about ASCII characters. It is about message longer than it should be. We need to Send Sms as Concatenated SMS. Thanks for your responses. – FSA Feb 12 '21 at 11:24
  • It was exactly my assumption, that's why I asked those question in my previous comments. – Roberto Caboni Feb 12 '21 at 11:30

1 Answers1

1

The problem was not about ASCII characters, but about sending a message longer than it should be.

When we use Concatenated SMS it solved our problem. It Send Sms without problem.

AT+CGMF=1
AT+CSCS="GSM"
AT+CSMP=17,167,0,241
AT+CMGS="Number"
> Message <CTRL+Z>
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
FSA
  • 67
  • 9