各位大大:
我写了个串行接口的程序,接受端口数据时采用轮循方式的时候没有问题。而采用监听方式的时候,事件触发不了,不知道为什么。请各位大大帮忙看看。
import javax.comm.*;
import java.io.*;
import java.util.*;
public class SerialRead{
String PortName;
CommPortIdentifier portId;
SerialPort serialPort;
public SerialRead(int port){
PortName = "COM" + port;
try
{
portId = CommPortIdentifier.getPortIdentifier(PortName);
try
{
serialPort = (SerialPort)portId.open("Serial_Communication", 20);
System.out.println("打开端口"+PortName+"成功");
}catch(PortInUseException e) {
System.out.println("打开端口失败,端口正在使用中");
}
try
{
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}catch(UnsupportedCommOperationException e) {
System.out.println("串行操作不支持");
}
try{
serialPort.addEventListener(new myPortListener());
System.out.println("添加监听器成功");
}catch(TooManyListenersException e){
System.out.println("添加监听器出错");
}
}catch(Exception e){
System.out.println("初始化失败");
}
}
public static void main(String[] args){
SerialRead aa = new SerialRead(1);
}
class myPortListener implements SerialPortEventListener{
InputStream input;
public void serialEvent(SerialPortEvent evt){
System.out.println("aaaaa");
switch (evt.getEventType()){
case SerialPortEvent.CTS:
System.out.println("清除发送");
break;
case SerialPortEvent.CD:
System.out.println("载波检测");
break;
case SerialPortEvent.BI:
System.out.println("通讯中断");
break;
case SerialPortEvent.DSR:
System.out.println("数据设备准备好");
break;
case SerialPortEvent.FE:
System.out.println("帧错误");
break;
case SerialPortEvent.OE:
System.out.println("溢位错误");
break;
case SerialPortEvent.PE:
System.out.println("奇偶校验错");
break;
case SerialPortEvent.RI:
System.out.println("振铃指示");
break;
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("输出缓冲区已清空");
break;
case SerialPortEvent.DATA_AVAILABLE:
System.out.println("有数据到达");
int n;
StringBuffer buf = new StringBuffer();
try{
input = serialPort.getInputStream();
}catch(IOException e){
}
try{
while((n=input.read()) > 0){
buf.append((char)n);
}
System.out.print(buf);
}catch(IOException e){
}
break;
default:
System.out.println("aa");
}
}
}
}
我写了个串行接口的程序,接受端口数据时采用轮循方式的时候没有问题。而采用监听方式的时候,事件触发不了,不知道为什么。请各位大大帮忙看看。
import javax.comm.*;
import java.io.*;
import java.util.*;
public class SerialRead{
String PortName;
CommPortIdentifier portId;
SerialPort serialPort;
public SerialRead(int port){
PortName = "COM" + port;
try
{
portId = CommPortIdentifier.getPortIdentifier(PortName);
try
{
serialPort = (SerialPort)portId.open("Serial_Communication", 20);
System.out.println("打开端口"+PortName+"成功");
}catch(PortInUseException e) {
System.out.println("打开端口失败,端口正在使用中");
}
try
{
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}catch(UnsupportedCommOperationException e) {
System.out.println("串行操作不支持");
}
try{
serialPort.addEventListener(new myPortListener());
System.out.println("添加监听器成功");
}catch(TooManyListenersException e){
System.out.println("添加监听器出错");
}
}catch(Exception e){
System.out.println("初始化失败");
}
}
public static void main(String[] args){
SerialRead aa = new SerialRead(1);
}
class myPortListener implements SerialPortEventListener{
InputStream input;
public void serialEvent(SerialPortEvent evt){
System.out.println("aaaaa");
switch (evt.getEventType()){
case SerialPortEvent.CTS:
System.out.println("清除发送");
break;
case SerialPortEvent.CD:
System.out.println("载波检测");
break;
case SerialPortEvent.BI:
System.out.println("通讯中断");
break;
case SerialPortEvent.DSR:
System.out.println("数据设备准备好");
break;
case SerialPortEvent.FE:
System.out.println("帧错误");
break;
case SerialPortEvent.OE:
System.out.println("溢位错误");
break;
case SerialPortEvent.PE:
System.out.println("奇偶校验错");
break;
case SerialPortEvent.RI:
System.out.println("振铃指示");
break;
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("输出缓冲区已清空");
break;
case SerialPortEvent.DATA_AVAILABLE:
System.out.println("有数据到达");
int n;
StringBuffer buf = new StringBuffer();
try{
input = serialPort.getInputStream();
}catch(IOException e){
}
try{
while((n=input.read()) > 0){
buf.append((char)n);
}
System.out.print(buf);
}catch(IOException e){
}
break;
default:
System.out.println("aa");
}
}
}
}
main里面只有一句,SerialRead创建完后,程序就结束了,哪有时间去监听?
应该加一个循环。或者,简单点,在main里面加上Thread.sleep(5000); 暂停5秒试试。
import javax.comm.*;
import java.io.*;
import java.util.*;import javax.swing.*;public class SerialRead{
... public static void main(String[] args){
MyFrame frame =
new MyFrame(
new SerialRead(Integer.parseInt(args[0]))); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack();
frame.setVisible(true);
}
...
}class MyFrame extends JFrame {
SerialRead aa;
public MyFrame(SerialRead aa) {
this.aa = aa;
}
}
虽然那个方法还是行不通,但是还是谢谢你。
现在的问题是可以确定SerialPort对象是存在的,但是监听器就是没有反应!!我白天要上课,没有太多的时间去研究串行包的特点。555。如果你需要的话,我可以把这个COMM.jar(for windows的,sun网上好像下不到了,只有for linux)和另外2个东西发到你邮箱里面。