Beispiel für eine serielle Kommunikation mit purejavacomm

Einführung

Neulich wurde "PureJavaComm in" Probleme und alternative Umfragen für die Java Serial Communication Library RXTX "vorgestellt. Hier ist ein Beispiel von "purejavacomm / purejavacomm.php)". Zusätzlich zu "PureJavaComm", "netty-transport-purejavacomm Ich benutze auch purejavacomm) ".

Stichprobe

maven Fügen Sie purejavacomm zu pom.xml hinzu. Außerdem wird "netty-transport-purejavacomm" kopiert und verwendet.

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-Quelle

Dieses Mal erstelle ich ein Beispiel, das einfach eine Verbindung zu COM5 herstellt und weiterhin Text empfängt. Für COM5 habe ich meinen MONO Wireless TWE-Lite-USB verwendet.

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();
	}
}

Ausführungsergebnis

Bei der Ausführung wird die folgende Ausgabe erhalten. Derzeit ist zu lesen, dass TWE-Lite-USB im Abstand von 1 Sekunde einen Zähler ausgibt. In Ordung! Ich bin verbunden!

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

Auswertung

Ich mache mir ein wenig Sorgen, dass ich "netty-transport-purejavacomm" kopieren und verwenden muss, aber beachten Sie die native Bibliothek. Es ist einfach, weil Sie nicht müssen. Wie wäre es, wenn Sie einfach kommunizieren möchten?

Recommended Posts

Beispiel für eine serielle Kommunikation mit purejavacomm
Beispiel für eine serielle Kommunikation mit jSerialComm
GUI-Beispiel mit JavaFX
Verwenden Sie die serielle Kommunikation unter Android
Beispielcode mit Minio aus Java
Prozesskommunikation mit AMQP mit RabbitMQ