RS232

Résolu
didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 - 6 mai 2008 à 21:42
didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 - 7 mai 2008 à 19:45
Bonjour à tous, ..

J'ai un petit mais relativelment embêtant.
En fait c'est que je viens de me rendre compte que mon application recoit pas les données qui sont censées lui parvenir (au travers d'un port RS232). Pour tester mon appli, j'ai créer deux ports virutels aux quels j'ai connecté un émulateur et mon programme. Comme je ne recevais rien, j'ai essayé avec deux emaulateurs (connectés l'un à l'autre). Comme les deux emulateurs recoivents les données, c'est que le problème vient de mon code (du moins, c'est ce que je penses). J'ai ajouté des "System.out.println(" ici ... ");"  un peut partout dans la classe qui gère le RS232 (notemment dans les évènement). Et il ne s'affiche jamais rien .. :( .. comme si le programme ne recevait jamais aucune données ou notifications (d'où l'absence d'évènement ..).

Pour communiqué (enfin .. essayé xD),  j'utilise l'API rxtx

Voici le code que j'utilise pour ouvrir et initialiser la connexion..

public boolean open () throws SerialConnectionException {
  
  //If there is no selected port
  if (tmpPort.equals(""))
   configure();
  
  //If we are using a port, we close it before opening an other
  if (isOpen())
   close();
  
  //Connection's way
  try {
   
   //Trying to get authorization for this application
   CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(tmpPort);
   serialPort = (SerialPort)cpi.open(appName, 1000);
   
   //Opening
   System.out.println("Opening .. ;o) !! =]");
   serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
   serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
   serialPort.addEventListener(new SerialPortEventListener () {
    public void serialEvent (SerialPortEvent spe) {
     System.out.print("event !!");
     switch (spe.getEventType()) {
      case SerialPortEvent.BI :
       System.out.println("enfin !");
       break;
      case SerialPortEvent.OE :
       System.out.println("enfin !");
       break;
      case SerialPortEvent.FE :
       System.out.println("enfin !");
       break;
      case SerialPortEvent.PE :
       System.out.println("enfin !");
       break;
      case SerialPortEvent.CD :
       System.out.println("enfin !");
       break;
      case SerialPortEvent.CTS :
       System.out.println("enfin !");
       break;
      case SerialPortEvent.DSR :
       System.out.println("enfin !");
       break;
      case SerialPortEvent.RI :
       System.out.println("enfin !");
       break;
      case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
       System.out.println("enfin !");
       break;
      case SerialPortEvent.DATA_AVAILABLE :
       System.out.println("reception donnees .. :p ");
       recieveData();
       break;
     }
    }
   });
   
   open = true;
   
  }catch (NoSuchPortException nspe) {
   throw new SerialConnectionException("The asked port does not exists .. ");
  }catch (PortInUseException piue) {
   throw new SerialConnectionException("The asked port is already used by an other program .. ");
  }catch (UnsupportedCommOperationException ucoe) {
   throw new SerialConnectionException("The defined port does not support minimal configuration ..");
  }catch (TooManyListenersException tmle) {
   
  }

  //Is port open ?
  return open;
  
 }

En vous remerciant pr avance de bien vouloir me donner un 'tit coup de pouce :p
Merci ..

1 réponse

didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 2
7 mai 2008 à 19:45
Bonjour
Je suis parvenu à trouvé une solution (sans utiliser les évènements ..) :

   //This is the reading thread (read input data)
   new Thread () {
    
    public void run () {
     
     //We do it allways (while theprogram is running)
     while (true) {
      
      //Where data will be saved
      String temporaryData = "";
      
      try {
       
       //Where data come from
       BufferedReader br = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
       
       //Getting each trame of "dataNulber" bytes
       int dataNumber = 11;
       for (int a=0; a<dataNumber; a++) {
        
        //Sleep while there is no data available
        while (!br.ready());
        
        //Saving data
        temporaryData += " " + br.read();
        
       }
       
      }catch (Exception e) {e.printStackTrace();}
      
      //Validing data ..
      inData = temporaryData;
      
      //To inform that all recieved data are available
      dataListener.dataAvailable(new RS232DataEvent(this));
      
     }
     
    }
    
   }.start();
   

voila.
3
Rejoignez-nous