Re: Access local COM-port on client from an Applet



On 30 Jun 2005 06:00:26 -0700, JimWilh wrote:

> Here is the class ..

OK, if I rework your example a little so
that it compiles in a single file[1], and add
this HTML[2], then drop the classes, the HTML and
the comm.jar into the same directory and open the
HTML, then click 'read tag' I get..

Exception in thread "AWT-EventQueue-117"
java.lang.ExceptionInInitializerError
at ReadPitTag.open(TestTagApplet.java:61)
.......
Caused by: java.security.AccessControlException: access denied

And that is the important one.
Are you getting similar results?

...and where is that URL I was asking for? I could know by now
if your applet class is properly signed, if the comm.jar is in the
right place, if it is even mentioned in the applet element..

This is gooing to go very slowly if you do not provide an URL.

[1]
<sscce>
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.comm.*;

public class TestTagApplet
extends java.applet.Applet
implements ActionListener {

String utData = null;
TextField resultField;
Button readButton;

public void init() {
setLayout(new FlowLayout());
readButton = new Button("Read Tag");
resultField = new TextField();
readButton.addActionListener(this);
add(readButton);
add(resultField);

} // End init()

public void actionPerformed(ActionEvent act){

if (act.getSource() == readButton){
ReadPitTag readTag = new ReadPitTag("COM1");
readTag.open();
while(!readTag.isDone()){
}
utData = readTag.getResult();
}

} // End actionPerformed()


} // End class TestTagApplet


class ReadPitTag implements
Runnable, SerialPortEventListener {

CommPortIdentifier portId;
String portName;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
String port;
static String result = null;
boolean done = false;

public ReadPitTag(String comm) {
super();
portName = comm;
} // End of Constructor ReadPitTag(String)

public void open(){
done = false;
try {
portId = CommPortIdentifier.getPortIdentifier(portName);
serialPort = (SerialPort) portId.open("LesPitTag", 2000);
} catch (Exception e) {
e.printStackTrace();
}

try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}

try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);

try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
} // End open()

public void close() {
serialPort.close();
done = true;
} // End close()

public void run(){}

public boolean isDone(){
return done;
}

public String getResult(){
return result;
}

public void serialEvent(SerialPortEvent event) {
try {
Thread.sleep(200);
} catch(Exception e){
e.printStackTrace();
}

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:
int maxChars = 1024;
byte[] readBuffer = new byte[maxChars];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
String fullResult = new String(readBuffer);
result = fullResult.substring(5, 15);
this.close();
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
break;
} // End switch
} // End serialEvent()
} // End class ReadPitTag
</sscce>

[2]
--- start index.html ---
<HTML>
<BODY>
<APPLET
CODE="TestTagApplet.class"
CODEBASE="."
archive=comm.jar
WIDTH=400
HEIGHT=300>
</APPLET>
</BODY>
</HTML>
--- end index.html ---

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
.


Quantcast