SIM808 之 中文短信 (一)-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8917|回复: 3

SIM808 之 中文短信 (一)

[复制链接]
发表于 2017-3-1 17:06 | 显示全部楼层 |阅读模式
    首先,非常感谢DFrobot提供的SIM808模块供我把玩,在此也说一声抱歉,这个中文短信的技术贴写的晚了不是一点点.........
    请允许我贴个图,给你们看看这个模块是有多么的漂亮!
    QQ截图20170301163314.png


    论坛中其实已经有了比较多的关于SIM808的创意小制作或者测评,包含了GPRS GPS GSM的各项功能,我这篇帖子主要是讲GSM这块中的短信服务,在我到手模块的时候,我发现其实很多人并不知道SIM808(或者SIM800又或者SIM900)这种GSM模块怎么进行一个中文短信的收发。本系列讲从SIM808入手,分成N个章节(N多少我自己也不知道),从中文短信的收,发方面入手,完善一下这一系列模块“尚未开发的”功能。(不是没开发,只是在Arduino方面写中文短信的Example实在少的可怜)


首先,我们先从收取短信方面入手,这里先不要脸的贴一段代码来凑字数!

[kenrobot_code]/*
### Read SMS messages
1. This example is used to test DFRobot_SIM808 GPS/GPRS/GSM Shield's reading SMS
2. Open the SIM808_SMSread example or copy these code to your project
3. Download and dial the function switch to Arduino
4. it will receive SMS Messages and send it to serial

create on 2016/09/23, version: 1.0
by jason
*/

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

#define MESSAGE_LENGTH 160
char message[MESSAGE_LENGTH];
int messageIndex = 0;

char phone[16];
char datetime[24];

//#define PIN_TX    10
//#define PIN_RX    11
//SoftwareSerial mySerial(PIN_TX,PIN_RX);
//DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

DFRobot_SIM808 sim808(&Serial);

void setup()
{
        //mySerial.begin(9600);
        Serial.begin(9600);

        //******** Initialize sim808 module *************
        while(!sim808.init())
        {
                Serial.print("Sim808 init error\r\n");
                delay(1000);
        }
          delay(3000);
        Serial.println("Init Success, please send SMS message to me!");
}

void loop()
{
        //*********** Detecting unread SMS ************************
        messageIndex = sim808.isSMSunread();
        Serial.print("messageIndex: ");
        Serial.println(messageIndex);

        //*********** At least, there is one UNREAD SMS ***********
        if (messageIndex > 0)
        {
                sim808.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);

                //***********In order not to full SIM Memory, is better to delete it**********
                sim808.deleteSMS(messageIndex);
                Serial.print("From number: ");
                Serial.println(phone);
                Serial.print("Datetime: ");
                Serial.println(datetime);
                Serial.print("Recieved Message: ");
                Serial.println(message);
        }
}[/kenrobot_code]

这一段代码是有关于DFrobot提供的有关于SIM808的短信接收程序,我们这里简单的下载一下程序,打开串口监视器看一下情况。

QQ截图20170301164232.png

我们发现,当没有短信的时候,串口监视器返回告诉我们短信目录为零,现在,我们通过另一台手机给这个模块发送Message。

QQ截图20170301164424.png

我们发现,这时候会提示我们短信目录为1,也就是有一条短信,可以返回发送的号码,时间还有内容(号码涉及隐私,涂了,如果是妹纸可以跟帖问我要。手动滑稽O(∩_∩)O)

因为我从手机端发送的是一个英文短信,所以我们发现能很完美的识别出短信的内容。


好了,以上内容都是凑字数的,这个Example有模块的人一定做过,啊哈哈哈......


现在,我们发送一条中文短信给目标号码,看看会返回什么结果?发送的短信内容是:你好,世界

QQ截图20170301164825.png

这时候,我们会发现,天哪,这是什么乱七八糟的数据。好,现在开始进入正题,这是什么数据?根据我一系列的查询文档加上各种平台的GSM教学视频,我得出结论,这是一段Unicode编码的中文短信。这时候我们打开百度,查询一下。

QQ截图20170301165037.png

仔细比较一下两者的数据,我们发现,其实短信内容和翻译出来的Unicode区别就在于省略的好多\u....

这时候我们就有思路了,Unicode是四位一组,每组前面加入\u即可形成Unicode编码(不是说Unicode中有\u的存在,只是用\u来表示这是一个Unicode的编码)

好,现在我们就需要一个上位机去“翻译”Unicode。

这里我选择的是Python,方便,快捷,暴力,轻松.....


这里我贴一个链接,相对简单的说明了Python转换Unicode的方式。

OK,现在我们整理一下思路。我们首先通过Python将还未处理的Unicode通过串口读进来,然后每四位前加一个\u,在通过Python的encode转码即可。

我这里直接帖程序,Python部分的代码如下:

[mw_shl_code=python,true]#!/usr/bin/python
# -*- coding: UTF-8 -*-

import serial
import msvcrt

ser = serial.Serial('COM5', 9600)  # 这里需要修改自己板子对应的COM口,这是WINDOWS环境下
# ser = serial.Serial('/dev/ttyS1', 9600)  # 这是Linux环境下,也需要修改对应的串口

def process(line):
    temp = line
    text = ""

    while temp:
        text = text + "\u" + temp[:4]
        temp = temp[4:]

    return text[:len(text) - len(text) % 6].decode('unicode-escape').encode('utf-8')


print "Press 'D' to exit..."

while True:
    print "短信内容是: " + process(ser.readline())

    if ord(msvcrt.getch()) in [68, 100]:
        break

ser.close()[/mw_shl_code]


Arduino端代码如下:

[kenrobot_code]/*
  ### Read SMS messages
  1. This example is used to test DFRobot_SIM808 GPS/GPRS/GSM Shield's reading SMS
  2. Open the SIM808_SMSread example or copy these code to your project
  3. Download and dial the function switch to Arduino
  4. it will receive SMS Messages and send it to serial

  create on 2016/09/23, version: 1.0
  by jason
*/

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

#define MESSAGE_LENGTH 160
char message[MESSAGE_LENGTH];
int messageIndex = 0;

char phone[16];
char datetime[24];

//#define PIN_TX    10
//#define PIN_RX    11
//SoftwareSerial mySerial(PIN_TX,PIN_RX);
//DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

DFRobot_SIM808 sim808(&Serial1);

void setup()
{
        //mySerial.begin(9600);
        Serial.begin(9600);
        Serial1.begin(9600);

        //******** Initialize sim808 module *************
        while (!sim808.init())
        {
                Serial.print("Sim808 init error\r\n");
                delay(1000);
        }
        delay(3000);
        Serial.println("Init Success, please send SMS message to me!");
}

void loop()
{
        messageIndex = sim808.isSMSunread();
        if (messageIndex > 0)
        {
                sim808.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
                Serial.println(message);
        }
}
[/kenrobot_code]

Arduino端我采用的是莱纳尔多,所以和SIM808通讯是建立在Serial1上的。


我没有添加什么区分短信内容的功能,所以正确的开机姿势是,首先将SIM808上点,程序运行,等待SIM808初始化完成后,再运行Python端的程序。

运行结果如下

Ubuntu 64 位-2017-03-01-11-46-00.png


至此,电脑端接收SIM808的中文短信内容到此结束。欢迎留言提问,我会继续完善这篇技术贴!

发表于 2017-3-1 19:03 | 显示全部楼层
哇塞  好久没看到桃子老师发帖子了   学习~~~支持一哈
发表于 2017-3-6 13:53 | 显示全部楼层
好鸡冻~~看到桃子老师的测评了
发表于 2017-3-14 22:16 | 显示全部楼层
本帖最后由 xflxfl 于 2017-3-14 22:19 编辑

是否可以提供下DFRobot_sim808.h的内容?----------------------
找到了https://github.com/DFRobot/DFRobot_SIM808/blob/master/DFRobot_sim808.cpp
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-9-21 14:28 , Processed in 0.081705 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表