gpt4 book ai didi

java - OS X 上 Java 中的串行通信

转载 作者:行者123 更新时间:2023-11-30 06:09:46 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,它可以通过 USB 到串行通信进行传输和接收。我通过研究我需要的两个文件了解到:一个包含实际库的 .jar 文件,以及据我了解的 librxtxSerial.jnilib,它是一个驱动程序?

从以下网站我了解到,这两个都需要出现在我的项目中才能开始串行通信过程。

http://rxtx.qbang.org/wiki/index.php/Download

我在 Mac 上使用 Eclipse IDE,似乎无法获得正确的文件配置来完成这项工作。网上到处都没有明确的答案,我希望有人能帮助大家解决这个问题。

最佳答案

我用过 NeuronRobotics nrjavaserial在 Linux 和 Mac OS/X 上取得了巨大成功。它将 native 库嵌入到 jar 文件中,然后呈现一个 Java 接口(interface)。它源自 RXTX,因此我希望它不会对您的项目造成巨大的改变。

主要编辑

为了节省您一些时间,以下是我目前正在做的事情。请注意,此时我没有使用 IDE - 这都是命令行。

import gnu.io.NRSerialPort;
import gnu.io.UnsupportedCommOperationException;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Arrays;

public class PulseOximeter {
public static void main( String[] argv ) throws IOException, UnsupportedCommOperationException {
NRSerialPort serial = new NRSerialPort("/dev/rfcomm0", 115200);
serial.connect();

DataInputStream ins = new DataInputStream(serial.getInputStream());

// read the first 10000 bytes a byte at a time
for( int i = 0; i < 10000; i++ ) {
int b = ins.read();
if( b == -1 ) {
System.out.println( "got EOF - going to keep trying" );
continue;
}
}

serial.disconnect();
}
}

以及用于构建它的 maven 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tld.domainname</groupId>
<artifactId>bluetooth</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.min.version>1.8</java.min.version>
<maven.min.version>3.2.0</maven.min.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<resource.directory>src/main/resources</resource.directory>
</properties>
<prerequisites>
<maven>${maven.min.version}</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>com.neuronrobotics</groupId>
<artifactId>nrjavaserial</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>${resource.directory}</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.min.version}</source>
<target>${java.min.version}</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<compilerArgument>-Xlint:all</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>

这是 Linux 版本的代码——最大的变化是串口名称(“/dev/rfcomm0”)部分。我这里没有我的 Mac,所以很抱歉我忘记了 Mac 设备的名称,但它在/dev 中 - 如果我没记错的话,它带有蓝牙。

此示例所做的只是从串行 (USB) 端口读取字节。请注意,Java 使这更有趣一点,因为它们以 整数 的形式返回,即使它们实际上是字节,所以您可以从需要做的任何位操作中获得一些乐趣。

关于java - OS X 上 Java 中的串行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37312224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com