Pb port série & inputstream

Résolu
cs_kaliel Messages postés 96 Date d'inscription mardi 6 janvier 2004 Statut Membre Dernière intervention 24 octobre 2005 - 21 juin 2004 à 10:44
al_b07 Messages postés 24 Date d'inscription lundi 27 septembre 2004 Statut Membre Dernière intervention 4 octobre 2004 - 27 sept. 2004 à 10:38
voila j'ai un problème avec le port série, il y a une erreur:
"java.lang.NullPointerException" à la ligne
"inputStream =serialPort.getInputStream();"

du code suivant:

import java.io.*;
import java.util.*;
import javax.comm.*;
import javax.swing.JList;

public class SerialRead implements Runnable, SerialPortEventListener{
  static CommPortIdentifier portID;
  static Enumeration portList;
  InputStream inputStream;
  SerialPort serialPort;
  Thread readThread;
  byte[] readBuffer= new byte[40];
  int numBytes;
  String ComUsed;

public String getMess(){
    return readBuffer.toString();
  }
public boolean connect(String COM_Name){

    boolean portfound=false;
    portList = CommPortIdentifier.getPortIdentifiers();
    while(portList.hasMoreElements())
    {
      portID = (CommPortIdentifier)portList.nextElement();
      if(portID.getPortType()==CommPortIdentifier.PORT_SERIAL)
      {
        if(portID.getName().equals(COM_Name)) portfound=true;

      }
    }

    return portfound;
  }
  public SerialRead(String identity) {
    if (connect(identity))
    {
      ComUsed=identity;
      
    try{
      serialPort =(SerialPort) portID.open("TestCom",1000);
    }
    catch(PortInUseException e){};
    try{

      inputStream =serialPort.getInputStream();
    }
    catch(IOException e){};
    try{
      serialPort.addEventListener(this);
    }
    catch(TooManyListenersException e){};
    serialPort.notifyOnDataAvailable(true);

      try {
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                                       SerialPort.STOPBITS_1,
                                       SerialPort.PARITY_NONE);
      }
      catch (UnsupportedCommOperationException ex) {
      }
    readThread =new Thread(this);
    readThread.start();
    }
  }

  public void run() {
    try {
      Thread.sleep(20000);
    }
    catch (InterruptedException ex) {
    }
  }

  public void serialEvent(SerialPortEvent serialPortEvent) {
    if (serialPortEvent.getEventType()==SerialPortEvent.DATA_AVAILABLE){

      try {
        while (inputStream.available() > 0) {
         numBytes = inputStream.read(readBuffer);
        }
      }
      catch (IOException ex) {
      }

    }

  }
}


Alors ça veut dire quoi? parce qu'il voit bien le port choisi mais marche pas quand même!!!

merci.

4 réponses

al_b07 Messages postés 24 Date d'inscription lundi 27 septembre 2004 Statut Membre Dernière intervention 4 octobre 2004
27 sept. 2004 à 10:38
Essaie d'utiliser DataInputStream ou autres classes I/O plutôt que
InputStream.
inputSream=new DataInputStream(commPort.getInputStream());

Ensuite ton problème vient que inputStream tu l'initialises dans un try...catch..., du coup il se peut qu'il se soit jamais initialiser, donc à la ligne de déclaration:
"InsputStream inputStream" met:
InsputStream inputStream=new InputStream();

Et n'oublie pas, le java ça déchire...et vive l'API
3
wargre Messages postés 649 Date d'inscription mardi 8 juin 2004 Statut Membre Dernière intervention 9 septembre 2004 7
21 juin 2004 à 11:14
ca veut dire que serialPort est null. donc que portId.open merde
0
cs_kaliel Messages postés 96 Date d'inscription mardi 6 janvier 2004 Statut Membre Dernière intervention 24 octobre 2005 4
21 juin 2004 à 11:48
et ça peut venir de quoi parce que il d"tecte bien le port alors bon?!
0
cs_kaliel Messages postés 96 Date d'inscription mardi 6 janvier 2004 Statut Membre Dernière intervention 24 octobre 2005 4
21 juin 2004 à 11:52
sinon en fait j'essaie de faire un chat par liaison série pour comprendre comment utiliser la java comm api!!!
0
Rejoignez-nous