serial port identification with java on ubuntu(在ubuntu上用java进行串口识别)
问题描述
我正在尝试使用 Java 连接 ubuntu 上的串行应用程序
在搜索和阅读资源后,我在库中添加了 comm.jar 和 RXTXcomm.jar.
我使用以下代码来识别comports.在我的系统中有三个端口,但在 ports.hasMoreElements()
方法中显示为 false.
请查看代码并帮助我.
I'm trying to connect a serial application on ubuntu with Java
After searching and reading resources,I add comm.jar and RXTXcomm.jar in the library.
I use the following code to identify the comports. In my system there are three ports but it is showing false in ports.hasMoreElements()
method.
Kindly look into the code and help me.
String wantedPortName = "/dev/ttya";
///dev/ttyS0 و /dev/ttyS1 نیز تست شد
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null; // will be set if port found
while (portIdentifiers.hasMoreElements())
{
CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
pid.getName().equals(wantedPortName))
{
portId = pid;
break;
}
}
if(portId == null)
{
System.err.println("Could not find serial port " + wantedPortName);
System.exit(1);
}
推荐答案
就我而言,我使用的是 Ubuntu,我的笔记本没有任何串行或并行端口.
In my case, I'm using Ubuntu, and my notebook does not have any serial or paralel ports.
所以,你必须模拟这种端口:
So, you have to simulate this kind of port:
apt-get install socat
运行它:
socat -d -d pty,raw,echo=0, pty,raw,echo=0
根据输出,注意创建的设备":
According to output, pay attention to "devices" created:
2014/02/05 01:04:32 socat[7411] N PTY is /dev/pts/2
2014/02/05 01:04:32 socat[7411] N PTY is /dev/pts/3
2014/02/05 01:04:32 socat[7411] N starting data transfer loop with FDs [3,3] and [5,5]
停止 socat [CTRL]+[C] 并将其符号链接到 RXTX 将识别为设备的位置,由于tty"前缀:
Stop socat [CTRL]+[C] and symlink it to a location that RXTX will recognise as a device, due to "tty" prefix:
sudo ln -s /dev/pts/2 /dev/ttyUSB02
sudo ln -s /dev/pts/3 /dev/ttyUSB03
现在,再次运行 socat
Now, run socat again
socat -d -d pty,raw,echo=0 pty,raw,echo=0
现在,使用以下代码,您将看到 2 个虚拟端口:
Now, using following code, you will see 2 virtual ports:
Enumeration portList = CommPortIdentifier.getPortIdentifiers();//this line was false
System.out.println(portList.hasMoreElements());
while(portList.hasMoreElements()){
System.out.println("Has more elements");
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
else{
System.out.println(portId.getName());
}
}
system.out:
system.out:
true
Has more elements
/dev/ttyUSB03
Has more elements
/dev/ttyUSB02
这篇关于在ubuntu上用java进行串口识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在ubuntu上用java进行串口识别
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01