各位大大:
我写了个串行接口的程序,接受端口数据时采用轮循方式的时候没有问题。而采用监听方式的时候,事件触发不了,不知道为什么。请各位大大帮忙看看。
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");
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货