Ver Mensaje Individual
  #11 (permalink)  
Antiguo 07/12/2014, 14:25
Avatar de warywin
warywin
 
Fecha de Ingreso: octubre-2013
Mensajes: 155
Antigüedad: 10 años, 6 meses
Puntos: 1
Respuesta: Que se necesita para acceder a USB

Bueno probando con la libreria JavaComm y RxTx me da un resultado(queriendo leer lo que transmite el dispositivo) por consola tal que este:
Código Java:
Ver original
  1. portlist... gnu.io.CommPortEnumerator@2a15cd
  2. el puerto identificado es Serial... 1
  3. incapaz de abrir el puerto
  4. el puerto identificado es Serial... 1
  5. el puerto identificado es COM4... COM4
  6. En el constructor SimpleRead()
  7. Serial Port... //./COM4
  8. InputStream... gnu.io.RXTXPort$SerialInputStream@b0bad7
  9. .......................
  10. En la funcion run()
  11. D
  12. D<
El input Stream como podeis comprobar me devuelve una cadena rara con un @ y demas asi como el portlist.
El codigo es el siguiente:
Código Java:
Ver original
  1. public class SimpleRead implements Runnable, SerialPortEventListener{
  2.     static CommPortIdentifier portId;
  3.     static Enumeration portList;
  4.    
  5.     InputStream inputStream;
  6.     SerialPort serialPort;
  7.     Thread readThread;
  8.     byte[] readBuffer;
  9.    
  10.     public static void main(String[] args) {
  11.         portList = CommPortIdentifier.getPortIdentifiers();
  12.         System.out.println("portlist... "+portList);
  13.         while(portList.hasMoreElements()){
  14.             portId = (CommPortIdentifier) portList.nextElement();
  15.             if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  16.                 System.out.println("el puerto identificado es Serial... "+portId.getPortType());
  17.                 if (portId.getName().equals("COM4")) {
  18.                     System.out.println("el puerto identificado es COM4... "+portId.getName());
  19.                     SimpleRead reader = new SimpleRead();
  20.                 }else{
  21.                     System.out.println("incapaz de abrir el puerto");
  22.                 }
  23.             }
  24.         }
  25.     }
  26.    
  27.    public SimpleRead(){
  28.        try {
  29.            System.out.println("En el constructor SimpleRead()");
  30.            serialPort = (SerialPort) portId.open("SimpleReadApp1111",500);
  31.            System.out.println("Serial Port... "+serialPort);
  32.        } catch (PortInUseException e) {
  33.            System.out.println("Excepcion de puerto en uso");
  34.        }
  35.        try {
  36.            inputStream = serialPort.getInputStream();
  37.            System.out.println("InputStream... "+inputStream);
  38.        } catch (IOException e) {
  39.            System.out.println("IO Exception");
  40.        }
  41.        try {
  42.            serialPort.addEventListener(this);
  43.        } catch (TooManyListenersException e) {
  44.            System.out.println("Excepcion de Too many Listeners");
  45.        }
  46.        serialPort.notifyOnDataAvailable(true);
  47.        try {
  48.           serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  49.          
  50.           //no handshaking or other flow control
  51.           serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
  52.          
  53.           //timer on any read of the serial port
  54.           serialPort.enableReceiveTimeout(500);
  55.          
  56.            System.out.println(".......................");
  57.        } catch (UnsupportedCommOperationException e) {
  58.            System.out.println("Excepcion Unsuppoerted comm operation");
  59.        }
  60.        readThread = new Thread(this);
  61.        readThread.start();
  62.    }
  63.    
  64.    public void run(){
  65.        try {
  66.            System.out.println("En la funcion run()");
  67.            Thread.sleep(500);
  68.        } catch (InterruptedException e) {
  69.            System.out.println("Interrupted Exception en metodo run()");
  70.        }
  71.    }
  72.    
  73.    public void serialEvent(SerialPortEvent event){
  74.        
  75.        switch (event.getEventType()){
  76.            case SerialPortEvent.DATA_AVAILABLE:
  77.                readBuffer = new byte[8];
  78.                try {
  79.                    while(inputStream.available()>0){
  80.                        int numBytes = inputStream.read(readBuffer);
  81.                    }
  82.                    System.out.println(new String(readBuffer));
  83.                    
  84.                } catch (IOException e) {
  85.                    System.out.println("IO Exception en SerialEvent()");
  86.                }
  87.                break;
  88.        }
  89.    }
  90. }

En consola la funcion run() dejandola ejecutada devuelve un par de caracteres extraños como una "D" quizá un simbolo "<" y un cuadrado, nose muy bien que significa eso. Aunque si en la linea
Código Java:
Ver original
  1. System.out.println(new String(readBuffer));
le quito el new String recoge algo como "[B@b1cc87"...es como que no sabe como recibir esos datos, creo que deberia recoger una cadena muy larga binaria.. a ver si me podeis ayudar que esta pasando y porq el inputstream devuelve eso
saludos!

Última edición por warywin; 08/12/2014 a las 09:19