加入星計劃,您可以享受以下權益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴散
  • 作品版權保護
  • 300W+ 專業(yè)用戶
  • 1.5W+ 優(yōu)質創(chuàng)作者
  • 5000+ 長期合作伙伴
立即加入

C學習-信息轉換為PUD格式發(fā)送短信(限英文)

03/20 15:11
2808
服務支持:
技術交流群

完成交易后在“購買成功”頁面掃碼入群,即可與技術大咖們分享疑惑和經(jīng)驗、收獲成長和認同、領取優(yōu)惠和紅包等。

虛擬商品不可退

當前內(nèi)容為數(shù)字版權作品,購買后不支持退換且無法轉移使用。

加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點資訊討論
放大
實物圖
  • 方案介紹
    • 前言
    • ?一、演示效果
    • 二、模塊
    • 三、代碼編寫
    • ?四、參考
  • 相關文件
  • 推薦器件
  • 相關推薦
  • 電子產(chǎn)業(yè)圖譜
申請入駐 產(chǎn)業(yè)圖譜

前言

因為之前做項目的時候,每次使用短信模塊發(fā)送短信都需要在平臺去轉換一次數(shù)據(jù),搞的相當麻煩,所以就嘗試這可以寫一共PUD格式轉換的文件來在程序中讓他自動就轉換成功,

**? 轉載請注明出處

?一、演示效果

1. 參數(shù)定義

2. STM32串口輸出

?3. 平臺驗證

PDU轉換器icon-default.png?t=N7T8http://smstools3.kekekasvi.com/topic.php?id=288

二、模塊

開發(fā)版的話還是用到的STM32系列,首先使用到的模塊的話?也在我之前提到的一個聯(lián)網(wǎng)模塊,支撐GPRS和GSM功能,用來學習再適合不過了,我就直接丟篇博客了

嵌入式外設集 -- GSM+GPRS聯(lián)網(wǎng)模塊(GA6-B)icon-default.png?t=N7T8https://blog.csdn.net/herui_2/article/details/130560784?spm=1001.2014.3001.5502

三、代碼編寫

1. C 程序驗證代碼

該程序可以將電話號碼和文本消息數(shù)據(jù)編碼為發(fā)送到手機的協(xié)議數(shù)據(jù)單元(PDU)格式。主功能體現(xiàn)在`to_pdu`函數(shù),它處理電話號碼反轉和Unicode數(shù)據(jù)轉換,然后將這些組合成PDU字符串。如果編碼成功,它將輸出PDU的長度和內(nèi)容;如果失敗,則輸出失敗信息

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// 判斷輸入字符串是否全部為數(shù)字字符。
int is_numeric(const char *input) {
    while (*input) {
        // 如果當前字符不是數(shù)字,則返回0(假)。
        if (!isdigit((unsigned char) *input)) return 0;
        input++;
    }
    // 如果所有字符都是數(shù)字,則返回1(真)。
    return 1;
}

// 將電話號碼中每兩位字符進行反轉。
char *two_bit_inversion(const char *phone) {
    // 如果輸入字符串包含非數(shù)字字符,則返回NULL。
    if (!is_numeric(phone)) return NULL;
    size_t len = strlen(phone);
    // 為反轉后的電話號碼分配內(nèi)存。額外的空間是給'F'和'?'的。
    char *inverted_phone = malloc(len + 1 + (len % 2));

    if (inverted_phone == NULL) return NULL;

    // 如果電話號碼的長度為奇數(shù),則添加一個'F'。
    if(len % 2 == 1) {
        strcat(strcpy(inverted_phone, phone), "F"); // 如果長度為奇數(shù),則在末尾加上'F'
    } else {
        strcpy(inverted_phone, phone);
    }

    // 在字符串中交換相鄰字符的位置。
    for(size_t i = 0; i < len; i += 2) {
        char tmp = inverted_phone[i];
        inverted_phone[i] = inverted_phone[i + 1];
        inverted_phone[i + 1] = tmp;
    }

    return inverted_phone;
}

// 將輸入字符串轉換為Unicode字符串。
void string_to_unicode(const char *input, char *output) {
    while (*input) {
        // 將輸入字符串的每個字符轉換為Unicode,并存儲在輸出字符串中。
        sprintf(output, "%04X", (unsigned char)*input++);
        output += 4;
    }
}

// 從電話號碼和數(shù)據(jù)中構建PDU(協(xié)議數(shù)據(jù)單元)字符串。
char *to_pdu(const char *phone, const char *data) {
    // 利用two_bit_inversion函數(shù)轉換電話號碼。
    char *pdu_phone = two_bit_inversion(phone);
    // 如果轉換失敗,則返回NULL。
    if (!pdu_phone) return NULL;

    size_t len = strlen(data);
    char unicode_data[len * 4 + 1];
    // 將數(shù)據(jù)轉換為Unicode編碼。
    string_to_unicode(data, unicode_data);

    // 計算PDU消息中數(shù)據(jù)的長度。
    int data_length = strlen(unicode_data) / 2;
    // 計算PDU消息的總長度。
    size_t pdu_length = strlen("0011000D9168") + strlen(pdu_phone) + strlen("0008FF") + 2 + strlen(unicode_data) + 1;
    char *message = malloc(pdu_length);

    // 如果消息分配失敗,則釋放電話號碼內(nèi)存并返回NULL。
    if (message == NULL) {
        free(pdu_phone);
        return NULL;
    }

    // 構建PDU消息。
    sprintf(message, "0011000D9168%s0008FF%02X%s", pdu_phone, data_length, unicode_data);

    // 釋放電話號碼內(nèi)存。
    free(pdu_phone);
    // 返回PDU消息字符串。
    return message;
}

int main() {
    const char *phone = "15883813998";
    const char *data = "999999";

    // 調用to_pdu函數(shù)并打印結果。
    char *pdu = to_pdu(phone, data);
    if (pdu) {
        // 打印NUM,表示PDU的長度。
        printf("NUM %dn", strlen(pdu)/2-1);
        // 打印PDU。
        printf("PDU: %s", pdu);

        // 使用完后記得釋放內(nèi)存。
        free(pdu);
    } else {
        // 如果編碼PDU失敗,則打印失敗信息。
        printf("Failed to encode PDU.n");
    }

    return 0;
}

2. STM32移植

main.c

移植的時候需要注意,這個C代碼再keil環(huán)境里面是跑不成功的,需要在 VSCode 或者其他編譯器繼續(xù)編譯生成hex文件,代碼本身沒有問題,只是keil不能格式兼容。

pdu.c

#include "pdu.h"

// 判斷輸入字符串是否全部為數(shù)字字符。
int is_numeric(const char *input)
{
    while (*input)
    {
        // 如果當前字符不是數(shù)字,則返回0(假)。
        if (!isdigit((unsigned char)*input))
            return 0;
        input++;
    }
    // 如果所有字符都是數(shù)字,則返回1(真)。
    return 1;
}

// 將電話號碼中每兩位字符進行反轉。
char *two_bit_inversion(const char *phone)
{
    int len;
    char *inverted_phone;
    int i;
    char tmp;

    // 如果輸入字符串包含非數(shù)字字符,則返回NULL。
    if (!is_numeric(phone))
        return NULL;
    len = strlen(phone);
    // 為反轉后的電話號碼分配內(nèi)存。額外的空間是給'F'和'?'的。
    inverted_phone = malloc(len + 1 + (len % 2));

    if (inverted_phone == NULL)
        return NULL;

    // 如果電話號碼的長度為奇數(shù),則添加一個'F'。
    if (len % 2 == 1)
    {
        strcat(strcpy(inverted_phone, phone), "F"); // 如果長度為奇數(shù),則在末尾加上'F'
    }
    else
    {
        strcpy(inverted_phone, phone);
    }

    // 在字符串中交換相鄰字符的位置。
    for (i = 0; i < len; i += 2)
    {
        tmp = inverted_phone[i];
        inverted_phone[i] = inverted_phone[i + 1];
        inverted_phone[i + 1] = tmp;
    }

    return inverted_phone;
}

// 將輸入字符串轉換為Unicode字符串。
void string_to_unicode(const char *input, char *output)
{
    while (*input)
    {
        // 將輸入字符串的每個字符轉換為Unicode,并存儲在輸出字符串中。
        sprintf(output, "%04X", (unsigned char)*input++);
        output += 4;
    }
}

// 從電話號碼和數(shù)據(jù)中構建PDU(協(xié)議數(shù)據(jù)單元)字符串。
char *to_pdu(const char *phone, const char *data)
{
    char *pdu_phone;
    int len;
    int data_length;
    int pdu_length;
    char *message;
    // 利用two_bit_inversion函數(shù)轉換電話號碼。
    pdu_phone = two_bit_inversion(phone);
    // 如果轉換失敗,則返回NULL。
    if (!pdu_phone)
        return NULL;

    len = strlen(data);
    char unicode_data[len * 4 + 1];

    // 將數(shù)據(jù)轉換為Unicode編碼。
    string_to_unicode(data, unicode_data);

    // 計算PDU消息中數(shù)據(jù)的長度。
    data_length = strlen(unicode_data) / 2;
    // 計算PDU消息的總長度。
    pdu_length = strlen("0011000D9168") + strlen(pdu_phone) + strlen("0008FF") + 2 + strlen(unicode_data) + 1;
    message = malloc(pdu_length);

    // 如果消息分配失敗,則釋放電話號碼內(nèi)存并返回NULL。
    if (message == NULL)
    {
        free(pdu_phone);
        return NULL;
    }

    // 構建PDU消息。
    sprintf(message, "0011000D9168%s0008FF%02X%s", pdu_phone, data_length, unicode_data);

    // 釋放電話號碼內(nèi)存。
    free(pdu_phone);
    // 返回PDU消息字符串。
    return message;
}

pdu.h

#ifndef __PUD_H
#define __PUD_H
// C 庫
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// 定義電話號碼和消息(必須是英文)
int is_numeric(const char *input);
char *two_bit_inversion(const char *phone);
void string_to_unicode(const char *input, char *output);
char *to_pdu(const char *phone, const char *data);

#endif

3. Android 驗證代碼

就是通過朋友寫的這個java文件,才有靈感去做的這個格式轉換,這里也發(fā)給大家做個參考


object PduConvert {
    fun toPdt(phone: String, data: String): PduData? {
        val headers = "0011000D9168"
        val pduPhone = twoBitInversion(phone) ?: return null
        val unicodeData = stringToUnicode(data)
        val dataLength = (unicodeData.length / 2).toString(16).padStart(2, '0')
        val message =
            (headers + pduPhone + "0008FF" + dataLength + unicodeData).uppercase(Locale.getDefault())
        return PduData(data.length * 2 + 15, message)
    }

    private fun twoBitInversion(data: String): String? {
        var phone = data
        return if (isNumeric(phone)) {
            if (phone.length % 2 == 1) phone += "F"
            phone.chunked(2) // 每兩位一組進行分割
                .joinToString("") { it.reversed() } // 每組內(nèi)的字符翻轉后拼接到一起
        } else
            null
    }

    private fun isNumeric(input: String): Boolean {
        val regex = Regex("""^d+$""")
        return regex.matches(input)
    }

    fun stringToUnicode(input: String): String {
        val stringBuilder = StringBuilder()
        for (char in input) {
            stringBuilder.append("u").append(char.toInt().toString(16).padStart(4, '0'))
        }
        return stringBuilder.toString().replace("u", "")
    }

    fun unicodeToString(input: String): String {
        val stringBuilder = StringBuilder()
        input.chunked(4).forEach {
            val charCode = Integer.parseInt(it, 16)
            stringBuilder.append(charCode.toChar())
        }
        return stringBuilder.toString()
    }
}

?四、參考

SMS短信的PDU編碼規(guī)則icon-default.png?t=N7T8https://blog.csdn.net/zx249388847/article/details/52597990/?ops_request_misc=&request_id=&biz_id=102&utm_term=%E4%BF%A1%E6%81%AF%E8%BD%AC%E6%8D%A2%E4%B8%BAPUD%E6%A0%BC%E5%BC%8F%E5%8F%91%E9%80%81%E7%9F%AD%E4%BF%A1&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-5-52597990.nonecase&spm=1018.2226.3001.4450


請關注公眾號進行獲取和咨詢


**? 轉載請注明出處

聯(lián)系方式 微信號:13648103287

  • 聯(lián)系方式.docx

推薦器件

更多器件
器件型號 數(shù)量 器件廠商 器件描述 數(shù)據(jù)手冊 ECAD模型 風險等級 參考價格 更多信息
AD73311ARSZ 1 Analog Devices Inc Single-Channel, 3 V and 5 V Front-End Processor for General Purpose Applications Including Speech and Telephony

ECAD模型

下載ECAD模型
$10.72 查看
AT27C256R-70JU-306 1 Microchip Technology Inc IC OTP 256KBIT 70NS 32PLCC
暫無數(shù)據(jù) 查看
511FBA125M000BAG 1 Silicon Laboratories Inc LVDS Output Clock Oscillator,

ECAD模型

下載ECAD模型
$3.39 查看

相關推薦

電子產(chǎn)業(yè)圖譜

方案定制,程序設計方案、單片機程序設計與講解、APP定制開發(fā)。本公眾號致力于向讀者傳遞關于程序設計和開發(fā)的相關知識,并分享一些關于軟件開發(fā)的最佳實踐。如果您有什么問題或建議,請隨時聯(lián)系我們。我們將竭誠為您服務