2

I have a problem in executing serial port reader code in jsp.The thing while am running the simple reader class alone then am able to read the values.When this class is initiated from a jsp inorder to call the reader.But it is not getting executed

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);*/
    }

}
Jose Luis
  • 994
  • 1
  • 8
  • 22
Rinoy
  • 33
  • 8
  • The result of a jsp is a web page that shows in a browser. You are not in the server, you are in the browser. You need to read the serial port before you return the html code to the browser? – Jose Luis Apr 09 '16 at 08:11
  • is there any way to use this serial port reader in jsp? – Rinoy Apr 09 '16 at 08:12
  • The jsp is intended to return HTML to a browser. The jsp can execute java code, you only need to instantiate class SimpleRead1. But who returns this jsp could also instantiate this class, and give to the jsp the results. – Jose Luis Apr 09 '16 at 08:15
  • but i have instantiated this reader class in another java class . – Rinoy Apr 09 '16 at 08:27
  • Your jsp don't need to access to the serial port, only need the values readed from it. This class has to give this values to the jsp. – Jose Luis Apr 09 '16 at 08:32
  • still i want to instantiate that class from jsp rite? – Rinoy Apr 09 '16 at 08:34
  • Ok, I hope this helps: http://stackoverflow.com/questions/2889018/instantiating-classes-between-jsp-scriptlets – Jose Luis Apr 09 '16 at 08:40
  • yeah i have done that too.But it is returning false while checking the port.When am running the class alone it is returning true – Rinoy Apr 09 '16 at 08:46
  • Then you need to debug. Put a breakpoint in the SimpleRead1 class, and see if each variable has the value expected. – Jose Luis Apr 09 '16 at 08:48
  • yeah i have checked it by debugging also.Till that while condition the program goes correcrtly.When it comes to the while(portlist.hasmoreelements()) it returns false and program execution stops – Rinoy Apr 09 '16 at 08:50
  • http://stackoverflow.com/questions/15955530/commportidentifier-getportidentifiers-is-empty. Perhaps you have to add some library to the web application than have the jsp. – Jose Luis Apr 09 '16 at 08:54
  • yeah i have added all those unzipped files in the jre folders properly.And the program executes while running that class alone.When accessing that class from jsp it goes wrong – Rinoy Apr 09 '16 at 08:58
  • I can't help then. I hope someone else could helpt to you. I'm sorry. :-( – Jose Luis Apr 09 '16 at 09:07
  • You could show the jsp also. – Jose Luis Apr 09 '16 at 09:08
  • I suggest to change the name of `SimpleRead5()` and `SimpleRead4()`. They seem constructors, but you class name is `SimpleRead1`. You could name them in lowercase. – Jose Luis Apr 09 '16 at 09:12

0 Answers0