[英]How to read incoming messages from GSM Modem in JAVA
package pack1;
import java.io.*;
import java.util.*;
import javax.comm.*;
public class Java_SerialCommTest1 implements Runnable, SerialPortEventListener
{
public void run()
{}
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "My Message to be sent";
static char ch = '"';
static String dest = "*********"; // 10 Digit Mobile Number.
static InputStream inputStream;
static SerialPort serialPort;
static OutputStream outputStream;
public void serialEvent(SerialPortEvent event)
{
switch (event.getEventType())
{
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
{
try
{
inputStream = serialPort.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
line = reader.readLine();
while ( line != null)
{
System.out.println(line);
break;
}
if(line.contains("SM"))
{
System.out.println("if it contains SM");
String messagepos= line.substring(6);
System.out.println(messagepos);
String recievedmessage = "AT +CMGR=" + messagepos + "\r\n";
outputStream = serialPort.getOutputStream();
outputStream.write(recievedmessage.getBytes());
outputStream.write(13);
}
if(line.contains("REC"))
{
System.out.println("**********REC Data*************");
System.out.println(line);
}
}
catch (IOException e)
{
System.err.println("Error while reading Port " + e);
}
break;
}
} //switch
}
public Java_SerialCommTest1(SerialPort serial)
{
try
{
inputStream = serial.getInputStream();
System.out.println(inputStream);
try
{
serial.addEventListener(this);
serial.notifyOnDataAvailable(true);
}
catch (TooManyListenersException e)
{
System.out.println("Exception in Adding Listener" + e);
}
}
catch (Exception ex)
{
System.out.println("Exception in getting InputStream" + ex);
}
}
public static void main(String[] args) throws NoSuchPortException, IOException
{
String line1 = "AT +CMGF=1\r\n";
String line2 = "AT +CMGS=" + dest + "\r\n";
String line3 = messageString + "\r\n";
portList=CommPortIdentifier.getPortIdentifiers();
System.out.println(portList);
// System.out.println(portId);
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if ( portId.getName().equals("COM6"))
{
System.out.println("SMS Sending....Port Found");
try
{
serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
Java_SerialCommTest1 wr = new Java_SerialCommTest1(serialPort);
}
catch (PortInUseException e)
{
System.out.println("Port In Use " + e);
}
try
{
outputStream = serialPort.getOutputStream();
}
catch (IOException e)
{
System.out.println("Error writing to output stream " + e);
}
try
{
serialPort.setSerialPortParam (4800,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e)
{
}
try
{
outputStream.write(line1.getBytes());
outputStream.write(line1.getBytes());
outputStream.write(line2.getBytes());
outputStream.write(line3.getBytes());
outputStream.write(26);
}
catch (Exception e)
{
System.out.println("Error writing message " + e);
}
finally
{
outputStream.flush();
}
}
}
}
}
/** show text in the text window
* @param Text text string to show on the display
*/
public static void showText(String Text)
{
System.out.println(Text);
}
}
輸出****************************************************** **
----------------------主要---------- ------------------主2 ---------------------- SMS發送中...端口找到了---------------消息寫作-----------------------
140好
---------------案例數據可用----------------------- --------- ------ line = reader.read -----------------------“ SM”,15
15 //我從SM消息中提取15 // //對於AT + CMGR = 15保持15,返回
********** REC數據*************“ REC UNREAD”,“ + Mobile no”,“ 14/01 / 23,16:02:06+ 22“-> SMS數據應該到了!!!
我從這個問題https://stackoverflow.com/a/12605880/2537945獲得了幫助。 但我無法讀取傳入的消息,因為它只會向我返回REC **行,而不能返回此REC **行之后的實際消息。 您能否通過其他解決方案或如何在GSM調制解調器中閱讀傳入消息來幫助我
我正在運行這個,我正在發送短信
public class PScanner implements SerialPortEventListener, Runnable {
CommPortIdentifier pid = null;
SerialPort sp;
BufferedReader input;
OutputStream output;
static char ch = '"';
static String dest = ch + "**********" + ch;
String line1 = "AT+CMGF=1\r\n";
String line2 = "AT+CMGS=" + dest + "\r\n";
String line3 = "hello" + "\r\n";
public PScanner() {
new Thread(this).start();
}
public void read() {
try {
Enumeration e = CommPortIdentifier.getPortIdentifiers();
while (e.hasMoreElements()) {
CommPortIdentifier cpi = (CommPortIdentifier) e.nextElement();
if (cpi.getName().equals("COM11")) {
pid = cpi;
break;
}
}
sp = (SerialPort) pid.open(getClass().getName(), 2000);
sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream is = sp.getInputStream();
input = new BufferedReader(new InputStreamReader(is));
output = sp.getOutputStream();
output.write(line1.getBytes());
output.write(line1.getBytes());
output.write(line2.getBytes());
output.write(line3.getBytes());
output.write(26);
output.flush();
sp.addEventListener(this);
sp.notifyOnDataAvailable(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public synchronized void serialEvent(SerialPortEvent oEvent) {
System.out.println("serialEvent CallBack");
try{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE){
String line = "";
while ((line = input.readLine())!= null) {
System.out.println(line);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public synchronized void close() {
if (sp != null) {
System.out.println("not null");
sp.removeEventListener();
sp.close();
}
}
@Override
public void run() {
read();
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(PScanner.class.getName()).log(Level.SEVERE, null, ex);
} finally {
System.out.println("done");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.