我在jsp中执行串口读取器代码时遇到问题。当我单独运行简单的读取器类时,我就能读取这些值。当这个类是从jsp启动时为了调用reader.But it没有被执行
import java.io.*;
import java.util.*; //import gnu.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.*;
public class SimpleRead1 implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
byte[] readBuffer;
String temperature;
String pressure;
int count=0;
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getPressure() {
return pressure;
}
public void setPressure(String pressure) {
this.pressure = pressure;
}
public void SimpleRead5()
{
System.out.println("Simpleread5");
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println(portList = CommPortIdentifier.getPortIdentifiers());
System.out.println(portList.hasMoreElements());
/* try {
System.out.println("portList... " + CommPortIdentifier.getPortIdentifier("COM5"));
} catch (NoSuchPortException ex) {
Logger.getLogger(SimpleRead.class.getName()).log(Level.SEVERE, null, ex);
}*/
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("port identified is Serial.. "
+ portId.getPortType());
if (portId.getName().equals("COM5")) {
System.out.println("port identified is COM4.. "
+ portId.getName());
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead1 reader = new SimpleRead1();
reader.SimpleRead4();
} else {
System.out.println("unable to open port");
}
}
}
}
public void SimpleRead4() {
try {
System.out.println("In SimpleRead() contructor");
serialPort = (SerialPort) portId.open("SimpleReadApp1111",500);
System.out.println(" Serial Port.. " + serialPort);
} catch (PortInUseException e) {
System.out.println("Port in use Exception");
}
try {
inputStream = serialPort.getInputStream();
System.out.println(" Input Stream... " + inputStream);
} catch (IOException e) {
System.out.println("IO Exception");
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println("Tooo many Listener exception");
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// no handshaking or other flow control
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
// timer on any read of the serial port
serialPort.enableReceiveTimeout(500);
System.out.println("................");
} catch (UnsupportedCommOperationException e) {
System.out.println("UnSupported comm operation");
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
System.out.println("In run() function ");
Thread.sleep(500);
// System.out.println();
} catch (InterruptedException e) {
System.out.println("Interrupted Exception in run() method");
}
}
String dataType="";
public void serialEvent(SerialPortEvent event) {
// System.out.println("In Serial Event function().. " + event +
// event.getEventType());
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:
readBuffer = new byte[8];
try {
while (inputStream.available()>0) {
int numBytes = inputStream.read(readBuffer);
// System.out.println("Number of bytes read " + numBytes);
}
String serialdata=new String(readBuffer);
if(serialdata.trim().equalsIgnoreCase("a")){
dataType="a";
}else if(serialdata.trim().equalsIgnoreCase("b")){
dataType="b";
}else{
if(dataType=="a"){
System.out.println("Temperature :"+serialdata.trim());
temperature=serialdata.trim();
setTemperature(temperature);
count++;
}else if(dataType=="b"){
System.out.println("Pressure :"+serialdata.trim());
pressure=serialdata.trim();
setPressure(pressure);
count++;
}
}
if(count==2)
{
System.out.println(getTemperature()+getPressure());
serialPort.notifyOnDataAvailable(false);
}
// System.out.println(new String(readBuffer));
} catch (IOException e) {
System.out.println("IO Exception in SerialEvent()");
}
break;
}
// System.out.println();
/* String one = new String(readBuffer);
char two = one.charAt(0);
System.out.println("Character at three: " + two);*/
}
}