【发布时间】:2012-10-03 21:48:08
【问题描述】:
我的 Java 程序无法检测到连接到计算机的串行端口设备。
我使用的是 Windows 7(64 位)计算机()。
串口设备是一个 GPS 模块,它以 37860 波特输出文本 GPS 字符串,连接到插入笔记本电脑 USB 端口的 Serial-USB 转换器。
Windows 控制面板/设备管理器将此设备显示为 COM7。
在计算机上运行的 GPS 应用程序“MiniGPS”能够与作为 COM7 的 GPS 芯片进行双向通信。
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();
【问题讨论】:
-
您必须在启动应用程序时加载 win32com.dll。你这样做了吗??
标签: java serial-port