I want to write a java program to communicate with my modem using
apache-mina-serial package.
these are my runngin environment properties:
mina version: 2.0.0 M3
Os: win xp service pack 2
JVM: Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
the sample source code is attached to the email.
i can send at command to the modem, and i can received the answer, for
example i write "AT" and the sample code sends the command to my
modem, then my modem answer the command with "OK" response. but, there
is a problem with mina serial package, when i want to dial a phone
number using at command : 'ATDT<a_phone_number>'.
the problem is that the modem responds 'ok' immediately, without
completely running the command. i think there may be other commands
which returns before they runs completely such as the dial command (i
do this later, using java comm api, and it works well).
probable cause:
i test the command using hyperterminal( in window xp) and i see that
hyperterminal first trys to detect carrier singnal and if it was
detected, trys to dial the phone number, and after successful dialing,
it responds 'ok'. if I press a key (such as Enter) just when the
'atdt' command is running, hyperterminal stops dialing (stops running
atdt command), and return 'ok'.
so i think <mina serial> sends a '\r\n' or '\n' character to my modem,
before atdt is completely run and this is why i receive 'ok' response,
immediately after sending atdt command without any dial trys.
please help me, what is the problem if i am wrong?
import org.apache.mina.transport.serial.*;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
public class SerialTest extends IoHandlerAdapter {
public static void main(String [] args){
SerialConnector connector = new SerialConnector();
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(
new TextLineCodecFactory()));
connector.setHandler(new SerialTest());
String comPort = "COM3";
int bauds = 115200;
ConnectFuture cf = connector.connect(
new SerialAddress(comPort, bauds, SerialAddress.DataBits.DATABITS_8,
SerialAddress.StopBits.BITS_1, SerialAddress.Parity.NONE,
SerialAddress.FlowControl.NONE));
cf.join();
String rawcmd = "";
while (true){
rawcmd = System.console().readLine().trim();
if (rawcmd.equals("exit"))
break;
else if (rawcmd.equals(""))
continue;
/* IoBuffer.setUseDirectBuffer(false);
IoBuffer buf = IoBuffer.allocate(1024); */
cf.getSession().write(rawcmd + "\r");//cf.getSession().write(rawcmd + "\r\n"); also tested
}
//cf.getSession().getCloseFuture().awaitUninterruptibly();
cf.getSession().close();
connector.dispose();
}
public void sessionClosed(IoSession session) {
System.err.println("Total " + session.getReadBytes() + " byte(s)");
}
public void messageSent(IoSession session, Object message) {
System.out.println("message has been sent: " + message);
}
public void messageReceived(IoSession session, Object message) {
System.out.println("message received:[" + message + "]");
}
}