En si el programa transmite y recibe, el problema es que el pic manda a travez del puerto serial a mi pc datos de este tipo: \2\4\4 ("creo que es un tipo de seguridad") y lo unico que recibo son 3 espacios en blanco en la consola de java, en hyperterminal recibo unas caras como puedo hacer para recibirlo como 244 en java, trabaje con la rxtxcomm.jar y la GyovinetDriver.jar, en la Gyovinet.jar al convertir los datos que entraban en String recibia los datos pero acompañado de muchos ceros, trate de separarlos y no se dejaron separar, la programacion del pic no debo cambiarla por politicas de empresa donde trabajo, y aqui el programa hecho con rxtx.
Código:
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
//import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Juan
*/
public class TwoWaySerialComm {
public TwoWaySerialComm()
{
super();
}
void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
// (new Thread(new SerialWriter(out))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
/** */
public static class SerialReader implements Runnable
{
InputStream in;
public SerialReader ( InputStream in )
{
this.in = in;
}
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
String valor=" ";
try {
while ((len = this.in.read(buffer)) > -1) {
valor = (new String(buffer, 0, len));
String nuv = valor.toString();
//String nuv = valor.toString();
// int numEntero = Integer.parseInt(nuv);
//System.out.print("receive" + numEntero);
System.out.print("receive"+valor);
}
}
catch ( IOException e )
{
//e.printStackTrace();
//}
// }
}
}
}
/** */
// public static class SerialWriter implements Runnable
//{
// OutputStream out;
//public SerialWriter ( OutputStream out )
//{
// this.out = out;
// }
// public void run ()
// {
// try
// {
// System.out.println("Listo");
// this.out.write("Listo pa escribir".getBytes());
// int c = 0;
// while ( ( c = System.in.read()) > -1 )
// {
// this.out.write(c);
// }
// }
// catch ( IOException e )
// {
//e.printStackTrace();
// }
//}
// }
public static void main ( String[] args )
{
try
{
(new TwoWaySerialComm()).connect("COM1");
}
catch ( Exception e )
{
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
}