简体   繁体   English

OS X上Java的串行通信

[英]Serial communication in Java on OS X

I am trying to create a program that can both transmit and receive via a USB to serial communication. 我正在尝试创建一个程序,该程序可以通过USB到串行通信进行传输和接收。 I have learned by research of two files that I will need: a .jar file containing the actual library, and from what I understand the librxtxSerial.jnilib , which is a driver? 通过研究,我了解到我需要两个文件:一个包含实际库的.jar文件,从据我了解的librxtxSerial.jnilib ,它是一个驱动程序?

From the following website I have learned that both of these need to be present in my project in order to begin the serial communication process. 从下面的网站,我了解到这两个都需要在我的项目中同时存在,以便开始串行通信过程。

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

I am using the Eclipse IDE on a mac and cannot seem to get the correct configuration of files in order to make this work. 我在Mac上使用Eclipse IDE,似乎无法获得正确的文件配置以使其正常工作。 There are no clear answers anywhere on the net and I am hoping that somebody can help clear this up for everybody. 网络上的任何地方都没有明确的答案,我希望有人可以帮助大家解决这个问题。

I've used NeuronRobotics nrjavaserial with great success on Linux and Mac OS/X. 我在Linux和Mac OS / X上使用NeuronRobotics nrjavaserial取得了巨大的成功。 It embeds a native library into the jar file and then presents a Java interface. 它将本机库嵌入jar文件中,然后提供Java接口。 It is derived from RXTX and so I'm hoping it won't be a huge change for your project. 它源自RXTX,所以我希望它对您的项目不会有太大的变化。

MAJOR EDIT 主要编辑

In the interest of saving you some time, here's what I'm doing currently. 为了节省您的时间,这是我目前正在做的事情。 Note that I'm not using an IDE at this time - this is all command line. 请注意,我目前使用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();
    }
}

and the maven file to build it: 和构建它的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>

This is code for the Linux version - the biggest change will be the serial port name (the "/dev/rfcomm0") part. 这是Linux版本的代码-最大的变化是串行端口名称(“ / dev / rfcomm0”)部分。 I don't have my Mac here so I'm sorry I forget the name of the Mac device but it is in /dev - something with Bluetooth if I remember correctly. 我的Mac不在这里,对不起,我忘记了Mac设备的名称,但是它在/ dev中-如果我没记错的话,它带有蓝牙功能。

All this sample does is read bytes off of the serial (USB) port. 此示例所做的只是从串行(USB)端口读取字节。 Note that Java makes this a little more fun in that they come back as integers even if they are really bytes so you'll have some fun with any bit manipulations you need to do. 请注意,Java使它变得更有趣,因为即使它们实际上是字节,它们也以整数形式返回,因此您将对需要做的任何位操作有所乐趣。

I faced the same problem a few years ago when I was setting up a Java application to receive bio-data from an arduino. 几年前,当我设置一个Java应用程序以接收来自arduino的生物数据时,我遇到了同样的问题。

I wrestled and wrestled with the RXTX libraries and was never able to get them working properly (on both Linux and Windows). 我为RXTX库苦苦挣扎,却始终无法使它们正常工作(在Linux和Windows上)。 This seems to be a common problem ( Stable alternative to RXTX ). 这似乎是一个常见的问题( RXTX的稳定替代品 )。

My advice: move away from the RXTX libraries. 我的建议:远离RXTX库。

An alternative library I found that worked great and was easy to implement is the Java Simple Serial Connector (JSSC) library found at https://github.com/scream3r/java-simple-serial-connector . 我发现一个很好用且易于实现的替代库是在https://github.com/scream3r/java-simple-serial-connector上找到的Java Simple Serial Connector(JSSC)库。
And here are a few examples: http://www.javaprogrammingforums.com/java-se-api-tutorials/5603-jssc-library-easy-work-serial-ports.html 以下是一些示例: http : //www.javaprogrammingforums.com/java-se-api-tutorials/5603-jssc-library-easy-work-serial-ports.html


As far as setting up in eclipse, you should be able to include the jar in the following manner: 至于在eclipse中进行设置,您应该可以通过以下方式包含jar:

Right click your project -> Configure Build Path -> Add External JARs -> select the jar you downloaded. 右键单击您的项目->配置构建路径->添加外部JAR->选择您下载的jar。
Now in package explorer you should see a "Referenced Libraries" item that, when expanded, shows the jar you added. 现在,在包浏览器中,您应该看到一个“参考库”项目,展开后会显示您添加的jar。


Lastly, it's worth your time to look into tools like Maven or Gradle. 最后,值得您花时间研究诸如Maven或Gradle之类的工具。 They greatly simplify adding external libraries. 它们极大地简化了添加外部库的过程。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM