Buen dia...
Para Antonino Linares ...
Para descartar el tema de mapeo en sesiones remota, adapte un c贸digo de java para leer puertos seriales,
y el resultado es que funciona muy bien tanto en modo local como en sesiones remota.
Queda entonces la duda de porque el ReadComm( nComm, @cBuffer) , se conecta al puerto pero no retorna ninguna cadena...(solo en sesi贸n remota)
Gracias
Johnson Russi
Anexo c贸digo de java
package comunication;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.*;
import java.util.*;
/**
聽* Este es un ejemplo de uso del API de Comunicaciones Java que permite la
聽* lectura de informaci贸n a trav茅s de uno de los puertos serie de la
聽* m谩quina en que se ejecuta.
聽* El ejemplo se ha probado en Windows y Solaris, utilizando la l铆nea
聽* de c贸digo que identifica el puerto a utilizar correspondiente
聽*/
public class Comunication implements Runnable,SerialPortEventListener {
聽 static CommPortIdentifier idPuerto;
聽 static Enumeration listaPuertos;
聽 InputStream entrada;
聽 SerialPort puertoSerie;
聽 Thread tLectura;
聽 // En este ejemplo implementa un thread que es el que se encarga de
聽 // que la aplicaci贸n se quede esperando en el puerto que se haya
聽 // abierto a que se reciban datos.
聽 // Primero abre el puerto y luego le fija los par谩metros
聽 public Comunication() {
聽 聽 // Si el puerto no est谩 en uso, se intenta abrir
聽 聽 try {
聽 聽 聽 puertoSerie = (SerialPort)idPuerto.open( "AplLectura",2000 );
聽 聽 } catch( PortInUseException e ) {
聽 聽 System.out.println(e);
聽 聽 }
聽 聽 // Se obtiene un canal de entrada
聽 聽 try {
聽 聽 聽 entrada = puertoSerie.getInputStream();
聽 聽 } catch( IOException e ) {
聽 聽 System.out.println(e);
聽 聽 }
聽 聽 聽
聽 聽 // A帽adimos un receptor de eventos para estar informados de lo
聽 聽 // que suceda en el puerto
聽 聽 try {
聽 聽 聽 puertoSerie.addEventListener( this );
聽 聽 聽 } catch( TooManyListenersException e ) {
聽 聽 聽 聽 聽 System.out.println("Peso : ");
聽 聽 聽 聽 聽 System.out.println(e);
聽 聽 聽 聽 聽 }
聽 聽
聽 聽 // Hacemos que se nos notifique cuando haya datos disponibles
聽 聽 // para lectura en el buffer de la puerta
聽 聽 puertoSerie.notifyOnDataAvailable( true );
聽 聽
聽 聽 // Se fijan los par谩metros de comunicaci贸n del puerto
聽 聽 try {
聽 聽 聽 puertoSerie.setSerialPortParams( 9600,
聽 聽 聽 聽 SerialPort.DATABITS_8,
聽 聽 聽 聽 SerialPort.STOPBITS_1,
聽 聽 聽 聽 SerialPort.PARITY_NONE );
聽 聽 } catch( UnsupportedCommOperationException e ) {
聽 聽 聽 聽 System.out.println(e);
聽 聽 }
聽 聽
聽 聽 // Se crea y lanza el thread que se va a encargar de quedarse
聽 聽 // esperando en la puerta a que haya datos disponibles
聽 聽 tLectura = new Thread( this );
聽 聽 tLectura.start();
聽 聽 }
聽
聽 聽 @Override
聽 public void run() {
聽 聽 try {
聽 聽 聽 // En los threads, hay que procurar siempre que haya alg煤n
聽 聽 聽 // m茅todo de escape, para que no se queden continuamente
聽 聽 聽 // bloqueados, en este caso, la comprobaci贸n de si hay datos
聽 聽 聽 // o no disponibles en el buffer de la puerta, se hace
聽 聽 聽 // intermitentemente
聽 聽 聽 聽 System.out.println("Entra hilo");
聽 聽 聽 Thread.sleep( 2000 );
聽 聽 } catch( InterruptedException e )
聽 聽 {
聽 聽
聽 聽 聽 聽 System.out.println(e);
聽 聽 }
聽 聽 }
聽
聽 聽 @Override
聽 public void serialEvent( SerialPortEvent _ev ) {
聽 聽 聽 System.out.println("Entra evento serial");
聽 聽 switch( _ev.getEventType() ) {
聽 聽 聽 // La mayor铆a de los eventos no se trata, 茅stos son los
聽 聽 聽 // que se producen por cambios en las l铆neas de control del
聽 聽 聽 // puerto que se est谩 monitorizando
聽 聽 聽 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;
聽 聽 聽 // Cuando haya datos disponibles se leen y luego se
聽 聽 聽 // imprime lo recibido en la consola
聽 聽 聽 case SerialPortEvent.DATA_AVAILABLE:
聽 聽 聽 聽 byte[] bufferLectura = new byte[20];
聽 聽 聽 聽 try {
聽 聽 聽 聽 聽 while( entrada.available() > 0 ) {
聽 聽 聽 聽 聽 聽 int nBytes = entrada.read( bufferLectura );
聽 聽 聽 聽 聽 聽 }
聽 聽 聽 聽 聽 System.out.print( new String(bufferLectura) );
聽 聽 聽 聽 聽
聽 聽 聽 聽 聽
聽 聽 聽 聽 } catch( IOException e ) {
聽 聽 聽 聽 System.out.println(e);
聽 聽 聽 聽 }
聽 聽 聽 聽 break;
聽 聽 聽 };
聽 聽 聽 //System.out.println("validar datos le铆dos y si son validos salir del app : ");
聽 聽 聽 //System.exit(-1);
聽 聽 }
聽
聽
聽 public static void main( String[] args ) {
聽 聽 // Lista de los puertos disponibles en la m谩quina. Se carga en el
聽 聽 // mimo momento en que se inicia la JVM de Java
聽 聽 listaPuertos = CommPortIdentifier.getPortIdentifiers();
聽 聽 while( listaPuertos.hasMoreElements() ) {
聽 聽 聽 idPuerto = (CommPortIdentifier)listaPuertos.nextElement();
聽 聽 聽 if( idPuerto.getPortType() == CommPortIdentifier.PORT_SERIAL ) {
聽 聽 聽 聽 if( idPuerto.getName().equals("COM4") ) { 聽 聽 聽 聽 聽 // WINDOWS
聽 聽 聽 聽 聽 // Lector del puerto, se quedar谩 esperando a que llegue algo
聽 聽 聽 聽 聽 // al puerto
聽 聽 聽 聽 聽 Comunication lector = new Comunication();
聽 聽 聽 聽 聽 }
聽 聽 聽 聽 }
聽 聽 聽 }
聽 聽 }
聽 }
codigo xharbour a manera de prueba b谩sica... lectura de sarta
#include "FiveWin.ch"
LeerSerial()
function LeerSerial()
聽 聽local oDlg, nComm := InitComm() 聽, nStatus := "ok"
聽 聽alerta("entro")
聽 聽BytesAtPort( nComm, nStatus )
聽 聽CloseComm( nComm )
聽 聽alerta("salio")
return nil
function InitCOMM()
聽 聽local cDcb, nError, nBytes
聽 聽local nComm := OpenComm( "COM4", 1024, 128 )
聽 聽if ! BuildCommDcb( "COM4:9600,n,8,1", @cDcb )
聽 聽 聽 MsgStop( "Error BUILD!" )
聽 聽 聽 return .f.
聽 聽endif
聽 聽#ifdef __CLIPPER__
聽 聽 聽 if ! SetCommState( cDcb )
聽 聽#else
聽 聽 聽 if ! SetCommState( nComm, cDcb )
聽 聽#endif
聽 聽 聽 MsgStop( "Error SETCOMM!" )
聽 聽 聽 return .f.
聽 聽endif
return nComm
function BytesAtPort( nComm, nStatus )
聽 聽local cBuffer := Space(20 ), x := 0, nBytes := 0
// 聽n := ReadComm( nComm, @cBuffer) 聽// <<<<---- program will lock here
聽 聽for x := 1 to 20
聽 聽 聽 聽n := ReadComm( nComm, @cBuffer) 聽// <<<<---- program will lock here
聽 聽 聽 聽if n > 0
聽 聽 聽 聽 聽 exit
聽 聽 聽 聽endif
聽 聽next x
聽 聽Msginfo( AllTrim( cBuffer ))
return nil