我的Java程序无法检测连接到计算机的串行端口设备.
我正在使用Windows 7(64位)计算机().
串口设备是GPS模块,输出37860波特的文本GPS串,连接到插入笔记本电脑USB端口的串行USB转换器.
Windows控制面板/设备管理器将此设备显示为COM7.
在计算机上运行的GPS应用程序"MiniGPS"能够与GPS芯片作为COM7在两个方向上进行通信.
Processing中的应用程序能够检测到计算机有2个串行端口(com3和com7,com3是鼠标).
我想用Java(或C++)编写一个应用程序来从GPS串行端口收集数据.
我从某个地方复制了下面的代码(下面),不知道现在在哪里,相同或类似的代码已经出现在一堆其他问题中了.
当我运行它时,portList枚举返回空,它根本找不到任何串口.
我知道一次只有一个程序可以看到串口,我在尝试之前关闭了设备管理器应用程序,GPS应用程序和处理应用程序,仍然没有CommPortIdentifier对象识别出串口.
有什么建议 ?
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args)
{
System.out.println("Hello World");
portList = CommPortIdentifier.getPortIdentifiers();
JOptionPane.showMessageDialog(null,"portlist :"+portList.hasMoreElements());
int numelt=0 ;
while (portList.hasMoreElements())
{
numelt++;
portId = (CommPortIdentifier) portList.nextElement();
Run Code Online (Sandbox Code Playgroud)
很难确定,但你的问题听起来像以下之一:
程序初始化后,您已插入设备。
根据我的经验,程序初始化时可以识别 COM 端口。我的公司已经工作了几个月,试图找到一种方法来侦听正在插入的设备,但纯 Java 看起来不可能(这让我感到困惑)。尝试将其插入,然后启动程序。
您缺少驱动程序文件。
您正在使用 javax.comm,它需要文件 javax.comm.properties 和 win32comm.dll(您可能必须在 64 位系统上找到不同的驱动程序)。属性文件应放置在%JAVA_HOME%\lib\ 目录中,dll 则放置在%JAVA_HOME%\bin 中。
| 归档时间: |
|
| 查看次数: |
6327 次 |
| 最近记录: |