Error: "No error in nativeDrain" caused by OutputStream.flush()

Error: "No error in nativeDrain" caused by OutputStream.flush()

我有以下问题:当我想刷新 OutputStream 时,RxTx return 出错。 目标是使用调制解调器密钥发送短信。那我哪里错了?

我搜索了一下,没有找到,我不知道是什么问题!

我认为错误消息来自 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();
    }
}

Output:

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

然后找一张大A3sheet的纸,一支红笔写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 在发送 SMS 负载之前。