1
votes

I am using Java to send SMS from my 3G dongle (GSM modem) using AT commands. It is mostly working, but at times the SMS text contains part of the AT commands fired prior to that. This is intermittent, but needs to be fixed.

The relevant code is as follows:

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");
    }
  }

The message I receive at times is as follows:

AT+CMGS="+9172xxxxxxxx", 145 Test Message sent from GSM Modem using AT Commands.

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

Thanks in advance for your help!

Regards, Kumarjit

1
Sounds like you have a multithreading problem. Do you accidentially run two instances of your program at once?Thorbjørn Ravn Andersen
That's correct. I had to send the same message to 2 persons. So I invoked the gsm.sendMessage twice. But, I had this problem even when I was sending it to a single number. There were some other commands being executed as well such as AT+CREG=? and ATZKumarjit Sen
You need to have a mechanism in place that guarantees that you only access COM22 from a single program at a time.Thorbjørn Ravn Andersen
Have u set baud rate i faced similar problem and send commands after a small delay else it will happen like this u have to get the timing of the delaycodefreaK
AT command lines should be terminated with only \r and not \r\n. stackoverflow.com/a/21503919/23118hlovdal

1 Answers

0
votes

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.