ok, ya logre comunicarme con el dispositivo conectado al puerto serie, pero desde una aplicacion, aqui el codigo:
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.comm.*;
public class Bascula implements Runnable, SerialPortEventListener {
static String messageString = "P";
static SerialPort serialPort;
static OutputStream outputStream;
static CommPortIdentifier portId;
static Enumeration portList;
static InputStream inputStream;
static Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//-------------------ciclo
//-------------------escritura
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort)
portId.open("BasculaApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
//---------------fin escritura
//---------------parametros lectura
Bascula reader = new Bascula();
}
}
}
}
public Bascula() {
try {
serialPort = (SerialPort) portId.open("BasculaApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
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) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
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:
byte[] readBuffer = new byte[10];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
/*-----------------------------
ahora quiero convertir este programita en un applet lo que hice fue lo siguiente:
*/
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.comm.*;
import java.applet.*;
public class bascapplet extends Applet
implements Runnable, SerialPortEventListener {
static String messageString = "P";
static SerialPort serialPort;
static OutputStream outputStream;
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
Thread readThread;
String dato;
public void init() {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//-------------------ciclo
//-------------------escritura
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort)
portId.open("bascappletApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
//---------------fin escritura
//---------------parametros lectura
bascapplet reader = new bascapplet();
}
}
}
try {
serialPort = (SerialPort) portId.open("bascappletApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
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) {}
readThread = new Thread(this);
}
public void start() {
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
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:
byte[] readBuffer = new byte[10];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
//System.out.print(new String(readBuffer));
dato=new String(readBuffer);
} catch (IOException e) {}
break;
}
}
public void paint( Graphics g ) {
g.drawString( "*"+dato+"*",25,25 );
}
}
/*
ya firme el applet, modifique la seguridad con el policytool, y nomas no funcia, si alguien me puede ayudar con este revoltijo, gracias...
saludos
*/