2017-01-24 1 views
0

J'ai déclaré un objet twoWaySerialComm pour configurer la connexion de port. Dans la classe twoWaySerialComm, j'ai créé un thread pour lire les données série entrantes dans une classe appelée 'SerialReader'. Comment accéder aux données série du thread principal 'twoWaySerialComm'?Communication série bidirectionnelle - Impossible d'accéder à la chaîne lue depuis le lecteur de série

J'avais lu les données série entrantes dans la chaîne 'hex' dans la classe 'SerialReader' dans une boucle while. Je suis capable d'imprimer les données entrantes dans la boucle while, mais je suis incapable d'accéder à cette chaîne 'hex' dans la classe 'twoWaySerialComm'. Pourquoi ne puis-je pas accéder à la chaîne 'hex' en dehors de la boucle while? Comment cela peut-il être fait?

import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.NoSuchPortException; 
import gnu.io.PortInUseException; 
import gnu.io.SerialPort; 
import gnu.io.UnsupportedCommOperationException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author asanka 
*/ 
public class TwoWaySerialComm { 

    private static TwoWaySerialComm twoWaySerialComm; 

    String read; 

    private TwoWaySerialComm() throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException { 
     String portName = "/dev/ttyUSB0"; 
     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(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 

       InputStream in = serialPort.getInputStream(); 
       OutputStream out = serialPort.getOutputStream(); 

       SerialReader reader; 
       (new Thread(reader = new SerialReader(in))).start(); 
       read = reader.readHex(); 

      } else { 
       System.out.println("Error: Only serial ports are handled by this example."); 
      } 
     } 
    } 

    public static TwoWaySerialComm getTwoWaySerialComm() throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException { 
     if (twoWaySerialComm == null) { 
      twoWaySerialComm = new TwoWaySerialComm(); 
     } 
     return twoWaySerialComm; 
    } 

    public String data() { 
     return read; 
    } 

    /** 
    * 
    */ 
    public static class SerialReader implements Runnable { 

     String hex; 
     InputStream in; 

     public SerialReader(InputStream in) { 
      this.in = in; 
     } 

     public void run() { 
      byte[] buffer = new byte[1024]; 

      int len; 
      try { 
       while ((len = this.in.read(buffer)) > -1) { 

        hex = new String(buffer, 0, len).replaceAll("\\s", ""); 
        Thread.sleep(1000); 
        System.out.println(hex); 
       } 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (InterruptedException ex) { 
       Logger.getLogger(TwoWaySerialComm.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 

     public String readHex() { 
      return hex; 
     } 
    } 

    /** 
    * 
    */ 
    public static class SerialWriter implements Runnable { 

     OutputStream out; 

     public SerialWriter(OutputStream out) { 
      this.out = out; 
     } 

     public void run() { 
      try { 
       int c = 0; 
       while ((c = System.in.read()) > -1) { 
        this.out.write(c); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

} 

Répondre

0

Pour autant que je puisse voir, vous exécutez l'opération de lecture dans un autre thread. Lorsque vous appelez la méthode readHex(), ce thread n'a probablement pas encore rempli le champ hexadécimal.

Vous devez d'abord appeler join() sur le nouveau thread pour vous assurer qu'il est terminé. Ou utilisez le CompletableFuture du package java.util.concurrent.