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 ---------------------- 短信发送....发现端口 ---------------留言写作-----------------------
140 行
---------------案例数据可用----------------------- ---------------线= reader.read ----------------------- “SM”,15
15 //我从SM消息中提取了15个 //保持15为AT + CMGR = 15,返回此
********** REC数据************* “REC UNREAD”,“+ Mobile no”,“14/01 / 23,16:02:06 + 22” - >这里的SMS数据应该来了!!!
我从这个问题https://stackoverflow.com/a/12605880/2537945获得了帮助。但是我无法读取收到的消息,因为它只返回REC **线,而不是返回此REC **线之后的实际消息。你能帮我解决任何其他解决方案或如何阅读GSM调制解调器中的传入消息
答案 0 :(得分:0)
我正在运行这个,我正在发送短信
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");
}
}
}