SIM800A模块使用

我使用的是SIM800A模块,USB转UART模块,树莓派给模块供电(理论上USB转UART也能给SIM800A供电)

1.安装对应系统CP2102 USB to UART Bridge Controller驱动程序
2.接线

(1)为串口,左RXTX (2)为5V供电,左VCCGND

通电后RING灯常亮NET灯闪烁说明工作正常


发送AT指令测试串口

使用python进行串口通信

# -*- coding: utf-8 -*-

import serial  
from utils import *


def init(p):  
    # 设置短信模式
    p.write('AT+CMGF=1\n'.encode())
    p.write('AT+CSCS="UCS2"\n'.encode())
    p.write('AT+CSCA?\n'.encode())
    p.write('AT+CSMP=17,167,0,25\n'.encode())
    p.flush()
    # 指示来电号码
    p.write('AT+CLIP=1\n'.encode())
    p.flush()


def process(p, line):  
    """
    :type p: serial.Serial
    :type line: bytes
    """
    line = line.decode()
    if line.startswith("+CMTI:"):
        print("收到短信")
        print("=======")
        p.write("AT+CMGR={}\n".format(line[12:-2]).encode())
    elif line.startswith("+CMGR:"):
        print("读取短信")
        print("=======")
        data = line[20:].split("\"")
        phone = unicode2str(data[1])
        time = data[5]
        content = unicode2str(p.readline().decode())
        print("{} [{}] \n{}".format(phone, time, content))
    elif line.startswith("RING"):
        print("来电")
        print("=======")
    elif line.startswith("+CLIP:"):
        print("来电号码")
        print("=======")
        print(line[8:-17])
    elif line.startswith("NO CARRIER"):
        print("挂断")
        print("=======")
    else:
        pass

port = serial.Serial(port='/dev/cu.SLAB_USBtoUART', baudrate=9600, bytesize=8, parity='N', stopbits=1)  
init(port)

while True:  
    res = port.readline()
    process(port, res)
"""utils.py"""
# -*- coding: utf-8 -*-

def str2unicode(s):  
    s = s.decode()
    u = ''
    for c in s:
        u += "%04X" % ord(c)
    return u

def unicode2str(u):  
    s = ''
    for i in range(int(len(u) / 4)):
        s += chr(int(u[i * 4: (i + 1) * 4], 16))
    return s