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 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>