我用java写了一个读写串口的小程序,貌似可以写进去,但不知道为什么就是读不出来,各位啊 帮帮忙
下面是我的程序
package c;import java.io.*;
import java.util.*;import javax.comm.*;public class Communication implements SerialPortEventListener {
SerialPort mySerialPort = null;
BufferedReader reader = null;//从串口输入内存的流
BufferedOutputStream writer = null;//从内存向串口输出的流
public Communication(){
//获得COM2串口
try {
CommPortIdentifier commPort = CommPortIdentifier.getPortIdentifier("COM2");
mySerialPort = (SerialPort) commPort.open("portApplication", 5000);
} catch (NoSuchPortException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mySerialPort.addEventListener(this);
} catch (TooManyListenersException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.exit(-1);
}
mySerialPort.notifyOnDataAvailable(true);
//设置串口读写参数
try {
mySerialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
mySerialPort.setFlowControlMode(mySerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(-1);
}
//从此还口中获得输入输出流
try {
reader = new BufferedReader(new InputStreamReader(mySerialPort.getInputStream()));
writer = new BufferedOutputStream(mySerialPort.getOutputStream());
writer.write(123123);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("程序能运行到这里");
} //监听串口是否有数据并读
public void serialEvent(SerialPortEvent event) {
// TODO Auto-generated method stub
System.out.println("程序怎么运行不到这里啊?????"); switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据,并且给串口返回数据 try {
System.out.println(reader.readLine());
reader.close();
mySerialPort.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} break;
}
} public static void main(String[] args) {
Communication comm = new c.Communication();
}
}
下面是我的程序
package c;import java.io.*;
import java.util.*;import javax.comm.*;public class Communication implements SerialPortEventListener {
SerialPort mySerialPort = null;
BufferedReader reader = null;//从串口输入内存的流
BufferedOutputStream writer = null;//从内存向串口输出的流
public Communication(){
//获得COM2串口
try {
CommPortIdentifier commPort = CommPortIdentifier.getPortIdentifier("COM2");
mySerialPort = (SerialPort) commPort.open("portApplication", 5000);
} catch (NoSuchPortException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mySerialPort.addEventListener(this);
} catch (TooManyListenersException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.exit(-1);
}
mySerialPort.notifyOnDataAvailable(true);
//设置串口读写参数
try {
mySerialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
mySerialPort.setFlowControlMode(mySerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(-1);
}
//从此还口中获得输入输出流
try {
reader = new BufferedReader(new InputStreamReader(mySerialPort.getInputStream()));
writer = new BufferedOutputStream(mySerialPort.getOutputStream());
writer.write(123123);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("程序能运行到这里");
} //监听串口是否有数据并读
public void serialEvent(SerialPortEvent event) {
// TODO Auto-generated method stub
System.out.println("程序怎么运行不到这里啊?????"); switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据,并且给串口返回数据 try {
System.out.println(reader.readLine());
reader.close();
mySerialPort.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} break;
}
} public static void main(String[] args) {
Communication comm = new c.Communication();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货