我尝试使用Jamod串行主代码与COM1端口连接的设备建立连接。以下是我的代码:
SerialConnection connection = null;
public void connection() {
ModbusCoupler.getReference().setUnitID(1);
SerialParameters params = new SerialParameters();
params.setPortName("COM1");
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity("None");
params.setEncoding(Modbus.SERIAL_ENCODING_ASCII);
params.setEcho(false);
params.setStopbits(1);
try {
connection = new SerialConnection(params);
connection.open();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我正在使用Maven并在Ubuntu 14.04上运行此代码。我按照这个link在Ubuntu 14.04中安装了RXTX。我将librxtxSerial.so
复制到/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/
,将RXTXcomm.jar
复制到/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext
。
以下是我的maven代码:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<camel.version>2.12.1</camel.version>
<github.global.server>github</github.global.server>
<camel-version>2.16.2</camel-version>
<junit-version>4.11</junit-version>
<log4j-version>1.2.17</log4j-version>
<slf4j-version>1.7.12</slf4j-version>
<jackson-version>2.6.3</jackson-version>
<build.serial.gnu>true</build.serial.gnu>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jamod</artifactId>
<version>1.2_1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<version>${camel.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.wimpi</groupId>
<artifactId>jamod</artifactId>
<version>1.2</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.rxtx/rxtx -->
<dependency>
<groupId>org.rxtx</groupId>
<artifactId>rxtx</artifactId>
<version>2.1.7</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>apache.snapshots</id>
<name>Apache Snapshots Maven Repository</name>
<url>http://repository.apache.org/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>zodiac-repository</id>
<name>Zodiac Milestone Repository</name>
<url>http://zodiac.springsource.com/maven/bundles/milestone</url>
</repository>
<repository>
<id>com.springsource.repository.bundle.external</id>
<name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
<url>http://repository.springsource.com/maven/bundles/external</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.1</version>
<configuration>
<systemPropertyVariables>
<net.wimpi.modbus.debug>true</net.wimpi.modbus.debug>
<argLine>-Djava.library.path=/usr/share/java</argLine>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.1</version>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.4</version>
<configuration>
<dependencyDetailsEnabled>false</dependencyDetailsEnabled>
<dependencyLocationsEnabled>false</dependencyLocationsEnabled>
</configuration>
<!-- simpler configuration without reportSets available for usual cases -->
<reports>
<report>dependencies</report>
<report>scm</report>
<report>index</report>
</reports>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<configuration>
<links>
<link>http://jamod.sourceforge.net/apidocs/</link>
<link>http://camel.apache.org/maven/current/camel-core/apidocs/</link>
</links>
</configuration>
</plugin>
</reportPlugins>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</profiles>
当我尝试执行代码时,我收到以下错误:
Caused by: java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)[:1.8.0_91]
at java.lang.Runtime.loadLibrary0(Runtime.java:870)[:1.8.0_91]
at java.lang.System.loadLibrary(System.java:1122)[:1.8.0_91]
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
at net.wimpi.modbus.net.SerialConnection.open(SerialConnection.java:87)
at modbus.Serial.connection(SerialDataSource.java:43) //connection.open()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_91]
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)
我尝试了在stackoverflow上建议的其他解决方案,例如安装librxtx-java
但是得到相同的错误。请帮我解决这个问题。
答案 0 :(得分:1)
您需要安装本机部分; sudo apt-get install librxtx-java
并将/usr/lib/jni
添加到您的java.library.path
。此外,已安装的jar文件为/usr/share/java/RXTXcomm.jar
答案 1 :(得分:1)
请您使用以下JVM参数运行您的应用程序:
for( const auto& dvd1: dvds )
{
std::cout << dvd1 ;
}
JVM参数在-Djava.library.path=/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/librxtxSerial.so
命令之后和应用程序主类或带主类的jar之前传递。