Dear Sir,
first of all thank you for any help.
i would like to use mina in my project to read from serial port
my problem is which filter i use for binary
any suggestion will be helpful
hereis my code
package com.sample.serial;
import java.io.IOException;
import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.serial.SerialAddress;
import org.apache.mina.transport.serial.SerialConnector;
/**
* @author giftsam
*/
public class MinaClient
{
private static final int PORT = 1234;
public static void main(String[] args) throws IOException,
InterruptedException
{
IoConnector connector = new SerialConnector();
connector.getSessionConfig().setReadBufferSize(2048);
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(
new TextLineCodecFactory()));
connector.setHandler(new MinaClientHandler("Hello Server.."));
String comPort = "/dev/tty.usbmodemfa131";
int bauds = 115200;
ConnectFuture future = null;
IoSession session;
for (;;) {
try {
future = connector.connect(
new SerialAddress(comPort, bauds,
SerialAddress.DataBits.DATABITS_8,
SerialAddress.StopBits.BITS_1,
SerialAddress.Parity.NONE,
SerialAddress.FlowControl.NONE));
future.await();
session = future.getSession();
break;
} catch (RuntimeIoException e) {
System.err.println("Failed to connect.");
e.printStackTrace();
Thread.sleep(5000);
}catch (Exception e) {
System.err.println("Failed to connect.");
e.printStackTrace();
Thread.sleep(5000);
}
}
session.getConfig().setUseReadOperation(true);
session.getCloseFuture().awaitUninterruptibly();
System.out.println("After Writing");
connector.dispose();
}
}
package com.sample.serial;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author giftsam
*/
public class MinaClientHandler extends IoHandlerAdapter
{
private final Logger logger = (Logger)
LoggerFactory.getLogger(getClass());
private final String values;
private boolean finished;
public MinaClientHandler(String values)
{
this.values = values;
}
public boolean isFinished()
{
return finished;
}
@Override
public void messageSent(IoSession session, Object message) {
//System.out.println(new
ByteArrayToString().convertByteArrayToString(byteArray ));
System.out.println("message has been sent: " + message);
}
@Override
public void sessionOpened(IoSession session)
{
//init
byte byteArray[] = { 0x12, (byte) 0xC0, 0x00, (byte) 0xD2 };
session.write(byteArray);
//reset
byte byteArray1[] = { 0x12, 0x00, 0x05, 0x20, 0x11, 0x00, 0x00,
0x00, 0x00 };
session.write(byteArray1);
//request icc
byte byteArray2[] = { 0x12, 0x00, 0x06, 0x20, 0x12, 0x01, 0x00,
0x01, 0x01, 0x00 };
session.write(byteArray2);
byte[] newEgk = { 0x02, 0x40, (byte) 0x0B, 0x00, (byte) 0xA4, 0x04,
0x0C, 0x06, (byte) 0xD2, 0x76, 0x00, 0x00, 0x01, 0x02, 0x00 };
session.write(newEgk);
byte[] byteArray3 = { 0x02, 0x00, 0x05, 0x00, (byte) 0xB0, 0x00,
0x00, 0x00, 0x00 };
session.write(byteArray3);
}
@Override
public void messageReceived(IoSession session, Object message)
{
byte[] mBytesIn = (byte[]) message;
logger.info("Message received in the client..");
logger.info("Message is: " + message);
}
@Override
public void sessionClosed(IoSession session) {
System.err.println("Total " + session.getReadBytes() + " byte(s)");
}
@Override
public void exceptionCaught(IoSession session, Throwable cause)
{
session.close();
cause.printStackTrace();
}
// // liest Daten der KVK
// public byte[] readBinaryKVKCommand() {
// try {
//
// byte[] recivedBytes = sendCommand(byteArray, 5000,
true);
// return recivedBytes;
// } catch (Exception ex) {
// return null;
// }
// }
// response = cw.initCTCommand();
// LOG.debug("Reset CT");
// response = cw.resetCTCommand();
//
// // do {
// LOG.debug("Request ICC");
// response = cw.requestICCCommand();
}