Exemplo n.º 1
0
  public static void main(String[] args) {

    SerialAddress portAddress =
        new SerialAddress(
            "COM1", 9600, DataBits.DATABITS_8, StopBits.BITS_1, Parity.NONE, FlowControl.NONE);
    CHYCAHandler handler = new CHYCAHandler(new CHYCA());
    handler.loadData();
    RS232Connector receiver = RS232Connector.getInstance(handler, portAddress);
    receiver.addFilter("logger", new LoggingFilter());
    receiver.addFilter(
        "codec", new ProtocolCodecFilter(new CHYCAProtocolCodecFactory())); // 设置编码过滤器
    // receiver.addFilter("codec", new ProtocolCodecFilter(
    // new TextLineCodecFactory(Charset.forName("UTF-8")))); // 设置编码过滤器
    receiver.startListen();
    CHYCATester01 test = new CHYCATester01(receiver);
    new Thread(test).start();
  }
Exemplo n.º 2
0
  public void sendSample() throws InterruptedException {
    IoSession session = rs232connector.getReceiverSession();
    // SendOnePacket(session, pkt, nc, 1000);
    CHYCAOutMessage pkt = new CHYCAOutMessage();
    WriteFuture wf;
    byte bt[] = pkt.getContent();
    bt[0] = 0;

    pkt.setContent(bt);
    wf = session.write(pkt); // 发送消息
    wf.awaitUninterruptibly();
    Thread.sleep(1000);
  }