[code="java"]
package com.hxks.xsm;
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
//读取设备端口号
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
//System.out.println("[]____________");
while (portList.hasMoreElements()) {
System.out.println("||||||||||||||||");
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//这里根本没判断
{
System.out.println("_________");
if (portId.getName().equals("COM1"))//而我本机是开了com1串口 进行发数据的
{
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
System.out.println("............");
}
}
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
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;
//DATA_AVAILABLE 为设备有数据情况
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
[/code]
ps 关于javacomm的 一些文章都看了 什么javax.comm.properties我也放到C:\Program Files\Java\jdk1.5.0\lib目录之下了 ,还有
win32得那个dll 也放在C:\Program Files\Java\jdk1.5.0\bin之下了 ,jar导入项目中。
另外一些人 由于win32的 冲突问题还报错 我这个程序啥错不报。
[b]问题补充:[/b]
package com.hxks.xsm;
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
//读取设备端口号
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
//System.out.println("[]____________");
while (portList.hasMoreElements()) {
System.out.println("||||||||||||||||");
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//这里根本没判断
{
System.out.println("_________");
if (portId.getName().equals("COM1"))//而我本机是开了com1串口 进行发数据的
{
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
System.out.println("............");
}
}
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
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;
//DATA_AVAILABLE 为设备有数据情况
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
ps 关于javacomm的 一些文章都看了 什么javax.comm.properties我也放到C:\Program Files\Java\jdk1.5.0\lib目录之下了 ,还有
win32得那个dll 也放在C:\Program Files\Java\jdk1.5.0\bin之下了 ,jar导入项目中。
另外一些人 由于win32的 冲突问题还报错 我这个程序啥错不报。
多谢lx的朋友解答 但是这个myeclipse 自带的是j2ee的jar 我用的是j2sdk的呵
[b]问题补充:[/b]
多谢lx的朋友解答 但是这个myeclipse 自带的是j2ee的jar 我用的是j2sdk的呵