Lien entre deux classe

pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012 - 17 avril 2010 à 21:27
tlaloctzin Messages postés 136 Date d'inscription mercredi 21 juin 2006 Statut Membre Dernière intervention 12 septembre 2013 - 19 avril 2010 à 18:50
salut,
j'ai une application en java (netbeans).j'ai bien réussir a faire une class identifiant.java qui permet d'envoyer et lire des données du port RS232.
et j'ai une class choix.java qui contient un bouton "donnee".
mon but est en cliquant sur ce bouton la class indentifiant.java s'ouvre.
la classe indentifiant.java récupère le code d'identifiant venant de port rs232 pour l'utiliser après dans mon application.
j'ai essayé avec setVisible mais aucune effet , j'ai même consulter un peu de recherche et j'ai fais un constructeur mais il ma bloque mon application.
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
//identifiant i= new identifiant();
//i.main(new String[1]);
//i.run();
} 

package projet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.*;
import com.sun.comm.Win32Driver;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
public class identifiant implements Runnable, SerialPortEventListener {
public static CommPortIdentifier portId;
public static CommPortIdentifier saveportId;
public static Enumeration portList;
public static InputStream inputStream;
public static SerialPort serialPort;
public static Thread readThread;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
public static byte[] readBuffer = new byte[20];
int i = 0;
public static String login;
static boolean condition = false;
public static String defaultPort = "COM1";;
public static void main(String[] args) {
boolean portFound = false;

Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();

//déterminer le nom du port série sur plusieurs systèmes d'exploitation
String osname = System.getProperty("os.name", "").toLowerCase();
if (osname.startsWith("windows")) {
// windows
defaultPort = "COM1";

} else {
System.out.println("Sorry, your operating system is not supported");
return;
}

if (args.length > 0) {
defaultPort = args[0];
}
System.out.println("Set default port to " + defaultPort);
//ports analyser et si le port par défaut est trouvé, initialisé le lecteur
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port: " + defaultPort);
portFound = true;
// init reader thread
identifiant reader = new identifiant();
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}

}

public static void initwritetoport() {
//initwritetoport () suppose que le port a déjà été ouvert et
//initialisé par "nulltest publique ()"
try {
// obtenir le flux sortant
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
}
try {
// activer le notifiant OUTPUT_BUFFER_EMPTY
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}

}

public static void writetoport(String s) {
System.out.println("Writing "" + s + "" to " + serialPort.getName());
try {
// Donnez votre chaîne sur le port série
outputStream.write(s.getBytes());
} catch (IOException e) {
}
}

public void readfromport() {
// nous obtenons ici si les données ont été reçues
try {
i++;
// données lues
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
// données d'impression

String result = new String(readBuffer).trim();
System.out.println("Read: " + i + " : " + result);

if (result.equals("*")) {
login = new String();
//System.out.println ( "ma chaine : vide ");
condition = true;
} else if (result.equals("#")) {
System.out.println("ma chaine :" + login);
condition = false;
} else if (condition == true) {
login = login + result;// gérer les chaine de caractére
System.out.println("mon result " + login);

}
} catch (IOException e) {
}
//Debut.login=login;
}

public identifiant() {
}

public void identifiant() {
// initalize port série

try {
serialPort = (SerialPort) portId.open("identifiant", 2000);
} catch (PortInUseException e) {
}

try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
}

try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
}

// activate the DATA_AVAILABLE notifier
serialPort.notifyOnDataAvailable(true);

try {
// définir les paramètres de port
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}

// commencer à lire le fil
readThread = new Thread(this);
readThread.start();

}

public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
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;
case SerialPortEvent.DATA_AVAILABLE:

readfromport();
break;
}
}

public void run() {

// première chose que dans le fil, on initialise l'opération d'écriture
initwritetoport();
// écrit une chaîne dans le port, le serialEvent vais le lire
writetoport("d");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(identifiant.class.getName()).log(Level.SEVERE, null, ex);
}
}
}


après run sur le console :on va récupère la trame *az#
Writing d to port COM1
Read: i : *
Read: i : a
Read: i : z
Read: i : #
...
mon result az

comment je le faire ?!!
merci d'avance

15 réponses

tlaloctzin Messages postés 136 Date d'inscription mercredi 21 juin 2006 Statut Membre Dernière intervention 12 septembre 2013 3
18 avril 2010 à 15:21
Salut , je ne comprends pas bien ce que tu veux ?
1) Tu veux "ouvrir la classe" en utilisant setVisible() ?
Ca ca ne fonctionne qu'avec les JFrame , dans une classe qui extends JFame.
2)
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
//identifiant i= new identifiant();
//i.main(new String[1]);
//i.run();
} 

Si tu laisse les instructions marqués comme des commentaires , c est normal que rien ne se passe

3) Peux tu etre plus precis sur ta demande stp.


Il vaut mieu être riche et en bonne santé que pauvre et malade .
0
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012
18 avril 2010 à 15:30
salut ,

je sais j'ai les marque comme ca.
si j'enleve les "//" ils ne marche pas .


ce que je veux faire est :
lancer la class identifiant.java ( communication avec rs 232) en cliquant sur un bouton ( ce bouton se trouve dans une class Frame choix.java)
0
tlaloctzin Messages postés 136 Date d'inscription mercredi 21 juin 2006 Statut Membre Dernière intervention 12 septembre 2013 3
18 avril 2010 à 15:38
1)
if (args.length > 0) {
defaultPort = args[0];
}
//*************************
i.main(new String[1]);

Tu n'alloues aucune valeur dans le tableau , donc ton port est null , enleves deja cette ligne.
La method main est apellée toute seule lorsque tu instancie ta classe.
Est ce que tu as un message d'erreur qq part ?


Il vaut mieu être riche et en bonne santé que pauvre et malade .
0
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012
18 avril 2010 à 15:43
salut,

dans le class identifiant.java non aucune message d'erreur , tous marche bien quand je la run seul
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012
18 avril 2010 à 15:49
quand je clique sur bouton ces messages s'affichent:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at projet.identifiant.initwritetoport(identifiant.java:69)
        at projet.identifiant.run(identifiant.java:182)
        at projet.Choix.jButton1MouseClicked(Choix.java:81)
        at projet.Choix.access$000(Choix.java:18)
        at projet.Choix$1.mouseClicked(Choix.java:45)
        at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:212)
        at java.awt.Component.processMouseEvent(Component.java:5520)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
0
tlaloctzin Messages postés 136 Date d'inscription mercredi 21 juin 2006 Statut Membre Dernière intervention 12 septembre 2013 3
18 avril 2010 à 15:49
Bin la faut que tu run tout et que tu copie/colles le rapport de la console ,
comme tu as l'air de travailler sur netBeans ( vu : private void jButton1MouseClicked(java.awt.event.MouseEvent evt))
tu copies le rapport de la zonne de texte en bas.

Il vaut mieu être riche et en bonne santé que pauvre et malade .
0
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012
18 avril 2010 à 15:52
ok

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at projet.identifiant.initwritetoport(identifiant.java:69)
        at projet.identifiant.run(identifiant.java:182)
        at projet.Choix.jButton1MouseClicked(Choix.java:81)
        at projet.Choix.access$000(Choix.java:18)
        at projet.Choix$1.mouseClicked(Choix.java:45)
        at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:212)
        at java.awt.Component.processMouseEvent(Component.java:5520)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
        at java.awt.Component.processEvent(Component.java:5282)
        at java.awt.Container.processEvent(Container.java:1966)
        at java.awt.Component.dispatchEventImpl(Component.java:3984)
        at java.awt.Container.dispatchEventImpl(Container.java:2024)
        at java.awt.Component.dispatchEvent(Component.java:3819)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3901)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
        at java.awt.Container.dispatchEventImpl(Container.java:2010)
        at java.awt.Window.dispatchEventImpl(Window.java:1791)
        at java.awt.Component.dispatchEvent(Component.java:3819)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
BUILD SUCCESSFUL (total time: 1 minute 4 seconds)
0
tlaloctzin Messages postés 136 Date d'inscription mercredi 21 juin 2006 Statut Membre Dernière intervention 12 septembre 2013 3
18 avril 2010 à 15:55
Tu as essayé en enlevant la ligne
i.main(new String[1]);

??
Pck a priori ton probleme viens du fait que tu n'arrives a recuperer le flux de sortie pour la connection au port.
Il vaut mieu être riche et en bonne santé que pauvre et malade .
0
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012
18 avril 2010 à 15:58
oui
identifiant i=   new identifiant();
     //i.main(new String[1]);
     i.run();

//if (args.length > 0) {
            //defaultPort = args[0];
        //}
0
tlaloctzin Messages postés 136 Date d'inscription mercredi 21 juin 2006 Statut Membre Dernière intervention 12 septembre 2013 3
18 avril 2010 à 16:27
Faudrait aussi que tu mettes des printStackTrace() sur tes exceptions , pck la ou ton code plante y en a pas , ca nous eclairerais surement

Il vaut mieu être riche et en bonne santé que pauvre et malade .
0
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012
19 avril 2010 à 11:12
salut

j'ai tester ce matin avec le cable rs232 aussi juste le class identifiant marche bien mais quand je le met avec mon application , rien ne marche !!!
je fais ce code pour appeler la class identifiant :

identifiant i = new identifiant ();
i.main( new String [1];
i.run();


les messages d'erreur sont:

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at projet.identifiant.(identifiant.java:126)
        at projet.Choix.jButton1MouseClicked(Choix.java:69)
        at projet.Choix.access$000(Choix.java:18)
        at projet.Choix$1.mouseClicked(Choix.java:42)
        at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:212)
        at java.awt.Component.processMouseEvent(Component.java:5520)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
        at java.awt.Component.processEvent(Component.java:5282)
        at java.awt.Container.processEvent(Container.java:1966)
        at java.awt.Component.dispatchEventImpl(Component.java:3984)
        at java.awt.Container.dispatchEventImpl(Container.java:2024)
        at java.awt.Component.dispatchEvent(Component.java:3819)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3901)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
        at java.awt.Container.dispatchEventImpl(Container.java:2010)
        at java.awt.Window.dispatchEventImpl(Window.java:1791)
        at java.awt.Component.dispatchEvent(Component.java:3819)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
0
tlaloctzin Messages postés 136 Date d'inscription mercredi 21 juin 2006 Statut Membre Dernière intervention 12 septembre 2013 3
19 avril 2010 à 12:29
i.main( new String [1]);

Déja ,comme je t'ai dis hier , cette instruction n'a aucun interet si tu n'as pas d'argument a fournir ( un port précis ).
Ensuite elle NE PEUT PAS FONCTIONNER vu que tu alloues a l'adresse de ton port une valeur qui est null.

Ca pourrais fonctionner si tu mettais
String [] s = new String[1];
s[0] = "COM1";
identifiant i=   new identifiant(s);

par exemple.
Réessayes comme ca et donnes le message d'erreur :
identifiant i=   new identifiant();
i.start();


Il vaut mieu être riche et en bonne santé que pauvre et malade .
0
tlaloctzin Messages postés 136 Date d'inscription mercredi 21 juin 2006 Statut Membre Dernière intervention 12 septembre 2013 3
19 avril 2010 à 12:30
Erreur
c est
identifiant i=   new identifiant();
i.run();


Il vaut mieu être riche et en bonne santé que pauvre et malade .
0
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012
19 avril 2010 à 13:50
salut

même erreur
si t as un cable rs232? si tu veux je t envoyé les deux classes identifiant.java et choix.java



merci d'avance
0
tlaloctzin Messages postés 136 Date d'inscription mercredi 21 juin 2006 Statut Membre Dernière intervention 12 septembre 2013 3
19 avril 2010 à 18:50
Non j en ai pas mais envoie moi les clases quand mm stp tlaloctzin@hotmail.com

Il vaut mieu être riche et en bonne santé que pauvre et malade .
0
Rejoignez-nous