help help ! java 读取xp 系统下串口,不能获取到串口

skyboy1 2009-11-10 06:40:27
portList.hasMoreElements();总是false,????
package com.century.chuangkou;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class Read implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
private java.io.FileOutputStream fos = null; // 定义日志函数

InputStream inputStream;
SerialPort serialPort;
Thread readThread;
private String portName;

public void readPort() {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("interface:"+portList.hasMoreElements());
while (portList.hasMoreElements()) {//这里总是false,为什么呀?
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(portName)) {
// if (portId.getName().equals("/dev/term/a")) {
Read reader = new Read();
}
}
}
}
public Enumeration<?> getPortList(){


return CommPortIdentifier.getPortIdentifiers();

}
public Read() {

}
public void init(){
try {
serialPort = (SerialPort) portId.open("ReadApp", 2000);
} catch (PortInUseException e) {
System.out.print("串口打开失败");
}
try {
fos = new java.io.FileOutputStream("c:\\log.txt");// 建立日志文件
System.out.print("串口数据");
inputStream = serialPort.getInputStream();
} catch (IOException e) {
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
}
}

public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[8];

try {

while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}

System.out.print(new String(readBuffer)); // 打印串口数据
fos.write(readBuffer); // 将读到的数据写到日志文件中

} catch (IOException e) {
}
break;
}
}

public String getPortName() {
return portName;
}
public void setPortName(String portName) {
this.portName = portName;
}
public static void main(String[] args) {
Read read=new Read();

while (read.getPortList().hasMoreElements()) {
portId = (CommPortIdentifier) read.getPortList().nextElement();
System.out.println("interface:"+portId.getName());
}

}
}
...全文
408 15 打赏 收藏 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰释绿茵 2011-07-05
  • 打赏
  • 举报
回复
求指教,同样问题
715655775
冰释绿茵 2011-07-05
  • 打赏
  • 举报
回复
求指教,同样问题
xiaokangruan 2010-05-18
  • 打赏
  • 举报
回复
哥们,你这是怎么弄的呢?378541753,望指教哈
skyboy1 2009-11-12
  • 打赏
  • 举报
回复
没有,我换rxtx读取就没有问题了。谢谢了。
scorpiomiracle 2009-11-12
  • 打赏
  • 举报
回复
解决了没有啊
timeriver_wang 2009-11-11
  • 打赏
  • 举报
回复
见识了
skyboy1 2009-11-11
  • 打赏
  • 举报
回复
导入,没有报错
一头头 2009-11-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 skyboy1 的回复:]
COM和LCP
通信端口 COM1端口
[/Quote]

javax.comm包你有导入哦?运行的时候有报错吗
skyboy1 2009-11-10
  • 打赏
  • 举报
回复
COM和LCP
通信端口 COM1端口
一头头 2009-11-10
  • 打赏
  • 举报
回复
如果com1是sql硬盘备份口的话 就认不出来的

我的电脑 -> 属性 -> 硬件 -> 设备管理器

你看下你的COM1写的是什么? Communication(通讯端口) 还是其他什么?
skyboy1 2009-11-10
  • 打赏
  • 举报
回复
怎么用呀?我对串口 不懂。但是,我的机器上确实有个COM1口
一头头 2009-11-10
  • 打赏
  • 举报
回复
代码没有问题 你用device manager看一下 你xp有serial port存在吗?
skyboy1 2009-11-10
  • 打赏
  • 举报
回复

package com.century.chuangkou;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class Read implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
private java.io.FileOutputStream fos = null; // 定义日志函数

InputStream inputStream;
SerialPort serialPort;
Thread readThread;
private String portName;

public void readPort() {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("interface:" + portList.hasMoreElements());
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(portName)) {
// if (portId.getName().equals("/dev/term/a")) {
Read reader = new Read();
}
}
}
}

public Enumeration<?> getPortList() {

return CommPortIdentifier.getPortIdentifiers();

}

public Read() {

}

public void init() {
try {
serialPort = (SerialPort) portId.open("ReadApp", 2000);
} catch (PortInUseException e) {
System.out.print("串口打开失败");
}
try {
fos = new java.io.FileOutputStream("c:\\log.txt");// 建立日志文件
System.out.print("串口数据");
inputStream = serialPort.getInputStream();
} catch (IOException e) {
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
}
}

public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[8];

try {

while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}

System.out.print(new String(readBuffer)); // 打印串口数据
fos.write(readBuffer); // 将读到的数据写到日志文件中

} catch (IOException e) {
}
break;
}
}

public String getPortName() {
return portName;
}

public void setPortName(String portName) {
this.portName = portName;
}

public static void main(String[] args) {
Read read = new Read();

while (read.getPortList().hasMoreElements()) {
portId = (CommPortIdentifier) read.getPortList().nextElement();
System.out.println("interface:" + portId.getName());
}

}
}
skyboy1 2009-11-10
  • 打赏
  • 举报
回复
package com.century.chuangkou;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class Read implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
private java.io.FileOutputStream fos = null; // 定义日志函数

InputStream inputStream;
SerialPort serialPort;
Thread readThread;
private String portName;

public void readPort() {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("interface:" + portList.hasMoreElements());
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(portName)) {
// if (portId.getName().equals("/dev/term/a")) {
Read reader = new Read();
}
}
}
}

public Enumeration<?> getPortList() {

return CommPortIdentifier.getPortIdentifiers();

}

public Read() {

}

public void init() {
try {
serialPort = (SerialPort) portId.open("ReadApp", 2000);
} catch (PortInUseException e) {
System.out.print("串口打开失败");
}
try {
fos = new java.io.FileOutputStream("c:\\log.txt");// 建立日志文件
System.out.print("串口数据");
inputStream = serialPort.getInputStream();
} catch (IOException e) {
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
}
}

public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[8];

try {

while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}

System.out.print(new String(readBuffer)); // 打印串口数据
fos.write(readBuffer); // 将读到的数据写到日志文件中

} catch (IOException e) {
}
break;
}
}

public String getPortName() {
return portName;
}

public void setPortName(String portName) {
this.portName = portName;
}

public static void main(String[] args) {
Read read = new Read();

while (read.getPortList().hasMoreElements()) {
portId = (CommPortIdentifier) read.getPortList().nextElement();
System.out.println("interface:" + portId.getName());
}

}
}
SunFulong 2009-11-10
  • 打赏
  • 举报
回复
把代码格式化之后再贴上来看
代码下载链接: https://pan.quark.cn/s/a4b39357ea24 用户账户控制(UAC)白名单的配置 Windows7环境中 UAC(User Account Control,用户帐户控制)是由微软在Windows Vista版本中推出的一项旨在增强系统安全性的创新技术,该技术强制要求用户在执行可能干扰计算机正常运作的操作或进行更改会波及其他用户设置的变动前,必须提供相应的权限或管理员密码进行验证。通过对这些操作启动前进行授权确认,UAC能够有效阻止恶意软件及间谍软件在未获授权的状态下于计算机内进行安装或实施修改。 自从Vista版本问世以来,微软便开始推行这一全新的安全机制,可视为对系统安全防护的显著提升。尽管UAC确实能够在一定程度上对某些非法程序起到防御作用,但与此同时,这一功能也给众多用户带来了诸多不便。 因此,许多用户开始探寻是否存在类似于白名单的功能,以便将那些值得信赖的程序直接赋予运行权限。事实上,这类功能确实存在,不过微软并未将其作为标准配置提供。 网络上关于此问题的绝大多数建议都是建议禁用UAC,这种说法显然缺乏针对性,因为若用户希望禁用此功能,本就不会提出相关疑问。 通过运用微软官方发布的Microsoft Application Compatibility Toolkit 5.6版本,可以将信任的程序纳入系统白名单范畴。 获取Application Compatibility Toolkit 安装程序成功后会出现三个可执行文件 以管理员身份启动Compatibility Administrator 在Custom DataBases部分创建新的数据库,并添加一个Application Fix(在下方空白处点击右键,选择...

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧