java串口抛出异常,跪求大神帮忙看一下

Redone_hy 2016-11-25 07:01:40
这些代码在别人电脑上可以,在我电脑上就抛出异常了,求好心人帮忙解决一下。

java.lang.UnsatisfiedLinkError: gnu.io.RXTXCommDriver.nativeGetVersion()Ljava/lang/String; thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: Native Library C:\Windows\System32\rxtxSerial.dll already loaded in another classloader
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1907)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:123)
at RStest2.RStest2.startComPort(RStest2.java:69)
at RStest2.RStest2.main(RStest2.java:163)






package RStest2;

import java.io.*;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

//import database.database;
import gnu.io.*;

public class RStest2 extends Thread implements SerialPortEventListener { // SerialPortEventListener
// 监听器,我的理解是独立开辟一个线程监听串口数据

// database DB=new database();
static CommPortIdentifier portId; // 串口通信管理类
static Enumeration<?> portList; // 有效连接上的端口的枚举
InputStream inputStream; // 从串口来的输入流
static OutputStream outputStream;// 向串口输出的流
static SerialPort serialPort; // 串口的引用
// 堵塞队列用来存放读到的数据
private BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>();

@Override
/**
* SerialPort EventListene 的方法,持续监听端口上是否有数据流
*/
public void serialEvent(SerialPortEvent event) {//

switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据
byte[] readBuffer = new byte[64];
try {
int numBytes = -1;
while (inputStream.available() > 0) {
numBytes = inputStream.read(readBuffer);

if (numBytes > 0) {
msgQueue.add(new String(readBuffer));
readBuffer = new byte[64];// 重新构造缓冲对象,否则有可能会影响接下来接收的数据
} else {
msgQueue.add("额------没有读到数据");
}
}
} catch (IOException e) {
}
break;
}
}

/**
*
* 通过程序打开COM4串口,设置监听器以及相关的参数
*
* @return 返回1 表示端口打开成功,返回 0表示端口打开失败
*/
public int startComPort() {
// 通过串口通信管理类获得当前连接上的串口列表
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {

// 获取相应串口对象
portId = (CommPortIdentifier) portList.nextElement();

System.out.println("设备类型:--->" + portId.getPortType());
System.out.println("设备名称:---->" + portId.getName());
// 判断端口类型是否为串口
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// 判断如果COM4串口存在,就打开该串口
if (portId.getName().equals("COM4")) {
try {
// 打开串口名字为COM_4(名字任意),延迟为2毫秒
serialPort = (SerialPort) portId.open("COM_4", 2000);

} catch (PortInUseException e) {
e.printStackTrace();
return 0;
}
// 设置当前串口的输入输出流
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
return 0;
}
// 给当前串口添加一个监听器
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
return 0;
}
// 设置监听器生效,即:当有数据时通知
serialPort.notifyOnDataAvailable(true);

// 设置串口的一些读写参数
try {
// 比特率、数据位、停止位、奇偶校验位
serialPort.setSerialPortParams(57600,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
return 0;
}

return 1;
}
}
}
return 0;
}

@Override
public void run() {
// TODO Auto-generated method stub
try {
System.out.println("--------------任务处理线程运行了--------------");
while (true) {
// 如果堵塞队列中存在数据就将其输出
if (msgQueue.size() > 0) {
String sss=msgQueue.take();
sss=sss.trim();
String[] split = sss.split("\\s+");

//int length=

if(split.length!=1)
{
// DB.cun(Integer.parseInt(split[2], 16));
System.out.println("和基站0距离:"+Integer.parseInt(split[2], 16));
System.out.println("和基站0距离:"+Integer.parseInt(split[2], 16));

// System.out.println("数据:"+sss);

}
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}

public static void main(String[] args) {
RStest2 cRead = new RStest2();
int i = cRead.startComPort();
if (i == 1) {
// 启动线程来处理收到的数据
cRead.start();
try {
String st = "哈哈----你好";
System.out.println("发出字节数:" + st.getBytes("gbk").length);
outputStream.write(st.getBytes("gbk"), 0,
st.getBytes("gbk").length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
return;
}

}
}
...全文
393 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧