Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-07-2009, 11:59 AM
Member
 
Join Date: Jul 2009
Posts: 1
Rep Power: 0
j_jpg is on a distinguished road
Default 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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
applet, serial

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
3D Applet DzzD Java Applets 1 03-31-2009 12:20 AM
Calling another applet on click of button in one applet niteshwar.bhardwaj Java 2D 1 02-19-2009 01:54 PM
BFS using APPLET bharanidharanit Java Applets 3 12-30-2008 03:45 AM
First Applet HELP???? nvidia New To Java 0 08-13-2007 11:11 PM
Applet, To center text and To open I engage in a dialog in an Applet Marcus Java Applets 4 06-08-2007 07:15 AM


All times are GMT +2. The time now is 06:26 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org