简体   繁体   English

在ubuntu上使用Java进行串行端口识别

[英]serial port identification with java on ubuntu

I'm trying to connect a serial application on ubuntu with Java 我正在尝试使用Java连接ubuntu上的串行应用程序
After searching and reading resources,I add comm.jar and RXTXcomm.jar in the library. 搜索和阅读资源后,我在库中添加了comm.jar和RXTXcomm.jar。
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. 在我的系统中,有三个端口,但是在ports.hasMoreElements()方法中显示为false。
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);
}    

In my case, I'm using Ubuntu, and my notebook does not have any serial or paralel ports. 就我而言,我使用的是Ubuntu,而我的笔记本没有任何串行或并行端口。

So, you have to simulate this kind of port: 因此,您必须模拟这种端口:

apt-get install socat

Run it: 运行:

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]

Stop socat [CTRL]+[C] and symlink it to a location that RXTX will recognise as a device, due to "tty" prefix: 停止socat [CTRL] + [C]并将其符号链接到RXTX将识别为设备的位置,这是由于前缀“ tty”:

sudo ln -s /dev/pts/2 /dev/ttyUSB02
sudo ln -s /dev/pts/3 /dev/ttyUSB03

Now, run socat again 现在,再次运行socat

socat -d -d pty,raw,echo=0 pty,raw,echo=0

Now, using following code, you will see 2 virtual ports: 现在,使用以下代码,您将看到2个虚拟端口:

        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

It looks like you are filtering out the port you want. 看起来您正在过滤所需的端口。 A /dev/tty is a special character device... not a serial port so / dev / tty是特殊字符设备,而不是串行端口,因此

if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
  pid.getName().equals(wantedPortName)) 

should never match your string. 永远不要匹配您的字符串。

To prove this, try iterating over your available ports. 为了证明这一点,请尝试迭代可用的端口。 I don't know if a tty can can be detected by RXTX but play with it and let us know. 我不知道RXTX是否可以检测到tty,但可以使用它并告诉我们。

Ref: http://rxtx.qbang.org/wiki/index.php/Discovering_available_comm_ports 参考: http : //rxtx.qbang.org/wiki/index.php/Discovering_available_comm_ports

EDIT: So you don't have any serial devices to test with? 编辑:因此,您没有要测试的任何串行设备? All I sat is make sure you have everything installed properly, including the properties file as described in this file. 我所要做的就是确保您已正确安装所有内容,包括此文件中所述的属性文件。

http://rxtx.qbang.org/pub/rxtx/rxtx-2.0-7pre2/INSTALL http://rxtx.qbang.org/pub/rxtx/rxtx-2.0-7pre2/安装

Once done, install a null modem emulator or find a serial device to test with. 完成后,安装一个空的调制解调器仿真器或查找要测试的串行设备。

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

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