python控制modem的at指令_python-gsmmodem

软件简介

Python实现的短信发送/接收库,支持多种型号的短信猫。主要功能有:

发送短信、检测信号强度

基于回调的来电和短信处理方法

支持短信状态报告追踪

使用Python异常包裹AT命令错误

模块化代码,具有良好的扩展能力

提供了一些简单的调试工具。

示例

短信接收和回复

#!/usr/bin/env python

"""\

Demo: handle incoming SMS messages by replying to them

Simple demo app that listens for incoming SMS messages, displays the sender's number

and the messages, then replies to the SMS by saying "thank you"

"""

from __future__ import print_function

import logging

PORT = '/dev/ttyUSB2'

BAUDRATE = 115200

PIN = None # SIM card PIN (if any)

from gsmmodem.modem import GsmModem

def handleSms(sms):

print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nMessage:\n{2}\n'.format(sms.number, sms.time, sms.text))

print('Replying to SMS...')

sms.reply(u'SMS received: "{0}{1}"'.format(sms.text[:20], '...' if len(sms.text) > 20 else ''))

print('SMS sent.\n')

def main():

print('Initializing modem...')

# Uncomment the following line to see what the modem is doing:

logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)

modem = GsmModem(PORT, BAUDRATE, smsReceivedCallbackFunc=handleSms)

modem.smsTextMode = False

modem.connect(PIN)

print('Waiting for SMS message...')

try:

modem.rxThread.join(2**31) # Specify a (huge) timeout so that it essentially blocks indefinitely, but still receives CTRL+C interrupt signal

finally:

modem.close();

if __name__ == '__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值