Serial communication sample using purejavacomm

Introduction

The other day, "PureJavaComm introduced in" Issues and alternative research of Java serial communication library RXTX Here is a sample of "purejavacomm / purejavacomm.php)". Actually, in addition to "PureJavaComm", "[netty-transport-purejavacomm](https://github.com/steveturner/netty-transport-" I also use purejavacomm) ".

sample

maven Add purejavacomm to pom.xml. Also, "netty-transport-purejavacomm" is copied and used.

pom.xml


...
	<dependencies>
		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty-all</artifactId>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.github.purejavacomm/purejavacomm -->
		<dependency>
			<groupId>com.github.purejavacomm</groupId>
			<artifactId>purejavacomm</artifactId>
			<version>1.0.2.RELEASE</version>
		</dependency>
	</dependencies>
...

Java source

This time, I'm creating a sample that simply connects to COM5 and keeps receiving text. For COM5, I used my MONO Wireless TWE-Lite-USB.

PureJavaCommClient.java


package net.kyosho.serial.pjc;

import java.util.Enumeration;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import purejavacomm.CommPortIdentifier;

public final class PureJavaCommClient {

	public static void main(String[] args) throws Exception {
		CommPortIdentifier portid = null;
		Enumeration<CommPortIdentifier> e = CommPortIdentifier.getPortIdentifiers();
		while (e.hasMoreElements()) {
			portid = (CommPortIdentifier) e.nextElement();
			System.out.println("found " + portid.getName());
		}
		EventLoopGroup group = new OioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group).channel(PureJavaCommChannel.class).handler(new ChannelInitializer<PureJavaCommChannel>() {
				@Override
				public void initChannel(PureJavaCommChannel ch) throws Exception {
					ch.config().setBaudrate(115200).setParitybit(Paritybit.NONE).setStopbits(Stopbits.STOPBITS_1);
					ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(), new StringDecoder(),
							new PureJavaCommClientHandler());
				}
			});

			ChannelFuture f = b.connect(new PureJavaCommDeviceAddress("COM5")).sync();

			f.channel().closeFuture().sync();
		} finally {
			group.shutdownGracefully();
		}
	}

	private PureJavaCommClient() {
	}
}

PureJavaCommClientHandler.java


package net.kyosho.serial.pjc;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class PureJavaCommClientHandler extends SimpleChannelInboundHandler<String> {

	@Override
	public void channelActive(ChannelHandlerContext ctx) {
		ctx.writeAndFlush("AT\n");
	}

	@Override
	public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		if ("OK".equals(msg)) {
			System.out.println("Serial port responded to AT");
		} else {
			System.out.println("Serial port responded with not-OK: " + msg);
		}
//		ctx.close();
	}
}

Execution result

When executed, the following output will be obtained. For the time being, it can be read that TWE-Lite-USB outputs a counter at 1-second intervals. Alright! I'm connected!

output


found COM5
found COM1
found COM3
Serial port responded with not-OK: ::ts=2743
Serial port responded with not-OK: ::ts=2744
Serial port responded with not-OK: ::ts=2745
Serial port responded with not-OK: ::ts=2746
Serial port responded with not-OK: ::ts=2747
Serial port responded with not-OK: ::ts=2748

Evaluation

I'm a little worried that I have to copy and use "netty-transport-purejavacomm", but be aware of the native library. It's easy because you don't have to. How about when you want to communicate easily?

Recommended Posts

Serial communication sample using purejavacomm
Serial communication sample using jSerialComm
GUI sample using JavaFX
Use serial communication on Android
Sample code using Minio from Java
Process Communication using AMQP with RabbitMQ