gpt4 book ai didi

java - 无法读取调制解调器的所有消息

转载 作者:行者123 更新时间:2023-11-30 08:00:33 25 4
gpt4 key购买 nike

我需要帮助才能使用命令 AT+CMGL="ALL"读取所有收件箱消息。使用我的java代码,我只能显示两条消息向上。实际上,当我在 super 终端中尝试该命令时,有 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();
}
}

以下消息是我从 super 终端收到的消息。

+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 中获得相同的结果?

最佳答案

首先是一般性评论:您应该能够通过这个简单的请求访问您的端口:

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 行,而不是循环读取所有内容。读取串口时最困难的部分是你很难知道数据是否即将到达。常见的处理方法有两种:

  1. 您的驱动程序支持读取超时:

    首先启用它:

    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);
    }

    next 只是循环直到空读取(注意在 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");
    }
  2. 使用一个专用线程来处理输入行,并在没有更多需要处理的地方终止它。

关于java - 无法读取调制解调器的所有消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32003666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com