1

我正在使用 Java 使用 AT 命令从我的 3G 加密狗(GSM 调制解调器)发送 SMS。它大部分都在工作,但有时 SMS 文本包含在此之前触发的部分 AT 命令。这是间歇性的,但需要修复。

相关代码如下:

public void sendMessage(String phoneNumber, String message) throws InterruptedException {
    char qu=34;
    char cz=26;
    send("AT+CMGF=1\r\n");
    Thread.sleep(2000);
    send("AT+CMGS=" + qu + phoneNumber + qu + ",145\r\n");
    send(message + cz + "\r");
  }

public static void main(String args[]) {
    GSMConnect gsm = new GSMConnect("COM22");
    if (gsm.init()) {
      try {
        gsm.connect();
        Thread.sleep(2000);
        gsm.sendMessage("+9172xxxxxxxx", "Test Message sent from GSM Modem using AT Commands.");
        System.out.println("Sleeping for 20 secs");
        Thread.sleep(20000);
        gsm.hangup();
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else {
      System.out.println("Can't init this card");
    }
  }

我有时收到的消息如下:

AT+CMGS="+9172xxxxxxxx", 145 使用 AT 命令从 GSM 调制解调器发送的测试消息。

=============

在此先感谢您的帮助!

问候, 库马尔吉特

4

1 回答 1

0

Start by acquiring a large A3 sheet of paper, find a red pen and write 1000 times

I will never use Thread.sleep as a substitute for reading and parsing responses from a modem.

I will never use Thread.sleep as a substitute for reading and parsing responses from a modem.

I will never use Thread.sleep as a substitute for reading and parsing responses from a modem.

I will never use Thread.sleep as a substitute for reading and parsing responses from a modem.

I will never use Thread.sleep as a substitute for reading and parsing responses from a modem.

...

Then read this answer, following the instructions regarding V.250. Wait until you have properly digested all information from the answer before going back to fixing your code (it probably takes some time to let all sink in).

Of course the first part was meant to be funny, but I am dead serious about the rest; you have some huge AT command knowledge "holes" you must fill. It should not be very difficult, but it will require some effort.

While I cannot say exactly how parts of your AT commands ends up in the message content, the root cause of this is that you are not reading and parsing the modem responses like you should.


TL;DR: You must read and parse everything the modem sends back to you. Nothing else will work reliably.

于 2017-09-17T10:22:02.487 回答