Serial communication sample using jSerialComm

Introduction

The other day, I introduced "jSerialComm" Here is a sample of "jSerialComm /)". Actually, "jSerialComm" is used. "Netty-Transport-jSerialComm "Use directly.

sample

maven Add netty-transport-jserialcomm to pom.xml.

pom.xml


...
	<dependencies>
		<!-- https://mvnrepository.com/artifact/se.koc/netty-transport-jserialcomm -->
		<dependency>
			<groupId>se.koc</groupId>
			<artifactId>netty-transport-jserialcomm</artifactId>
			<version>1.0.0</version>
		</dependency>
	</dependencies>
...

Java source

This time, I am creating a sample that simply connects to COM5 and continues to receive text. For COM5, I used my MONO Wireless TWE-Lite-USB.

JSerialCommClient.java


package net.kyosho.serial.jsc;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.jsc.JSerialCommDeviceAddress;
import io.netty.channel.jsc.JSerialCommChannel;
import io.netty.channel.jsc.JSerialCommChannelConfig.Paritybit;
import io.netty.channel.jsc.JSerialCommChannelConfig.Stopbits;
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;

/**
 * Sends one message to a serial device
 */
public final class JSerialCommClient {

	static final String PORT = System.getProperty("port", "COM5");

	public static void main(String[] args) throws Exception {
		EventLoopGroup group = new OioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group).channel(JSerialCommChannel.class).handler(new ChannelInitializer<JSerialCommChannel>() {
				@Override
				public void initChannel(JSerialCommChannel 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 JSerialCommClientHandler());
				}
			});

			ChannelFuture f = b.connect(new JSerialCommDeviceAddress(PORT)).sync();

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

JSerialCommClientHandler.java


package net.kyosho.serial.jsc;

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

public class JSerialCommClientHandler 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.

output


Serial port responded with not-OK: ::ts=3760
Serial port responded with not-OK: ::ts=3761
Serial port responded with not-OK: ::ts=3762
Serial port responded with not-OK: ::ts=3763
Serial port responded with not-OK: ::ts=3764

Evaluation

It's easier to use than purejavacomm because you only need to add "Netty-Transport-jSerialComm" to pom.xml. (Purejavacomm will be as easy to use as it maintains netty-transport) There is also a link to the original site of netty, and if you get lost, you should use jSerialComm and netty-transport-jserialcomm.

Recommended Posts

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