一、前言
在上一篇:(五)Linux 4G模块封装发送指令函数以及检测串口和SIM卡是否就绪,封装了发送指令的send_at_cmd()函数,以及检测串口和SIM卡是否就绪的Check系列函数。现在我们可以发送短信了,不过需要知道的是,Text格式的编码只能发送英文,而PDU格式的编码即可以发送英文也可以发送中文,不过PDU实现起来相对麻烦,所以还是用Text格式来实现英文的发送。在第四篇,我们就已经实现了PDU包的封装了,现在要做的就是怎么把它们发送出去了。如果对AT指令发送短信,以及Text和PDU格式不了解的可回去看我之前写的文章,如下。
(三)Linux 4G模块实现短信发送的两种格式(Text和PDU)
二、Text格式发送英文
ASCII码分为两套:128个字符的标准ASCII码和额外的128个字符的扩展和ASCII码。1个Byte占8位(0~256)每个ASCII码存储在一个Byte中,从0到127的数字代表不同的常用符号,如65代表大写A,97代表小写A。由于ASCII字节的7位(0 ~ 127)中,最高位没有使用。所以ASCII编码足以表示所有英文字符和英文符号,这样我们用Text格式发送英文,而不是PDU, 也相对简单了。
int send_text_sms(ttyusb_ctx_t *ttyusb_ctx, char *phone_buf, char *sms_buf)
{
char temp_buf[256] = {0};
if (!ttyusb_ctx || !phone_buf || !sms_buf)
{
printf("[%s]Invalid argument!\n", __func__);
return -1;
}
//设置为Text格式
if (send_at_cmd(ttyusb_ctx, "AT+CMGF=1\r", "OK", NULL, 0))
{
printf("[%s]Send at command [AT+CMGF=1] to tty failure!\n", __func__);
return -2;
}
printf("[%s]Send at command [AT+CMGF=1] to tty successfully!\n\n\n", __func__);
sprintf(temp_buf, "AT+CMGS=\"%s\"\r", phone_buf);
//如果出现>号说明可以发送短信了
if (send_at_cmd(ttyusb_ctx, temp_buf, ">", NULL, 0))
{
printf("[%s]Receive > failure!\n", __func__);
return -3;
}
printf("[%s]Recv the '>' ok!\n", __func__);
strcat(sms_buf, "\x1a");
//发送短信,返回OK,发送成功
if (send_at_cmd(ttyusb_ctx, sms_buf, "OK", NULL, 0))
{
printf("[%s]Failed to send SMS messages!\n", __func__);
return -4;
}
printf("[%s]Successed to send SMS messages!\n", __func__);
return 0;
}
三、PDU格式发送中文
(1)获取短信中心号
用PDU发送短信用到短信中心号,这里对这个过程封装成了一个函数
int get_center_number(ttyusb_ctx_t *ttyusb_ctx, char *center_buf)
{
int send_rv = -1;
char return_buf[256] = {0};
char separator[] = "\"";//分隔符引号
char *token = NULL;
int i = 1;
if ((!ttyusb_ctx) || (!center_buf))
{
printf("[%s]Invalid argument!\n", __func__);
return -1;
}
#if 1
send_rv = send_at_cmd(ttyusb_ctx, "AT+CSCA?\r", "+CSCA", return_buf, sizeof(return_buf));
if (send_rv < 0)
{
printf("[%s]Not found center number!\n", __func__);
return -2;
}
token = strtok(return_buf, separator);
while (token != NULL)
{
++i;
token = strtok(NULL, separator);
printf("[%s]i: %d \ntoken:%s\n", __func__, i , token);
if(2 == i)
{
strncpy(center_buf, token, strlen(token));
break;
}
}
#endif
printf("[%s]The center number:%s\n", __func__, center_buf);
return 0;
}
(2)发送PDU短信
int send_pdu_sms(ttyusb_ctx_t *ttyusb_ctx, char *phone_buf, char *sms_buf)
{
char center_buf[256] = {0};
char pdu_buf[512] = {0};
char at_buf[256] = {0};
int cmgs_length = 0;
if (!ttyusb_ctx || !phone_buf || !sms_buf)
{
printf("[%s]Invalid argument!\n", __func__);
return -1;
}
if (get_center_number(ttyusb_ctx, center_buf) < 0)
{
printf("[%s]Get center number failure!\n", __func__);
return -2;
}
if (pdu_packet(center_buf, phone_buf, sms_buf, pdu_buf, &cmgs_length) < 0)
{
printf("[%s]Failed to package SMS messages into PDU format\n", __func__);
return -3;
}
//设置为PDU模式
if (send_at_cmd(ttyusb_ctx, "AT+CMGF=0\r", "OK", NULL, 0) < 0)
{
printf("[%s]Send at command [AT+CMGF=0\r] failure!\n", __func__);
return -4;
}
//发送PDU短信 "AT+CMGS=\"%s\"\r"
sprintf(at_buf, "AT+CMGS=%d\r", cmgs_length);
if (send_at_cmd(ttyusb_ctx, at_buf, ">", NULL, 0) < 0)
{
printf("[%s]Send at command [AT+CMGS=%d\r] failure!\n", __func__, cmgs_length);
return -5;
}
//ASCII码1,2,3...分别依次对应键盘按键的Ctrl+A键,Ctrl+B键,Ctrl+C键,...Ctrl+Z键的ASCII
strcat(pdu_buf,"\x1a");
if (send_at_cmd(ttyusb_ctx, pdu_buf, "OK", NULL, 0))
{
printf("[%s]Send pud sms failure!\n", __func__);
return -6;
}
printf("[%s]Send pud sms successfully!\n", __func__);
return 0;
}
四、程序流程图
五、主程序
#include "main.h"
void install_signal(void);
void handler(int sig);
int identify_sms_type(char *sms_buf);
int g_stop = 0;
int main(int argc, char *argv[])
{
int rv = - 1;
int rv_fd = -1;
char send_buf[128] = {0};
char recv_buf[128] = {0};
char phone_buf[128] = {0};
char sms_buf[256] = {0};
int sms_type = 0;
int chose;
int ch;
int i;
ttyusb_ctx_t ttyusb_ctx;
ttyusb_ctx_t *ttyusb_ctx_ptr;
ttyusb_ctx_ptr = &ttyusb_ctx;
ttyusb_ctx_ptr->timeout = 10;
log_ctx_t log_ctx;
log_ctx.loglevel = 2; //LOG_LEVEL_INFO
strcpy(log_ctx.logfile, "../logger/running.log");
log_ctx.logsize = 1048576; //1MB
struct option opts[] = {
{"baudrate", required_argument, NULL, 'b'},
{"databits", required_argument, NULL, 'd'},
{"parity", required_argument, NULL, 'p'},
{"stopbits", required_argument, NULL, 's'},
{"serial_name", required_argument, NULL, 'm'},
{"help", no_argument, NULL, 'h'},
{0,0,0,0}
};
while((ch = getopt_long(argc, argv, "b:d:p:s:m:h", opts, NULL)) != -1)
{
switch(ch)
{
case 'b':
{
ttyusb_ctx_ptr->baudrate = atoi(optarg);
break;
}
case 'd':
{
ttyusb_ctx_ptr->databits = atoi(optarg);
break;
}
case 'p':
{
ttyusb_ctx_ptr->parity = optarg[0];
break;
}
case 's':
{
ttyusb_ctx_ptr->stopbits = atoi(optarg);
break;
}
case 'm':
{
strncpy(ttyusb_ctx_ptr->serial_name, optarg, SERIAL_NAME);
break;
}
case 'h':
{
print_usage(argv[0]);
break;
}
default:
{
printf("%s input invalid argument!\n", __func__);
return -1;
}
}
}
if(logger_init(log_ctx.logfile, log_ctx.loglevel) < 0)
{
fprintf(stderr, "Initial logger system failure\n");
return -5;
}
if(0 == strlen(ttyusb_ctx_ptr->serial_name))
{
log_error("Failed to obtain the device name!\n");
return -1;
}
install_signal();
if(tty_open(ttyusb_ctx_ptr) < 0)
{
log_error("Failed to open the device file");
return -2;
}
if(tty_init(ttyusb_ctx_ptr) < 0)
{
log_error("Failed to initialize the serial port\n");
return -3;
}
if(check_all_ready(ttyusb_ctx_ptr) < 0)
{
log_error("tty or SIM is not ok!\n");
return -4;
}
while(!g_stop)
{
printf("-------------------------Start sending messages-------------------------\n");
memset(phone_buf, 0, sizeof(phone_buf));
memset(sms_buf, 0, sizeof(sms_buf));
printf("\tInput <1> Send mesage\t\t Input <other> Quit\n");
scanf("%d", &chose);
getchar();
if(1 != chose)
{
goto CleanUp;
}
printf("Enter phone number: ");
scanf("%s", phone_buf);
getchar();
//下面代码:Invalid or incomplete multibyte or wide character
printf("\nEnter SMS: ");
fgets(sms_buf, sizeof(sms_buf), stdin);
for(i = 0; i < sizeof(sms_buf); i++)
{
if(sms_buf[i] == 0x0a)
{
sms_buf[i] = 0;
break;
}
}
printf("\n");
sms_type = identify_sms_type(sms_buf);
if(1 == sms_type)
{
if(send_pdu_sms(ttyusb_ctx_ptr, phone_buf, sms_buf) < 0)
{
printf("Send pdu sms failure!\n");
continue;
}
printf("Send pdu sms successfully!\n");
continue;
}
else
{
log_debug("[%s]sms_buf:%s\n", __func__, sms_buf);
if(send_text_sms(ttyusb_ctx_ptr, phone_buf, sms_buf) < 0)
{
printf("Send text sms failure!\n");
continue;
}
printf("Send text sms successfully!\n");
continue;
}
}
return 0;
CleanUp:
tty_close(ttyusb_ctx_ptr);
return rv;
}
int identify_sms_type(char *sms_buf)
{
int rv = 0;
int i = 0;
for(i = 0; i < strlen(sms_buf); i++)
{
if((int)sms_buf[i] > 0x7F)
{
return 1;
}
}
return 0;
}
void print_usage(char *program_name)
{
printf("Usage:%s[OPTION]\n\n", program_name);
printf("-b[baudrate]:Select baud rate, for example 115200 and 9600.\n");
printf("-p[parity]:Select parity check, for example n N e E o O.\n");
printf("-s[stopbits]:Select stop bit, for example 1 and 2.\n");
printf("-m[serial_name]:Select device file, for example /dev/ttyUSB0.\n");
printf("-h[help]:Printing Help Information.\n");
printf("For example:./SMS -b 115200 -p n -s 1 -m /dev/ttyUSB0 \n\n");
}
void handler(int sig)
{
switch(sig)
{
case SIGINT:
{
printf("Process captured SIGINT signal!\n");
g_stop = 1;
break;
}
case SIGTERM:
{
printf("Process captured SIGTERM signal!\n");
g_stop = 1;
break;
}
case SIGSEGV:
{
printf("Process captured SIGSEGV signal!\n");
g_stop = 1;
exit(0);
break;
}
case SIGPIPE:
{
printf("Process captured SIGPIPE signal!\n");
g_stop = 1;
break;
}
default:
break;
}
return ;
}
void install_signal(void)
{
struct sigaction sigact;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigact.sa_handler = handler;
sigaction(SIGINT, &sigact, 0);
sigaction(SIGTERM, &sigact, 0);
sigaction(SIGPIPE, &sigact, 0);
sigaction(SIGSEGV, &sigact, 0);
return ;
}
六、运行结果
(1)发送端
(2)手机接收端
七、问题解决
1、主要还是细节问题吧,前前后后不知道发了多少条短信测试。PDU数据打包一定要准确无误,错一个字符都会出问题。有时候都不知道自己的转码是否有问题,后来找了个PDU转码网站:http://tools.bugscaner.com/smspdu.html,挺好用,事半功倍。
2、按照发送信息的流程,在发送信息前需要发送"AT+CMGS=长度值\r"这个指令,然后我一直以为这个长度值是整个PDU包的长度,后来调了半天发不出信息才知道并不是整个PDU包的长度,而是处理过的收件号码长度+已处理的短信长度。
3、对了,千万不要用循环发短信,不然你都不知道话费怎么秒变0的。