Results 1 to 8 of 8
  1. #1
    leeloo is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default Applet working in Eclipse but not in browser

    Hello,

    2 things first: 1. sorry for the english mistakes
    2. i'm new to java

    Ok, so i have to create an interface between a microcontroller connected to the computer via a serial cable ( RS 232 ) and the end user. I did this with an applet that i wanted to implement in a simpe web page an publish it on the big world wide web.

    I created 2 classes: SimpleRead and Applet

    SimpleRead takes care of the serial comunication and determines a variable called "temp" ( it is a temperature and it is a string ). At first i wrote this variable to the consol and everything was perfect.

    Then i created the applet class with an textField for the temperature. This calls the method SimpleRead. The actual writing of the temperature in the textField happens in the class SimpleRead.

    When i run the project in Eclipse it works perfect, meening that every 1-2 seconds the temperature is written in the textFiled again in the applet.

    The problem is that in the web page the textField remains empty.....

    Anyone any ideas on what should I change? Thanks in advance

    I don't know how much it will help if i write the code... but i will do so. So:

    SimpleRead.java

    Java Code:
    import java.io.*;
    import java.util.*;
    import gnu.io.*;
    import java.awt.*;
    import java.applet.*;
    
    public class SimpleRead implements Runnable, SerialPortEventListener
    {
        public static CommPortIdentifier 	portId;
        public static Enumeration	      	portList;
        public InputStream		      		inputStream;
        public OutputStream           		outputStream;
        public SerialPort		      		serialPort;
        public Thread		      			readThread;
        public Thread 			  			writeThread;
        static String 				emma=new String(" ");
        int 						numBytes;
        InputStreamReader 			in;
        char[] 						con=new char[100];
        String						var;
        char						var2;
        int 						j=0;
       public static String temp,but1,but2,led1,led2; 
    
        /* Constructor declaration*/
        
       TextField t,b1,b2;
       
        public SimpleRead(TextField textF, TextField text1, TextField text2) 
        {
        	t=textF;
        	b1=text1;
        	b2=text2;
        	
        	try 
        	{
        		serialPort = (SerialPort) portId.open("SimpleReadApp", 20000); 
        	} catch (PortInUseException e) {}
    
        	try
        	{
        		inputStream = serialPort.getInputStream();
        		outputStream = serialPort.getOutputStream();
        	} catch (IOException e) {}
    
        	try 	
        	{
        		serialPort.addEventListener(this);
        	} catch (TooManyListenersException e) {}
    
        	serialPort.notifyOnDataAvailable(true);
    
        	try 
        	{
        		serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
        	} catch (UnsupportedCommOperationException e) {}
    
        	readThread = new Thread(this); 
        	writeThread =new Thread(this);
    	
        	readThread.start();
        	writeThread.start();
        }
    
      
        
        public void run() 
        {
        	try 
        	{
        		Thread.sleep(20000);
        	} catch (InterruptedException e) {}
        } 
        
        @SuppressWarnings("restriction")
        
    	public void serialEvent(SerialPortEvent event) 
        {
        	try{
    			String enter = "9\r\n";
    	
    			System.out.println("Trimis: "+ enter+"\r\n");
    			outputStream.write(enter.getBytes());
    			emma="0";
    			} catch (IOException e) {}	
    		
        	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:
        				byte[] readBuffer = new byte[300];
        				
        				try 
        				{
        					while (inputStream.available() > 0)
        					{
        						numBytes = inputStream.read(readBuffer); 
        					} 
      
        					for(int i=0; i<numBytes; i++)  
        					{
        						System.out.print((char) (readBuffer[i]));    						
        						var2=(char) readBuffer[i]; // 1 letter at a time
        						con[i]=var2; // building the words one by one
        					}
        					
        					var=new String(con);
        					emma+=var; // putting all the words together
        					
        				} catch (IOException e) {}
    	    
        				break;
        	}
        	
      
    	       		int index_temp_start=emma.indexOf("temp:")+5;
    	       		int index_temp_end=emma.indexOf("\r\npb1:");
    	       		
    	       		temp=emma.substring(index_temp_start,index_temp_end);
    	       		
    	       		//System.out.println(" ");
    	       		//System.out.print(" !!!!! Temperatura:");
    	       		//System.out.print(temp);
    	       		//System.out.println(" ");
    	       		
    	       		t.setText(temp);
    	       		
    	       		index_temp_start=emma.indexOf("pb1:")+4;
    	       		index_temp_end=emma.indexOf("\r\npb2:");
    	       		
    	       		if(emma.substring(index_temp_start,index_temp_end).indexOf("rel") !=-1)
    	       	
    	       			but1="Relesed";
    	       		else
    	       			if(emma.substring(index_temp_start,index_temp_end).indexOf("pre") !=-1)
    	       				but1="Pressed";
    	       		
    	       		b1.setText(but1);
    	       		
    	       		index_temp_start=emma.indexOf("pb2:")+4;
    	       		index_temp_end=emma.indexOf("\r\nled1");
    	       		
    	       		if(emma.substring(index_temp_start,index_temp_end).indexOf("rel") !=-1)
    	    	       	
    	       			but2="Relesed";
    	       		else
    	       			if(emma.substring(index_temp_start,index_temp_end).indexOf("pre") !=-1)
    	       				but2="Pressed";
    	    
    	       		b2.setText(but2);
    	 
    	}
    }

    NotHelloAgain.java - the applet class of couse :D

    Java Code:
    import java.io.*;
    import java.util.*;
    
    import gnu.io.*;
    
    import java.awt.*;
    import java.applet.*;
    
    	public class NotHelloAgain extends Applet
    	{
    		static TextField temperatura;
    		static TextField buton2;
    		static TextField buton1;
    		
    		public void init()
    		{
    			boolean		      portFound = false;
    	    	String		      defaultPort = "COM8";
    			
    	    	setLayout(null);
    			
    			Label label;
    		    label = new Label("Temperatura");
    		    label.setBounds(20,20,90,15); 
    		    add(label);
    		    
    			 temperatura=new TextField(15); 
    			 temperatura.setBounds(120,20,100,20); 
    			add(temperatura);
    			
    			Label label2;
    		    label2 = new Label("Buton 1"); 
    		    label2.setBounds(20,60,90,15); 
    		    add(label2);
    		    
    			buton1=new TextField(15);
    			buton1.setBounds(120,60,100,20);
    			add(buton1);
    			
    			Label label3;
    		    label3 = new Label("Buton 2");
    		    label3.setBounds(20,100,90,15); 
    		    add(label3);
    		    
    			buton2=new TextField(15);   
    			buton2.setBounds(120,100,100,20);
    			add(buton2);
    			
    			
    	    	SimpleRead.portList = CommPortIdentifier.getPortIdentifiers();   
    
    	    	while (SimpleRead.portList.hasMoreElements()) 					
    	    	{
    	    		SimpleRead.portId = (CommPortIdentifier) SimpleRead.portList.nextElement();
    	    		if (SimpleRead.portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
    	    		{
    	    			if (SimpleRead.portId.getName().equals(defaultPort))  
    	    			{
    	    				System.out.println("Found port: "+defaultPort);  
    	    				portFound = true;
    	    				SimpleRead reader = new SimpleRead(temperatura,buton1,buton2); 	
    	    			} 
    	    		} 
    	    	}
    	    	
    	    	if (!portFound)
    	    	{
    	    		System.out.println("port " + defaultPort + " not found.");
    	    	}	 
    			
    		}
    		public void paint(Graphics g)
    		{
    			  g.drawRect(10,10,240,160);
    
    		}
    		}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't want to ignore exceptions:
    Java Code:
    catch (IOException e) {}
    Otherwise how will you know what's wrong with your program?

  3. #3
    leeloo is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default

    I know you're wright about the exceptions... and that i should tell my programm to do something when it cathes errors.

    But the program works, as i wrote in the post :) The temperature is displayed in the TextFiled in the applet. My problem is that it doesn't work in the browser...

    I may be wrong of course :), but i don't think that it has anything to do with the exceptions that could appear :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by leeloo View Post
    I know you're wright about the exceptions... and that i should tell my programm to do something when it cathes errors.

    But the program works, as i wrote in the post :) The temperature is displayed in the TextFiled in the applet. My problem is that it doesn't work in the browser...

    I may be wrong of course :), but i don't think that it has anything to do with the exceptions that could appear :)
    If it were me, until I know why it's not working, I would avoid making any assumptions. I would make sure to catch all exceptions and then look at the Java Console of my browser to make sure that no exceptions have been caught and sent.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Did you look in the browser's java console?
    Many errors will be written there.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Norm View Post
    Did you look in the browser's java console?
    Many errors will be written there.
    Not if he ignores exceptions. :rolleyes:

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    I was thinking of the JVM not finding classes that are present in the IDE.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 10
    Last Post: 10-11-2009, 08:07 PM
  2. Applet with Images in Browser
    By baron in forum Eclipse
    Replies: 0
    Last Post: 09-20-2009, 11:06 PM
  3. Applet not working in browser
    By gkr1989 in forum Java Applets
    Replies: 3
    Last Post: 07-03-2009, 10:43 AM
  4. applet won't run in browser...
    By shwein in forum Java Applets
    Replies: 6
    Last Post: 10-17-2008, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •