我需要帮助才能使用命令AT + CMGL =“ALL”读取所有收件箱消息。使用我的java代码,我只能显示两条消息 起来。实际上,当我在HyperTerminal中尝试命令时,有10条消息。
这是我的代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package consoledmt;
/**
*
* @author user
*/
import javax.comm.*;
import java.util.*;
import java.io.*;
public class Main {
public Main() {
}
public static void main(String[] args) {
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null; // will be set if port found
String wantedPortName = "COM17";
while (portIdentifiers.hasMoreElements()) {
CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers
.nextElement();
if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL
&& pid.getName().equals(wantedPortName)) {
portId = pid;
break;
}
}
if (portId == null) {
System.err.println("Could not find serial port " + wantedPortName);
System.exit(0);
}
SerialPort port = null;
try {
port = (SerialPort) portId.open("Wavecom", 10000); // Wait max. 10
} catch (PortInUseException e) {
System.err.println("Port already in use: " + e);
System.exit(1);
}
try {
port.setSerialPortParams(115200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (Exception e) {
System.out.println(e.toString());
}
BufferedReader is = null;
PrintStream os = null;
try {
is = new BufferedReader(new InputStreamReader(port.getInputStream()));
} catch (IOException e) {
System.err.println("Can't open input stream");
is = null;
}
try {
os = new PrintStream(port.getOutputStream(), true);
} catch (IOException e) {
System.err.println("Can't open output stream");
is = null;
}
os.print("AT+CMGL=\"ALL\"");
os.print("\r\n");
try {
System.out.println(is.readLine());
System.out.println(is.readLine());
System.out.println(is.readLine());
}
catch (IOException e) {
System.err.println("Can't recieve input signals");
}
port.close();
}
}
以下信息是我从超级终端获得的信息。
+CMGL: 1,"REC READ","777",,"15/08/12,11:03:30+28"
Total saldo DompetKu anda adalah 100000
+CMGL: 2,"REC READ","INDOSAT",,"15/08/12,08:00:00+00"
Total saldo DompetKu anda adalah 100000
+CMGL: 3,"REC READ","777",,"15/08/12,11:08:49+28"
Total saldo DompetKu anda adalah 100000
+CMGL: 4,"REC READ","777",,"15/08/12,16:24:49+28"
Total saldo DompetKu anda adalah 100000
+CMGL: 5,"REC READ","777",,"15/08/12,16:31:36+28"
Total saldo DompetKu anda adalah 100000
+CMGL: 6,"REC READ","777",,"15/08/12,16:32:58+28"
Total saldo DompetKu anda adalah 100000
+CMGL: 7,"REC READ","777",,"15/08/12,16:34:15+28"
Total saldo DompetKu anda adalah 100000
+CMGL: 8,"REC READ","777",,"15/08/12,16:41:00+28"
Total saldo DompetKu anda adalah 100000
+CMGL: 9,"REC READ","777",,"15/08/12,16:42:54+28"
Total saldo DompetKu anda adalah 100000
+CMGL: 10,"REC READ","777",,"15/08/12,16:45:18+28"
Total saldo DompetKu anda adalah 100000
如何在Java中获得相同的结果?
答案 0 :(得分:1)
首先是一般性评论:您应该能够通过该简单请求访问您的端口:
String String wantedPortName = "COM17";= "COM17";
try {
CommPortIdentifier portId = CommPortIdentifier(wantedPortName);
}
catch(NoSuchPortException ex) {
System.err.println("Could not find serial port " + wantedPortName);
System.exit(0);
}
但你真正的问题是你只能从串口读取3行,而不是循环读取所有内容。读取串口时的难点在于,您很难知道数据是否即将到来。有两种常见的方法可以解决它:
您的驱动程序支持读取超时:
首先启用它:
port.enableReceiveTimeout(2000); // assume 2 s is long enough to wait
if (! port.isReceiveTimeout) {
System.out.println("Receive timeout is not supported");
port.close(); // maybe more housekeeping is needed...
System.exit(0);
}
接下来只循环直到空读(注意未经Java测试):
os.print("AT+CMGL=\"ALL\"");
os.print("\r\n");
try {
String line;
while(true) {
line = is.readLine());
if (is.isEmpty()) { break; }
System.out.println(is.readLine());
}
catch (IOException e) {
System.err.println("Can't recieve input signals");
}
使用一个专用线程来处理输入行并将其杀死,无需处理。