问题
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);
}
}
Output ***************************************************
----------------------At main---------------------- ----------------------At main 2---------------------- SMS Sending....Port Found ---------------message Writing-----------------------
140 OK
---------------case Data Available----------------------- ---------------line=reader.read----------------------- "SM",15
15 //I extract the 15 out of SM message //Keep that 15 for AT +CMGR=15,, returns this
**********REC Data************* "REC UNREAD","+Mobile no",,"14/01/23,16:02:06+22" -> Here the SMS Data Should come !!!
I have taken help from this question https://stackoverflow.com/a/12605880/2537945. but iam unable to read the incoming messages as it only returns me the REC ** Line but not the actual message which comes after this REC ** Line. Could you help me with any other solution or how to read Incoming messages in GSM Modem
回答1:
i am running this and i am sending sms
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");
}
}
}
来源:https://stackoverflow.com/questions/21306784/how-to-read-incoming-messages-from-gsm-modem-in-java