0

I send SMS via GSM device, try this: https://stackoverflow.com/a/31361225/6250402

It can send normal SMS, I try to send a Unicode text but It auto-convert to ASCII: VD¥n ba:#n D_a:?n: TBM01

Lib:

<dependency>
    <groupId>com.neuronrobotics</groupId>
    <artifactId>nrjavaserial</artifactId>
    <version>5.1.1</version>
</dependency>

My code:

CommPortIdentifier portId = "I get CommPort match name";
SerialPort serialPort = portId.open("name", 2000);
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, 
        SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
this.outputStream = serialPort.getOutputStream();

// each command function
private void send(String cmd) throws IOException {
    outputStream.write(cmd.getBytes(StandardCharsets.UTF_8)); //
    outputStream.flush();
}

// this sends message method

 send("AT+CMGS=" + '"' + phone + '"' + "\r\n");
 send(message + '\032');

Did I have the wrong config?

Any code to send SMS via GSM for java is a good answer.

Result I missing AT+CSMP=1,167,0,8 (https://stackoverflow.com/a/15312611/6250402, to more info, look https://www.smssolutions.net/tutorials/gsm/sendsmsat/)

hong4rc
  • 3,999
  • 4
  • 21
  • 40

3 Answers3

1

To send unicode text ,first the charset to be selected as UTF-16 (UCS2)

AT+CSCS="UCS2"

After you set AT+CSCS="UCS2" every single string parameter you send must be encoded to UCS2 format ,

You canrefer the blow utilty to enCode to UCS2 http://d-chips.blogspot.com/2012/06/coding-of-alpha-fields-in-sim-for-ucs2.html

Rajesh Gopu
  • 863
  • 9
  • 33
1

I've an example project of GSM device communication in Java which I run in a raspberry attached to a SIMCom800L module. It uses jSerialComm for serial communications which works nice with no configuration at all. Please take a look to my repo: https://github.com/pepevalbe/sms-gateway

To send Unicode you need to configure your GSM device Character Set. Check possible sets with this command:

AT+CSCS=?
+CSCS: ("GSM","UCS2","IRA","HEX")

If the answer contains "HEX" or "UCS2", Unicode seems to be supported. I tried with UCS2 and it is working nice. Just change the Character Set with: AT+CSCS="UCS2" This will only affect to sms, not regular commands.

Now your device will properly recognize the unicode strings you send from Java. Don't forget to set the sms text mode as well: AT+CMGF=1

I give you a full example based on my repo:

//SIMCom serial port configuration: 115200 bps, 8 bit data, no parity, 1 bit stop, no data stream control
SerialPort serialPort = SerialPort.getCommPort("serial0");
serialPort.setComPortParameters(115200, 8, ONE_STOP_BIT, NO_PARITY);
serialPort.setFlowControl(FLOW_CONTROL_DISABLED);
serialPort.setComPortTimeouts(TIMEOUT_READ_BLOCKING, 3000, 0);
serialPort.openPort();

// Set sms text mode
String textModeCommand = "AT+CMGF=1\r";
serialPort.writeBytes(textModeCommand.getBytes(StandardCharsets.UTF_8), textModeCommand.length());

// Set UCS2 charset
String characterSetCommand = "AT+CSCS=\"UCS2\"\r";
serialPort.writeBytes(characterSetCommand.getBytes(StandardCharsets.UTF_8), characterSetCommand.length());

// Send sms command
String sendTextCommand1 = "AT+CMGS=\"" + number + "\"\n";
String sendTextCommand2 = text + (char) 26 + "\r";
serialPort.writeBytes(sendTextCommand1.getBytes(StandardCharsets.UTF_8), sendTextCommand1.length());
serialPort.writeBytes(sendTextCommand2.getBytes(StandardCharsets.UTF_16), sendTextCommand2.length());

Notice that in the send sms command I encoded the text part using UTF_16 charset, not UTF_8

pepevalbe
  • 1,340
  • 6
  • 18
  • Call `AT+CSCS=?` and I got `+CSCS: ("GSM","PCCP437","CUSTOM","HEX")`, cannot set `AT+CSCS="UCS2"` – hong4rc Feb 18 '21 at 10:15
  • Go for HEX then. You need to encode the text string as hexadecimal. Check this out: https://www.smssolutions.net/tutorials/gsm/sendsmsat/ – pepevalbe Feb 18 '21 at 10:30
  • I think I have the wrong config or something, I sent `06450631062D06280627 ` like your example but got `ùEù1ù-ù(ù'` instead of `مرحبا` – hong4rc Feb 18 '21 at 10:39
  • I missing `AT+CSMP=1,167,0,8 ` – hong4rc Feb 19 '21 at 03:17
0

Following are the reasons need to be check in sending/receiving unicoded text

  • Make sure your reading the input in UTF-8 format, create InputstreamReader constructor with UTF-8 encoding

InputStream inputStream = new FileInputStream("data.txt");

InputStreamReader inputStreamReader =  new InputStreamReader(inputStream, Charset.forName("UTF-8"));

For more references : https://docs.oracle.com/javase/tutorial/i18n/text/stream.html

  • Use bytemessage methods instead of text message for sending/receiving unicode text

https://docs.oracle.com/javaee/7/api/javax/jms/BytesMessage.html