This discussion is archived
4 Replies Latest reply: Sep 20, 2012 1:46 AM by EJP RSS

comm.jar not working in Applet but works in Eclipse

962820 Newbie
Currently Being Moderated
Hello,

Please help me to read serial port data from Java Applet.

The below code working well and get data from weighing machine when we run in eclipse(Run Applet). But it now working when we use class file in Applet.
I think its security issue, but i still could not understand what we need to do.
I put the JOptionPane.showDialog and found that the code is crashing in line CommPortIdentifier.getPortIdentifiers(); (Not understood why try catch not working.)
I am using in Windows 7 environment.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.logging.Logger;

import javax.comm.*;
import javax.swing.*;

public class SimpleApplet extends JApplet {

public void init() {
     
JButton button = new JButton("Click me!");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {                 
     JOptionPane.showMessageDialog(SimpleApplet.this, "hello");
JOptionPane.showMessageDialog(SimpleApplet.this, Getdata());
}
});

add(button, BorderLayout.CENTER);
setBackground(Color.GRAY);
}

public String Getdata()
{
     try
     {         
          String drivername = "com.sun.comm.Win32Driver";
try
{
CommDriver driver = (CommDriver) Class.forName(drivername).newInstance();
driver.initialize();
}
catch (Exception e) { //just do nothing, it doesn't really matter
}

     Enumeration portList=null;
CommPortIdentifier portId;
SerialPort serialPort;
OutputStream outputStream;

try
{
     portList = CommPortIdentifier.getPortIdentifiers();
}catch(Exception ex)
{
     JOptionPane.showMessageDialog(SimpleApplet.this, "erorr:" + ex.getStackTrace().toString());
}
JOptionPane.showMessageDialog(SimpleApplet.this, "get port lsit");
          while(portList.hasMoreElements())
          {
               portId = (CommPortIdentifier) portList.nextElement();
               if(portId.getPortType()== CommPortIdentifier.PORT_SERIAL)
               {
                    if(portId.getName().equals("COM1"))
                    {
                         try
                         {
                              serialPort = (SerialPort) portId.open("WeightMachine",200);                              
                              serialPort.setSerialPortParams(1200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                              
                              outputStream = serialPort.getOutputStream();
                              InputStream mInputFromPort = serialPort.getInputStream();
                              
                              outputStream.write("W".getBytes());
                              outputStream.flush();
                              Thread.sleep(500);
                              byte mBytesIn [] = new byte[20];
                              mInputFromPort.read(mBytesIn);
                              //mInputFromPort.read(mBytesIn);
                              String value = new String(mBytesIn);
                              mInputFromPort.close();
                              serialPort.close();
                              return value.replace("+00", "").replace(" Kg", "");
                         }catch(Exception ex)
                         {
                              return ex.getMessage();
                         }
                    }
               }
               
          }
          }catch(Exception ex)
          {
               JOptionPane.showMessageDialog(SimpleApplet.this, "erorr last:" + ex.getStackTrace().toString());
          }
          return "Not found";
}
}

Thanks in advance.

Avinash

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points