请问在Linux下怎么用Java进行串口编程

vKazz 2015-09-02 09:58:35
只要显示监听就行了
不需要向串口写数据

本人对Linux不太熟悉,求大家多多指点
我用的是树莓派
系统信息如下
Linux raspberrypi 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l GNU/Linux

串口设备我是参考如下连接去设置的
http://blog.csdn.net/yangqicong11/article/details/26571787
安装玩minicom之后 测试正常,只是捕获到的串口数据是乱码

之后我用apt-get install命令安装了openJDK和librxtx
参考网上的代买写了如下的程序进行串口测试,使用的包是RXTXcomm.jar


package javaapplication1;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class TwoWaySerialComm {

public TwoWaySerialComm() {
super();
}

void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

InputStream in = serialPort.getInputStream();

InputStreamReader reader = new InputStreamReader(in);
BufferedReader r = new BufferedReader(reader);

(new Thread(new SerialReader(r))).start();

} else {
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}

/**
*
*/
//读的线程
public static class SerialReader implements Runnable {

BufferedReader in;

public SerialReader(BufferedReader in) {
this.in = in;
}

public void run() {
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
*
*/
//写的线程
// public static class SerialWriter implements Runnable {
//
// OutputStream out;
//
// public SerialWriter(OutputStream out) {
// this.out = out;
// }
//
// public void run() {
// try {
// int c = 0;
// while ((c = System.in.read()) > -1) {
// this.out.write(c);
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
public static void listPorts() {
@SuppressWarnings("unchecked")
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
int i = 1;
while (portEnum.hasMoreElements()) {
CommPortIdentifier portIdentifier = portEnum.nextElement();
System.out.println("第" + i + "个是 " + portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()));
}
}

static String getPortTypeName(int portType) {
switch (portType) {
case CommPortIdentifier.PORT_I2C:
return "I2C";
case CommPortIdentifier.PORT_PARALLEL:
return "Parallel";
case CommPortIdentifier.PORT_RAW:
return "Raw";
case CommPortIdentifier.PORT_RS485:
return "RS485";
case CommPortIdentifier.PORT_SERIAL:
return "Serial";
default:
return "unknown type";
}
}
}


主函数里面是

listPorts(); //显示当前可用的串口号
try {
(new TwoWaySerialComm()).connect("/dev/ttyAMA0");
} catch (Exception e) {
e.printStackTrace();
}



做树莓派运行的适时候显示
Stable Library
=========================================
Native lib Version = RXTX-2.2pre2
Java lib Version = RXTX-2.1-7
WARNING: RXTX Version mismatch
Jar version = RXTX-2.1-7
native lib Version = RXTX-2.2pre2
gnu.io.NoSuchPortException
at gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:218)
at javaapplication1.TwoWaySerialComm.connect(TwoWaySerialComm.java:25)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:34)

请问大家 这个要怎么解决



...全文
938 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
猿小马 2017-10-20
  • 打赏
  • 举报
回复
没有串口连接 http://blog.csdn.net/cheng157507947/article/details/43764623 参考这个把
bayougeng 2016-05-10
  • 打赏
  • 举报
回复
我最近正好在做java rxtx和串口通讯的项目。 RXTX可以从这里下载: http://fizzed.com/oss/rxtx-for-java 这里不是官网,是在rxtx最新版的基础上做了一些改动,貌似源代码也有的。 至于波特率,是指发送数据的频率,如果11520可以承受的话,你的9600也肯定没问题。 rxtx是通过native的代码来执行串口操作的,看你的输出,貌似jar version和native lib version不匹配。建议你先把这问题搞定。 另外就是,你要先写个小程序列出可用的列表。如果列表都列不出来,那就是设备没被识别
dsshf0801 2016-05-06
  • 打赏
  • 举报
回复
你是怎么在树莓派上安装rxtx的?请赐教
leo19870706 2015-10-22
  • 打赏
  • 举报
回复
遇到同样的问题,也是树莓派,各项都检查正常,就是串口列表获取不到。哎呀,好头疼的感觉。我采用的USB转串口,字符串写的是/dev/ttyUSB0 。持续关注该问题....
vKazz 2015-09-08
  • 打赏
  • 举报
回复
引用 4 楼 lanhxg的回复:
显示你的端口不对,“/dev/ttyAMA0”,你自己到对应目录下检查下。
检查过了,应该是没问题的,这个名字是我自己设置的,listport方法列出的端口是没有的,怎么破
lanhxg 2015-09-05
  • 打赏
  • 举报
回复
显示你的端口不对,“/dev/ttyAMA0”,你自己到对应目录下检查下。
vKazz 2015-09-04
  • 打赏
  • 举报
回复
万能的csdn,帮帮忙吧!!!!!
vKazz 2015-09-03
  • 打赏
  • 举报
回复
自己再顶顶,6666666
vKazz 2015-09-02
  • 打赏
  • 举报
回复
自己顶一顶 那个波特率是115200,程序里面写的是9600 我改过来的 还是不行

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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