如何使用AT命令发送/接收短信?

发布于 2024-08-19 13:35:37 字数 131 浏览 7 评论 0原文

谁能帮我在Python中使用AT命令发送和接收短信?

如果重要的话,我正在使用 Fedora 8。

哪款手机使用 Linux 会更好(诺基亚、索尼爱立信、三星……)? 所有手机都支持使用 AT 命令发送和接收短信吗?

Can anyone help me to send and receive SMS using AT commands in Python?

In case it matters, I'm using Fedora 8.

Which phone will be better with Linux (Nokia, Sony Ericson, Samsung,.....)?
Will all phones support sending and receiving SMS using AT commands?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我也只是我 2024-08-26 13:35:37

下面是一些可以帮助您入门的示例代码(在 Python 3000 中):

import time
import serial

recipient = "+1234567890"
message = "Hello, World!"

phone = serial.Serial("/dev/ttyACM0",  460800, timeout=5)
try:
    time.sleep(0.5)
    phone.write(b'ATZ\r')
    time.sleep(0.5)
    phone.write(b'AT+CMGF=1\r')
    time.sleep(0.5)
    phone.write(b'AT+CMGS="' + recipient.encode() + b'"\r')
    time.sleep(0.5)
    phone.write(message.encode() + b"\r")
    time.sleep(0.5)
    phone.write(bytes([26]))
    time.sleep(0.5)
finally:
    phone.close()

您需要做另外两件事:

  • 以适当的格式对消息进行编码(主要是 GSM 03.38,有一个方便的 unicode.org 上的翻译表)。如果您确实不关心 ASCII 以外的任何字符,则可以检查每个字符是否都在 string.printable 中。

  • 检查消息的长度(我不确定是否与编码有关,但有时是 140 个字符,有时是 160)。

您可以使用 phone.readall() 检查错误,但最好在将消息发送到手机之前确保消息正确。还要注意,睡眠似乎是必要的。

大多数手机都会理解这一点。为了让我的旧诺基亚 C5 打开串行连接,我必须从插入 USB 电缆时弹出的菜单中选择“PC 套件”。这在蓝牙上应该同样有效。

该代码使用 PySerial 包,适用于 python 2 和 3。

另请参阅:

Here's some example code that should get you started (in Python 3000):

import time
import serial

recipient = "+1234567890"
message = "Hello, World!"

phone = serial.Serial("/dev/ttyACM0",  460800, timeout=5)
try:
    time.sleep(0.5)
    phone.write(b'ATZ\r')
    time.sleep(0.5)
    phone.write(b'AT+CMGF=1\r')
    time.sleep(0.5)
    phone.write(b'AT+CMGS="' + recipient.encode() + b'"\r')
    time.sleep(0.5)
    phone.write(message.encode() + b"\r")
    time.sleep(0.5)
    phone.write(bytes([26]))
    time.sleep(0.5)
finally:
    phone.close()

You need to do two additional things:

  • Encode the message in the appropriate format (mostly GSM 03.38, there's a handy translation table at unicode.org). If you really don't care about any characters other than ASCII, you can just check if every character is in string.printable.

  • Check the length of the message (I'm not sure if it's to do with the encoding, but it's sometimes 140 characters, sometimes 160).

You can use phone.readall() to check for errors, but it's best to make sure your message is OK before you send it off to the phone. Note also that the sleeps seem to be necessary.

Most phones will understand this. In order to get my old Nokia C5 to open up the serial connection, I had to select "PC Suite" from the menu that pops up when you insert the USB cable. This should work equally well over Bluetooth.

The code uses the PySerial package, available for python 2 and 3.

See also:

完美的未来在梦里 2024-08-26 13:35:37

查看使用 At 命令发送短信这会有所帮助。

    import serial
    import time

    class TextMessage:
        def __init__(self, recipient="+2348065777685", message="TextMessage.content not set."):
            self.recipient = recipient
            self.content = message

        def setRecipient(self, number):
            self.recipient = number

        def setContent(self, message):
            self.content = message

        def connectPhone(self):
            self.ser = serial.Serial('COM70', 460800, timeout=5, xonxoff = False, rtscts = False, bytesize = serial.EIGHTBITS, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE)
            time.sleep(1)

        def sendMessage(self):
            self.ser.write('ATZ\r')
            time.sleep(1)
            self.ser.write('AT+CMGF=1\r')
            time.sleep(1)
            self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
            time.sleep(1)
            self.ser.write(self.content + "\r")
            time.sleep(1)
            self.ser.write(chr(26))
            time.sleep(1)

        def disconnectPhone(self):
            self.ser.close()

    sms = TextMessage("+2348063796720","Mummy i sent this message from my computer")
    sms.connectPhone()
    sms.sendMessage()
    sms.disconnectPhone()
    print "message sent successfully"

要使用 At 命令接收短信,这应该有所帮助

    import serial
    import time
    import sys


    class HuaweiModem(object):

        def __init__(self):
            self.open()

        def open(self):
            self.ser = serial.Serial('COM70', 406800, timeout=5)
            self.SendCommand('ATZ\r')
            self.SendCommand('AT+CMGF=1\r')


        def SendCommand(self,command, getline=True):
            self.ser.write(command)
            data = ''
            if getline:
                data=self.ReadLine()
            return data 

        def ReadLine(self):
            data = self.ser.readline()
            print data
            return data 



        def GetAllSMS(self):
            self.ser.flushInput()
            self.ser.flushOutput()



            command = 'AT+CMGL="REC UNREAD"\r\n'#gets incoming sms that has not been read
            print self.SendCommand(command,getline=True)
            data = self.ser.readall()
            print data


    h = HuaweiModem()
    h.GetAllSMS()

to see send sms using At command this will help.

    import serial
    import time

    class TextMessage:
        def __init__(self, recipient="+2348065777685", message="TextMessage.content not set."):
            self.recipient = recipient
            self.content = message

        def setRecipient(self, number):
            self.recipient = number

        def setContent(self, message):
            self.content = message

        def connectPhone(self):
            self.ser = serial.Serial('COM70', 460800, timeout=5, xonxoff = False, rtscts = False, bytesize = serial.EIGHTBITS, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE)
            time.sleep(1)

        def sendMessage(self):
            self.ser.write('ATZ\r')
            time.sleep(1)
            self.ser.write('AT+CMGF=1\r')
            time.sleep(1)
            self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
            time.sleep(1)
            self.ser.write(self.content + "\r")
            time.sleep(1)
            self.ser.write(chr(26))
            time.sleep(1)

        def disconnectPhone(self):
            self.ser.close()

    sms = TextMessage("+2348063796720","Mummy i sent this message from my computer")
    sms.connectPhone()
    sms.sendMessage()
    sms.disconnectPhone()
    print "message sent successfully"

To recieve sms using At command this should help

    import serial
    import time
    import sys


    class HuaweiModem(object):

        def __init__(self):
            self.open()

        def open(self):
            self.ser = serial.Serial('COM70', 406800, timeout=5)
            self.SendCommand('ATZ\r')
            self.SendCommand('AT+CMGF=1\r')


        def SendCommand(self,command, getline=True):
            self.ser.write(command)
            data = ''
            if getline:
                data=self.ReadLine()
            return data 

        def ReadLine(self):
            data = self.ser.readline()
            print data
            return data 



        def GetAllSMS(self):
            self.ser.flushInput()
            self.ser.flushOutput()



            command = 'AT+CMGL="REC UNREAD"\r\n'#gets incoming sms that has not been read
            print self.SendCommand(command,getline=True)
            data = self.ser.readall()
            print data


    h = HuaweiModem()
    h.GetAllSMS()
亣腦蒛氧 2024-08-26 13:35:37

与电话交谈很容易。您只需打开适当的 /dev/ttyACM* 设备并与其交谈。哪个手机比较麻烦。任何支持“网络共享”和 SMS 消息的完整 AT 命令集的手机都应该没问题。

Talking to the phone is easy. You just need to open the appropriate /dev/ttyACM* device and talk to it. Which phone is trickier. Any phone that supports "tethering" and the full AT command set for SMS messages should be fine.

玉环 2024-08-26 13:35:37

我建议将 time.sleep 替换为条件循环,等待调制解调器“OK”的响应,然后再继续下一个状态。

I would suggest replace the time.sleep with condition loop waiting for the response from the modem "OK" before continue next state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文