Delay in continuosly writing to serial port
I am writing "AT" commands to a GSM modem via an RS 232 serial port for sending messages. I am doing so in a "for" loop and i need to know how to reduce the delay that i need to provide in the for loop for successfully sending messages.
The delay varies a lot, e.g for sending a message to 10 ppl a delay of 3800ms is enough, but for 200 recipients the delay needs to be increased to something like 9000ms.
Environment - Netbeans, javax.comm api, wavecom fastrack modem with baud rate - 115200[supported], RS232 seri开发者_Python百科al port connection.
I'm doing something like --
OutputStream os = serialPortInstance.getOutputStream();
String arrOfNumbers = {"872346334","23423433"};//I have the list of numbers here
String command = "";
for(int i = 0 ;i < arrOfNumbers.length ; i++){
command = "AT+CMGS=\""+arrOfNumbers[i]+"\"\nHello" + ((char)26);
os.write(command.getBytes());
Thread.sleep(5000);
}
I want to minimize this delay.
Kindly help, Thank you.The delay is superflous - the OutputStream will block until all the data has been written. Your real problem is probably that the device is busy after you send it a command and will abort the previous command if you send it another too soon.
That is avoided by checking the replies from the device. You need to also open an InputStream and wait for the reply from the device after each command. Consult the device manual on what possible replies it gives.
This would make your for loop more efficient:
final OutputStream os = serialPortInstance.getOutputStream();
final String arrOfNumbers = {"872346334","23423433"};//I have the list of numbers here
final Byte[] commands = new Byte[arrOfNumbers.length];
for(int i = 0 ;i < arrOfNumbers.length ; i++){
final String commandString = "AT+CMGS=\""+arrOfNumbers[i]+"\"\nHello" + ((char)26);
commands[i++] = commandString.getBytes();
}
for (int i = 0; i < arrOfNumber.length; i++) {
os.write(commands[i]);
os.flush();
Thread.sleep(5000);
}
精彩评论