我在与 Apache Mina 的串行连接上工作了几天。但我没有运气。我建立了一个小测试程序并得到了这个错误:
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.mina.core.session.AbstractIoSession.<init>(Lorg/apache/mina/core/service/IoService;)V
at org.apache.mina.transport.serial.SerialSessionImpl.<init>(SerialSessionImpl.java:73)
at org.apache.mina.transport.serial.SerialConnector.connect0(SerialConnector.java:97)
at org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:262)
at org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:172)
at org.apache.mina.timeserver.SerialTest.main(SerialTest.java:22)
我不知道我能做些什么来解决这个问题......
这是我的小SerialTest.java
package org.apache.mina.timeserver;
import org.apache.mina.transport.serial.*;
import org.apache.mina.core.service.IoHandlerAdapter;
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 {
@SuppressWarnings("deprecation")
public static void main(String [] args){
SerialConnector connector = new SerialConnector();
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(
new TextLineCodecFactory()));
connector.setHandler(new SerialTest());
String comPort = "COM10";
int bauds = 9600;
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 + "]");
}
}