• (六)Linux 4G模块Text格式和PDU格式实现中英文短信发送


    一、前言

    在上一篇:(五)Linux 4G模块封装发送指令函数以及检测串口和SIM卡是否就绪,封装了发送指令的send_at_cmd()函数,以及检测串口和SIM卡是否就绪的Check系列函数。现在我们可以发送短信了,不过需要知道的是,Text格式的编码只能发送英文,而PDU格式的编码即可以发送英文也可以发送中文,不过PDU实现起来相对麻烦,所以还是用Text格式来实现英文的发送。在第四篇,我们就已经实现了PDU包的封装了,现在要做的就是怎么把它们发送出去了。如果对AT指令发送短信,以及Text和PDU格式不了解的可回去看我之前写的文章,如下。

    (三)Linux 4G模块实现短信发送的两种格式(Text和PDU)

    (四)Linux 4G模块实现短信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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    三、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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    (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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    四、程序流程图

    在这里插入图片描述

    五、主程序

    #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  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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270

    六、运行结果

    (1)发送端
    在这里插入图片描述
    (2)手机接收端
    在这里插入图片描述

    七、问题解决

    1、主要还是细节问题吧,前前后后不知道发了多少条短信测试。PDU数据打包一定要准确无误,错一个字符都会出问题。有时候都不知道自己的转码是否有问题,后来找了个PDU转码网站:http://tools.bugscaner.com/smspdu.html,挺好用,事半功倍。
    2、按照发送信息的流程,在发送信息前需要发送"AT+CMGS=长度值\r"这个指令,然后我一直以为这个长度值是整个PDU包的长度,后来调了半天发不出信息才知道并不是整个PDU包的长度,而是处理过的收件号码长度+已处理的短信长度。
    3、对了,千万不要用循环发短信,不然你都不知道话费怎么秒变0的。

  • 相关阅读:
    欧美风商务简约通用PPT模板
    DocuWare Workflow Manager(工作流管理器)
    QT 笔记 QGraphicsItem 介绍
    1. Unified Structure Generation for Universal Information Extraction 阅读笔记
    为什么要把ip和mac地址绑定
    全球领先飞瞳引擎™云服务全球两千+企业用户,集装箱识别集装箱箱况残损检测,正常箱号识别率99.98%以上,箱信息识别及铅封识别免费
    12个MySQL慢查询的原因分析
    32、JAVA进阶——解析XML技术
    React函数组件状态Hook—useState《进阶-对象&&数组》
    用户上传图片并将路径地址存入数据库中
  • 原文地址:https://blog.csdn.net/weixin_45880057/article/details/126021537