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());
}

}
}
...全文
406 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
  • 打赏
  • 举报
回复
把代码格式化之后再贴上来看

62,620

社区成员

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

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