Results 1 to 1 of 1
Thread: Applet with RS-232
- 07-07-2009, 10:59 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
Applet with RS-232
Hi,
I'm new, and I want develop a Applet to communicate with RS-232, I use RxTX libraries. I try to convert an application sample (that works fine) into a applet, but it doesn't work.
Here is the code:
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.applet.Applet;
import java.awt.Graphics;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TwoWaySerialComm extends Applet
{
StringBuffer buffer;
CommPort commPort;
CommPortIdentifier portId;
SerialPort serialPort;
InputStream inputStream;
public void init()
{
buffer = new StringBuffer();
addItem("initializing... ");
// initalize serial port
try
{
showStatus("1");
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM2");
/*
if ( portId.isCurrentlyOwned() )
{
showStatus("1.5");
System.out.println("Error: Port is currently in use");
}
else
{
showStatus("2");
CommPort commPort = portId.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DAT ABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NO NE);
showStatus("3");
InputStream inputStream = serialPort.getInputStream();
serialPort.notifyOnDataAvailable(true);
showStatus("4");
//serialPort.addEventListener(this);
serialPort.addEventListener(new SerialReader(inputStream));
showStatus("5");
}
}
*/
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void start() {
addItem("starting... ");
}
public void stop() {
addItem("stopping... ");
}
public void destroy() {
addItem("preparing for unloading...");
}
void addItem(String newWord) {
System.out.println(newWord);
buffer.append(newWord);
repaint();
}
public void paint(Graphics g) {
//Draw a Rectangle around the applet's display area.
g.drawRect(0, 0, getWidth() - 1,getHeight() - 1);
//Draw the current string inside the rectangle.
g.drawString(buffer.toString(), 5, 15);
}
public class SerialReader implements SerialPortEventListener
{
private InputStream in;
private byte[] buffer = new byte[1024];
public SerialReader ( InputStream in )
{
this.in = in;
}
public void serialEvent(SerialPortEvent event)
{
showStatus("Evento Port Serial");
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 data;
try
{
int len = 0;
byte[] buffer = new byte[1024];
while ( ( data = inputStream.read()) > -1 )
{
if ( data == '\n' )
{
break;
}
buffer[len++] = (byte) data;
}
showStatus("Leido Código");
}
catch ( IOException e )
{
showStatus("Error");
e.printStackTrace();
System.exit(-1);
}
}
}
}
}
Someone has any idea?
Than you
Similar Threads
-
3D Applet
By DzzD in forum Java AppletsReplies: 1Last Post: 03-30-2009, 11:20 PM -
Calling another applet on click of button in one applet
By niteshwar.bhardwaj in forum Java 2DReplies: 1Last Post: 02-19-2009, 12:54 PM -
BFS using APPLET
By bharanidharanit in forum Java AppletsReplies: 3Last Post: 12-30-2008, 02:45 AM -
First Applet HELP????
By nvidia in forum New To JavaReplies: 0Last Post: 08-13-2007, 10:11 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks