Re: Access local COM-port on client from an Applet
- From: "JimWilh" <jim.wilhelmsen@xxxxxxxxx>
- Date: 30 Jun 2005 06:00:26 -0700
Here is the class that read from the port:
import java.io.*;
import java.util.*;
import javax.comm.*;
public 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) {}
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){}
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
And here is the simple applet.
import java.awt.Graphics;
import java.io.*;
import java.awt.event.*;
public class TestTagApplet extends 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
.
- Follow-Ups:
- Re: Access local COM-port on client from an Applet
- From: Andrew Thompson
- Re: Access local COM-port on client from an Applet
- References:
- Access local COM-port on client from an Applet
- From: JimWilh
- Re: Access local COM-port on client from an Applet
- From: JimWilh
- Re: Access local COM-port on client from an Applet
- From: Andrew Thompson
- Access local COM-port on client from an Applet
- Prev by Date: Re: drawing arrows...
- Next by Date: back into the swing (no pun intended)
- Previous by thread: Re: Access local COM-port on client from an Applet
- Next by thread: Re: Access local COM-port on client from an Applet
- Index(es):