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());
}
}
}
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());
}
}
}
解决方案 »
- jtextarea字体设置
- 〓〓大家来看看,用完list后需要做善后处理吗???〓〓
- 以下代码共创建了几个字符串对象 String a,b,c a ="java",b=a,c=a+b
- JAVA刚起步新手,求解答关于学习过程中的疑惑?
- 关于引入包的问题
- 很及很及很及.快快快!!!!!!!!!!!!!!!!!!!!!!!!!!
- 关于Event
- 抽象类继承实体类,是否需要实体类必须有明确的构造函数?
- java小问题
- java环境变量配置问题,javac不可用。path中写绝对路径javac可用,用%JAVA_HOME%就不可用
- 在线等!java查询出一个时间段内的日期
- Applet出现EXCEPTION_ACCESS_VIOLATION
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());
} }
}
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());
} }
}
通信端口 COM1端口
javax.comm包你有导入哦?运行的时候有报错吗