gpt4 book ai didi

java - 错误 : "No error in nativeDrain" caused by OutputStream. 刷新()

转载 作者:行者123 更新时间:2023-11-30 01:50:08 27 4
gpt4 key购买 nike

我遇到以下问题:当我想刷新 OutputStream 时,RxTx 返回错误。目标是使用调制解调器 key 发送短信。那么我哪里错了?

我查了一下,没有发现什么问题,不知道是什么原因造成的!

我认为错误消息来自 RxTx 库,但我不确定。我已经尝试过不冲水,但也没有效果。所以请帮助我<3!

这是我的类(class)的实际代码:

package fr.ryfax.smscontroller.main;

import java.io.*;
import java.util.ArrayList;

import gnu.io.*;

public class USBModem {

private CommPortIdentifier PORT_ID;
private final String PORT;
private final int BITRATE;
private final String CENTER;

private final char ENTER = 13;
private final char CTRLZ = 26;

public USBModem(String PORT, int BITRATE, String SMSCENTER) {

try {
this.PORT_ID = CommPortIdentifier.getPortIdentifier(PORT);
}catch(Exception e) {
this.PORT_ID = null;
System.err.println("USBModem Error: Unknown port!");
}

this.PORT = PORT;
this.BITRATE = BITRATE;
this.CENTER = SMSCENTER;

}

public void sendSMS(String TO, String MSG) throws Exception {

SerialPort serial = (SerialPort) PORT_ID.open(this.PORT, 2000);
//InputStream inputStream = serial.getInputStream();

serial.setSerialPortParams(this.BITRATE, //115200
SerialPort.DATABITS_8, //Bit de données
SerialPort.STOPBITS_1, //Bit d'arrêt
SerialPort.PARITY_NONE //Parité
);
serial.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); //Flux

ArrayList<String> commands = new ArrayList<String>();

commands.add("AT" + ENTER);
commands.add("AT+CMGF=1" + ENTER);
commands.add("AT+CSCA=\"" + CENTER + "\"" + ENTER);
commands.add("AT+CSCA=\"" + TO + "\"" + ENTER);
commands.add(MSG + CTRLZ);

sendCommands(commands, serial);

System.out.println("[MOI] -> [" + TO + "]" + " : " + MSG);
}

public void sendCommands(ArrayList<String> commands, SerialPort serial) throws Exception {
OutputStream outputStream = serial.getOutputStream();
for(String command : commands) {
outputStream.flush();
outputStream.write(command.getBytes());
Thread.sleep(100);
}
Thread.sleep(3000);

serial.close();
outputStream.close();
}
}

输出:

Exception in thread "main" java.io.IOException: No error in nativeDrain
at gnu.io.RXTXPort.nativeDrain(Native Method)
at gnu.io.RXTXPort$SerialOutputStream.flush(RXTXPort.java:1248)
at fr.ryfax.smscontroller.main.USBModem.sendCommands(USBModem.java:61)
at fr.ryfax.smscontroller.main.USBModem.sendSMS(USBModem.java:53)
at fr.ryfax.smscontroller.main.Main.main(Main.java:21)

最佳答案

outputStream.write 之后调用 outputStream.flush 更有意义,所以就这样做。说到切换顺序,请在 serial.close 之前调用 outputStream.close

然后找一张大A3纸,一支红笔写1000遍

I will never use Thread.sleep as a substitute for reading and parsing responses from a modem.

I will never use Thread.sleep as a substitute for reading and parsing responses from a modem.

I will never use Thread.sleep as a substitute for reading and parsing responses from a modem.

I will never use Thread.sleep as a substitute for reading and parsing responses from a modem.

...

向调制解调器发送 AT 命令时,您必须 read and parse它发送回给您的所有内容。

您的第二个 AT+CSCA 应该是 AT+CMGS,对于该命令,您必须 wait for the "ready to receive payload data" prompt在发送短信有效负载之前。

关于java - 错误 : "No error in nativeDrain" caused by OutputStream. 刷新(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56367113/

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