简体   繁体   English

Java中的COM端口标识

[英]com port identification in java

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。 I also added comm.jar in the library and i am using windows xp (service pack 3). 我还在库中添加了comm.jar,并且我正在使用Windows XP(Service Pack 3)。 Kindly look into the code and help me. 请查看代码并为我提供帮助。

import java.util.*;
import javax.comm.*;

public class Main
{
    public static void main(String[] args)
    {
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        System.out.println(ports.hasMoreElements());
        while(ports.hasMoreElements())
        {
            CommPortIdentifier cpi =
                           (CommPortIdentifier)ports.nextElement();
            System.out.println("Port " + cpi.getName());
        }
    }
}

Thanks in advance.. 提前致谢..

you just import "import gnu.io.*;" 您只需导入“ import gnu.io. *;” and RxTxcomm.jar add into libraries. 和RxTxcomm.jar添加到库中。

1) Download Rxtx binaries from http://rxtx.qbang.org/wiki/index.php/Download 1)从http://rxtx.qbang.org/wiki/index.php/Download下载Rxtx二进制文件
2) Extract the zip file. 2)解压缩zip文件。
3) Copy RXTXcomm.jar ---> <JAVA_HOME>/jre/lib/ext 3)复制RXTXcomm.jar ---> <JAVA_HOME>/jre/lib/ext
4) Copy librxtxSerial.so ---> <JAVA_HOME>/jre/lib/i386/ 4)复制librxtxSerial.so ---> <JAVA_HOME>/jre/lib/i386/
5) Copy librxtxParallel.so ---> <JAVA_HOME>/jre/lib/i386/ 5)复制librxtxParallel.so ---> <JAVA_HOME>/jre/lib/i386/

There are some tricks involved in getting the Java Communications API to correctly interact with the Windows system. 获取Java Communications API与Windows系统正确交互涉及一些技巧。 Among the items that you download from Sun are three very important files: 从Sun下载的项目中有三个非常重要的文件:

comm.jar  
win32com.dll  
javax.comm.properties  

For the JVM to recognize the serial ports, proper placement of these files is important. 为了使JVM识别串行端口,正确放置这些文件很重要。

Use the following installation methods to be effective: 使用以下安装方法可以有效:
comm.jar should be placed in: comm.jar应该放在:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib/ext    

win32com.dll should be placed in: win32com.dll应该放在:

%JAVA_HOME%/bin   
%JAVA_HOME%/jre/bin  
%windir%System32  

javax.comm.properties should be placed in: javax.comm.properties应该放在:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib

Thats THE prototypical problem with the commapi :-) 多数民众赞成在原型的问题:-)

Read through the installation guide, beside javacomm.api or wossname, two or three other files have to be copied to a very specific location. 通读安装指南,除了javacomm.api或wossname之外,还必须将其他两个或三个文件复制到一个非常特定的位置。

All of them. 他们全部。

Otherwise you simply don't see any com ports, error handling's a bit on the weak side, sorry, I went through the same thing about thrice myself ;-) 否则,您根本看不到任何COM端口,错误处理有点薄弱,对不起,我自己经历了三次相同的事情;-)

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib/ext    

win32com.dll should be placed in: win32com.dll应该放在:

%JAVA_HOME%/bin   
%JAVA_HOME%/jre/bin  
%windir%System32  

javax.comm.properties should be placed in: javax.comm.properties应该放在:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib  //this work out for me by sann seun

comm.jar should be placed in: comm.jar应该放在:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib/ext 

win32com.dll should be placed in: win32com.dll应该放在:

%JAVA_HOME%/bin   
%JAVA_HOME%/jre/bin  
%windir%System32

javax.comm.properties should be placed in: javax.comm.properties应该放在:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib  //this work out for me by sanni seun

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

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