No serial ports found

http://www.systronix.com/book/tools/javaxcomm.html

Javaxcomm installation and configuration problems are common. Everytime I configure a new PC it seems there is some new problem waiting to bite -- for instance, these:

First, check your system environment

If the configurations you tried to set up didn't really take place, then of course nothing will work as intended. Typos in system environment variables are a simple example of what can go wrong. So before you get too worried, check your settings. At a command prompt, type "set" to view all your environment settings and verify that JAVA_HOME, etc are actually set to what you intended, and that your JSDK bin folder appears early in the system PATH. You should see something like this (I've deleted a lot of irrelevant lines):

D:\>set
CLASSPATH=D:\j2sdk14\lib;D:\j2sdk14\lib\comm.jar;
JAVA_HOME=D:\j2sdk14
JAVA_LIB=D:\java_lib
Path=D:\j2sdk14\bin;C:\PVSW\BIN;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;


"No serial ports found" and errors in the javaxcomm docs
or
"where do those silly files *really* need to go"?

Recently, running this command line on a new notebook PC installation:
C:\java_lib\commapi\samples\BlackBox>java -verbose BlackBox
resulted in this eventual output:
[Loaded javax.comm.CommPortEnumerator from file:/C:/Program%20Files/Java/jdk1.5.0_03/lib/comm.jar]
No serial ports found!

What's going on here? Java is finding comm.jar but enumeration of serial ports is failing... After some trial and error, and with thanks to Peter Stewart for this link which confirms others have had the same problem, here is the answer.

It seems Windoze JDK is hardcoded to look for properties in <JAVA_HOME>\jre\lib (without regard to where the comm.jar file is located! - you'd think Java would look for the .properties file in the same folder containing comm.jar). The javax.comm.properties file is nothing mysterious - it just lists "Driver=com.sun.comm.Win32Driver". So as long as that Win32Driver class can be found some/any/where in your classpath, javaxcomm is happy. win32com.dll can go in <JAVA_HOME>\jre\bin too, in spite of what the javaxcomm docs tell you.

2006 July 18: So, bottom line:
  1. Install comm.jar and javax.comm.properties in <JAVA_HOME>\jre\lib
  2. Install win32com.dll in <JAVA_HOME>\jre\bin or in <JAVA_HOME>\bin and be sure whichever folder you use is in your PATH environment variable. The second choice is maybe best because you need the <JAVA_HOME>\bin in your path anyway to enable java.exe, javac.exe, javadoc.exe, etc, and you don't need the \jre\bin folder in your PATH.
  3. Add the comm.jar location to your classpath - preferably as a system environment variable. Also be sure the classpath contains a "." so that the current directory (such as when you run the BlackBox test below) is included.
  4. Ignore the javaxcomm javadocs in PlatformSpecific.html, regarding locations of the files mentioned above.
2006 May 04: On a new notebook PC, after doing all the above, BlackBox still fails with a No serial ports found! error. Urk. What now? I decided to try to get a clean and simple installation.
  1. I installed the comm.jar and javax.comm.properties in the jre/lib folder and win32com.dll in the jre/bin folder. Then added the jre/lib.comm.jar to the classpath followed by the jdk/bin folder, and the jre/bin to the path. BlackBox fails with
    Exception in thread "main" java.lang.NoClassDefFoundError: javax/comm/PortInUseException
  2. oops, it was a typo in my CLASSPATH variable. Fixed that and now things work as expected.

Then to test it, open a command prompt in the BlackBox samples folder (the one containing BlackBox.class) and type "java BlackBox"


Using javaxcomm on a PC with USB but no serial ports

USB to serial adapters are cheap and plentiful. I've personally used a Belkin model sold as a PDA synchronization accessory, since they were available cheap at a local office supply store. Follow the USB serial adapter installation instructions provided by that hardware vendor, and BlackBox should report the new device as simply another serial port. This has worked for me on notebook PCs without any serial ports, and on notebook and desktop PCs to add another serial port.

You can verify the proper serial port operation and get or set the COM port number in Windows Device Manager on the advanced tab.

USB serial adapters will function as a "normal serial port" with at least one difference - there is more software latency since the USB driver software adds another intermediate layer to the code which access the UART in the adapter. So you may need to increase timeout values in javaxcomm code which may use a USB serial adapter.


Java finds javaxcomm files in an unexpected place
Exception in thread "main" java.lang.NullPointerException
at com.ajile.drivers.serialport.aJileCommDriver.registerWithJavaxComm(aJileCommDriver.java:42)
at javax.comm.CommPortIdentifier.getPortIdentifiers(Unknown Source)
at BlackBox.main(BlackBox.java:222)

This looks strange because you wonder "what does an aJile driver have to do with BlackBox?" How is the aJile library getting mixed up with BlackBox? The answer is in the classpath order. In this case on my system it was:

CLASSPATH=d:\java_lib\commapi\samples\BlackBox\BlackBox.jar;D:\aJile\Runtime_cldc\Rts;D:\j2sdk14\lib\comm.jar;D:\tini1.11\bin;

Here the problem is in the order of the classpath folders.
BlackBox.jar is listed first, but then the aJile Runtime_cldc\Rts folder is next. Since the aJile runtime includes J2ME/CLDC support for javaxcomm, the BlackBox example is trying to use that to run on the PC, and this won't work. Reordering the paths so that the order is BlackBox.jar, then comm.jar, then others fixes this problem:

CLASSPATH=d:\java_lib\commapi\samples\BlackBox\BlackBox.jar;D:\j2sdk14\lib\comm.jar;D:\aJile\Runtime_cldc\Rts;D:\tini1.11\bin;

This points up a weakness in trying to have all your classpaths set up in your system environment. One ordering may not be what you want all the time. Fortunately there is a solution: the java -cp (classpath) option. For example, instead of adding BlackBox.jar and comm.jar to the environment, add paths to them on the command line:

java -cp d:\java_lib\commapi\samples\BlackBox\BlackBox.jar;D:\j2sdk14\lib\comm.jar; BlackBox

or, even better, by using our environment variables:

java -cp %JAVA_LIB%\commapi\samples\BlackBox\BlackBox.jar;%JAVA_HOME%\lib\comm.jar; BlackBox


Default install puts the JDK in C:\Program Files

Aargh! On my new notebook I let the JDK install itself where it wanted to - under C:\Program Files - note the whitespace. So now (some) java command line tools fail because "Program Files" contains white space! How inane. For example:
C:\java_lib\commapi\samples>java -cp %JAVA_LIB%\commapi\samples\BlackBox\BlackBox.jar;%JAVA_HOME%\lib\comm.jar; BlackBox

Exception in thread "main" java.lang.NoClassDefFoundError: Files\Java\jdk1/5/0_03\lib\comm/jar;
This appears to fail because JAVA_HOME expands to
JAVA_HOME=C:\Program Files\Java\jdk1.5.0_03
and "Program Files\Java\..." gets delimited to "Files\Java\..."

However, not all is broken:
C:\java_lib\commapi\samples\BlackBox>java -verbose BlackBox
successfully loads files in the classpath such as:
[Loaded javax.comm.CommPortEnumerator from file:/C:/Program%20Files/Java/jdk1.5.0_03/lib/comm.jar]

so frankly I don't understand what's going on here...


javax/comm/PortInUseException

While writing this tutorial, I installed javaxcomm and received this - new to me - error:

D:\>java BlackBox
Exception in thread "main" java.lang.NoClassDefFoundError: javax/comm/PortInUseException

I was not using any of the serial ports, so what could this mean? I started by using the -verbose switch. However this was not terribly heplful:

D:\>java -verbose BlackBox
[Opened D:\j2sdk14\jre\lib\rt.jar]
[Opened D:\j2sdk14\jre\lib\sunrsasign.jar]
[Opened D:\j2sdk14\jre\lib\jsse.jar]
[Opened D:\j2sdk14\jre\lib\jce.jar]

<snipped out a hundred lines or so>
[Loaded java.awt.event.WindowListener from D:\j2sdk14\jre\lib\rt.jar]
[Loaded BlackBox]
Exception in thread "main" java.lang.NoClassDefFoundError: javax/comm/PortInUseException
[Loaded java.lang.StackTraceElement from D:\j2sdk14\jre\lib\rt.jar]
[Loaded java.util.HashMap$KeySet from D:\j2sdk14\jre\lib\rt.jar]
[Loaded java.util.HashMap$KeyIterator from D:\j2sdk14\jre\lib\rt.jar]

This seems to indicate that the PortInUseException class can't be found. This class is located in the comm.jar file. One solution is to add the javaxcomm jar file explicitly to my environment classpath:

%JAVA_LIB%\commapi\comm.jar < note: this is incorrect; the reason why is explained below, so keep reading...

Then execution generated this error:

D:\>java BlackBox
No serial ports found!

From past experience I knew that this results when javaxcomm can't find the javax.comm.properties file, which causes the serial port enumeration to fail.

Hmm, I think I really wanted the comm.jar reference to be the JAVA_HOME\lib\comm.jar folder, not the JAVA_LIB location. OK, change that in my system variables. That does it:

D:\>set
CLASSPATH=D:\j2sdk14\lib;D:\j2sdk14\lib\comm.jar;
JAVA_HOME=D:\j2sdk14
JAVA_LIB=D:\java_lib
Path=D:\j2sdk14\bin;C:\PVSW\BIN;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;

D:\>set CLASSPATH=%JAVA_LIB%\commapi\samples\BlackBox\BlackBox.jar;%CLASSPATH%

D:\>java BlackBox
COM1: PORT_OWNED

... and now I can see the BlackBox GUI starting up as expcted.


Applications can't enumerate serial ports (COM1, COM2, ... don't appear to exist)

The javax.comm.properties must be in the correct directory. Failure to do this will result in javaxcomm application's inability to enumerate your PC serial ports. Instead of COM1, COM2 etc, you will see 'serial0' which are just the name placeholders, not real serial ports. (You can't actually access 'serial0' on the PC.)


BlackBox example can't find serial ports even though you'd followed the instructions

You may have another "java.exe" being executed in spite of the path settings, typically in a Windows system folder. I think in Windoze some system registry settings overrule those in the environment settings, though I've never seen documentation on this (if you have, please let me know). I've observed this issue on our PCs here, more than once.

Try executing with the -verbose switch and with an explicit path to your desired Java exe, from within the BlackBox folder in the commapi:

D:\j2sdk14\bin\java -verbose BlackBox

this should tell you exactly why it can't find the serial ports. Then you can usually proceed from there to solve the problem.


*******************************************************************************************************************************************************

以下是图片

*******************************************************************************************************************************************************



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值