First, make sure that the SMS cat is connected to the host normally and install the SIM card.
Use a Super Terminal to test whether the SMS cat can be used.
Install minicom:
# Sudo apt-get install minicom
After the installation is complete, run
# Sudo minicom-s
Package com. sendSMS; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. IOException; import jssc. serialPort; import jssc. serialPortEvent; import jssc. serialPortEventListener; import jssc. serialPortException; import jssc. serialPortList; public class Sender {private String msgCenterPhone; private String portName; private int baud; private int parity; private int stop; private int data; Private int gsmType; private SerialPort serialPort;/** AT + CMGS = */public final static byte [] SMS_STRUCTOR = {0x41, 0x54, 0x2B, 0x43, 0x4D, 0x47, 0x53, 0x3D};/** AT + CMGF = 0 */public final static byte [] SMS_PDU_HEADER = {0x41, 0x54, 0x2B, 0x43, 0x4D, 0x47, 0x46, 0x3D, 0x30}; public final static byte [] SMS_HEADER_START = {0x41, 0x54, 0x2B, 0x43, 0x4D, 0x47, 0x53, 0x3D}; public final static byte [] SMS_HEA DER_END = {0x0D}; public final static byte [] SMS_CONTENT_END = {0x1A}; public void setCenterPhone (String phoneNumber) {this. msgCenterPhone = phoneNumber;} public void setSerialPort (String portName) {this. portName = portName;} public void setParams (int baudRate, int parity, int stop, int data) {this. baud = baudRate; this. parity = parity; this. stop = stop; this. data = data;} public void setGSMType (int gsmTy Pe) {this. gsmType = gsmType;} public Sender () {// msgCenterPhone = msgCenterPhone. trim (); portName = "COM10"; baud = 115200; parity = 0; stop = 1; data = 8; gsmType = 2;} public Sender (String phoneNo, String portName, int gsmType) {msgCenterPhone = phoneNo; this. portName = portName; baud = 115200; parity = 0; stop = 1; data = 8; this. gsmType = gsmType;}/*** confirm the sending format and send the short message. The long message is divided into N normal messages * @ param mobileSendAlarmInfo *@ Return 1: sent successfully;-1: failed to send */public int sendSMS (String destPhone, String content) {/*** success mark */int succeedFlag = 0; serialPort = new SerialPort (this. portName); try {System. out. println ("Port opened:" + serialPort. openPort (); System. out. println ("Params setted:" + serialPort. setParams (115200, 8, 1, 0); System. out. println ("Send PDU Header" + serialPort. writeBytes (SMS_PDU_HEADER); System. out. println ("Send Hea Der End "+ serialPort. writeBytes (SMS_HEADER_END); int timeout = 0; while (true) {Thread. sleep (100); byte [] B = new byte [21]; B = serialPort. readBytes (); if (timeout> 50) {System. out. println ("Time Out"); return succeedFlag =-1;} if (B = null) {timeout ++; continue;} String s = new String (B ); if (s. indexOf ("OK ")! =-1) {// 'OK' System. out. println ("Set PDU Successful! "); Break ;}} int [] tpduLength = new int [1]; String pack = packStrMessage (destPhone, content, msgCenterPhone, tpduLength); System. out. println ("Send Header Start" + serialPort. writeBytes (SMS_HEADER_START); String strLength = (tpduLength [0] <100? ("0" + Integer. toString (tpduLength [0]): Integer. toString (tpduLength [0]); System. out. println ("Send Header Length" + serialPort. writeBytes (strLength. getBytes (); System. out. println ("Send Header End" + serialPort. writeBytes (SMS_HEADER_END); Thread. sleep (100); timeout = 0; while (true) {Thread. sleep (100); byte [] B = new byte [21]; B = serialPort. readBytes (); if (timeout> 50) {System. out. println ("Time Out "); Return succeedFlag =-1;} if (B = null) {timeout ++; continue;} String s = new String (B); if (s. indexOf ('> ')! =-1) {// '> 'System. out. println ("Title Successful! "); Break;} if (s. indexOf (" ERROR ")! =-1) {// 'R' System. out. println ("Error"); return succeedFlag =-1 ;}} System. out. println ("Send Content:" + serialPort. writeBytes (pack. getBytes (); System. out. println ("Send Contend End:" + serialPort. writeBytes (SMS_CONTENT_END); System. out. println ("Send Header End:" + serialPort. writeBytes (SMS_HEADER_END); timeout = 0; while (true) {Thread. sleep (100); byte [] B = new byte [21]; B = serialPort. readBytes (); If (timeout> 50) {System. out. println ("Time Out"); return succeedFlag =-1;} if (B = null) {timeout ++; continue;} String s = new String (B ); system. out. println (s); // System. exit (0); if (s. indexOf ("OK ")! =-1) {// '> 'System. out. println ("Content Successful! "); SerialPort. closePort (); return succeedFlag = 1;} if (s. indexOf (" ERROR ")! =-1) {// 'R' System. out. println ("Error"); return succeedFlag =-1 ;}} catch (SerialPortException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return succeedFlag;} private String packStrMessage (String destPhone, String content, String centerPhone, int [] tpduLength) {if (destPhone = null | content = Null | centerPhone = null) return ""; // encode the SCAbyte [] sca = new byte [3]; sca [0] = (byte) (0x00); sca [1] = (byte) 0x11; sca [2] = (byte) 0x00; // encode TPDUif (! DestPhone. startsWith ("86") // add china code 86 to dest phone. 81 is the Japanese Code destPhone = "86" + destPhone; int length; int destLength = length = destPhone. length (); if (length % 2! = 0) {// if the dest code is odd, then add 'F' destPhone + = "F"; length ++ ;} // content + = new String (new char [] {0x00}); char [] ccontent = content. toCharArray (); int contentLength = ccontent. length;/* Data Length * Dest Phone (encoding) | Data (encoding) * _________ | __________ | _____ | ________ * The tpdu 'sample byte 11 00 0D 91 68 31 19 01 28 14 F1 00 00 00 05 D4 E2 94 0A 02 * | | * | TON/NPI | VP Validity Period * | Dest Phone Length (Number) | DCS Data Coding Scheme (00-ASCII, 08-UCS2) * | MR Message Reference PID Protocol ID * 8 Bits MTI, RD, VPF, SRR, UDHI, RP, MMS, SRI * so 4 + phoneLength/2 + 3 + 1 + contenLength is the tpdu length */byte [] tpdu = new byte [2 + length/2 + 3 + 1 + contentLength * 2]; int index = 0; tpdu [index ++] = (byte) destLength; tpdu [index ++] = (byte) 0x91; for (int I = 0; I <length; I + = 2) {tpdu [index ++] = swap (destPhone. substring (I, I + 2), 16);} tpdu [index ++] = 0x00; tpdu [index ++] = 0x08; // UCS2tpdu [index ++] = (byte) 0xA7; tpdu [index ++] = (byte) (contentLength * 2); for (int I = 0; I <contentLength; I ++) {if (I <ccontent. length) {tpdu [index ++] = (byte) (ccontent [I]> 8); tpdu [index ++] = (byte) (ccontent [I] & 0x00FF);} else {tpdu [index ++] = (byte) (0xFF); tpdu [index ++] = (byte) (0xFF) ;}}// copy sca and tpdu to retint scaLength = sca. length; tpduLength [0] = tpdu. length + 2; byte [] ret = new byte [scaLength + tpdu. length]; System. arraycopy (sca, 0, ret, 0, scaLength); System. arraycopy (tpdu, 0, ret, scaLength, tpdu. length); String r = new String (); for (int I = 0; I <ret. length; I ++) {String s = Integer. toHexString (char) ret [I] & 0x00ff); if (s. length () = 1) s = "0" + s; r + = s;} // System. out. println (r); return r;} // swap the string and parse it as specail radix byte, just same binary code. // note: the s must has two length string. // eg. swap ("12", 16) will return byte 33 // eg. swap ("68", 16) will return byte-122 public static byte swap (String s, int radix) {byte b1 = Byte. parseByte (s. substring (0, 1), radix); byte b2 = Byte. parseByte (s. substring (1, 2), radix); return (byte) (b2 * radix + b1);} public String toString () {StringBuffer sb = new StringBuffer (); sb. append ("The SMS Properties: \ n"); sb. append ("\ nSerial Port =" + serialPort); sb. append ("\ nBaud =" + baud); sb. append ("\ nData Bits =" + data); sb. append ("\ nStop Bit =" + stop); sb. append ("\ nParity =" + parity); sb. append ("\ nSMS Center Phone =" + msgCenterPhone); return sb. toString ();} public static void main (String [] args) {Sender sender = new Sender ("13800210500", "/dev/ttyS0", 2 ); // The SMS center number, port number, and SIM card type in the region where the SIM card is located. The default value is 2. int code = sender. sendSMS ("xxxxxxxxxxx", "Just for a test"); // The receiving number and content System. out. println (code );}}
Compile or not. Check the log. Okay, it's a permission issue. Then run it in the Super Terminal:
# Sudo chmod 777/dev/ttyS0
Re-compile, the phone received a text message, OK.
In Windows, replace the port number with "COM1.