I have the following problem: when I want to flush the OutputStream, RxTx return an error. The goal is to send sms using modem key. So where was I wrong?
I searched and found nothing, I don't know what causes the problem !
I think the error message comes from the RxTx library but I'm not sure. I've already tried without flushing, but it didn't work either. So please help me <3 !
This is the actual code of my 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)
It makes more sense to call outputStream.flush
after outputStream.write
, so do that. And speaking of switching order, call outputStream.close
before serial.close
.
Then find a large A3 sheet of paper, a red pen and write 1000 times
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....
When sending AT commands to a modem, you MUST read and parse everything it sends back to you.
Your second AT+CSCA
is supposed to be AT+CMGS
, and for that command you
MUST wait for the "ready to receive payload data" prompt before sending the SMS payload.