//*******Applet Java réalisée en 2005 pour le centre ERASME par Guillaume BROBECKER********// // Importation des librairies //********************************************************************** import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.JTextField; import javax.swing.JButton; import javax.comm.*; import com.sun.comm.Win32Driver; import java.io.*; import java.util.*; import javax.swing.JLabel; import javax.swing.*; // Déclaration de la classe publique Appplet1 qui hérite de la classe Applet et qui implémente la class SerialportEventLitener public class Applet1 extends Applet implements SerialPortEventListener { // Définition des variables private CommPortIdentifier portId; private SerialPort serialPort; private BufferedReader fluxLecture; private String valeurLue=""; // Initialisation du contexte graphique BorderLayout borderLayout1 = new BorderLayout(); JTextField TextField1 = new JTextField(); JLabel jLabel1 = new JLabel(); // getValeurLue retourne la valeur de l'identifiant détecté par le lecteur public String getValeurlue(){ return (valeurLue); } //*******************FIN DE LA FCT getValeurlue()************************ // Méthode de gestion des événements public void serialEvent(SerialPortEvent event) { // Géstion des événements sur le port : // On ne fait rien sauf quand les données sont disponibles 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 : try { // Lecture du buffer et affichage fluxLecture.read(); valeurLue = new String(fluxLecture.readLine().toString()); fluxLecture.read(); TextField1.setText(""+valeurLue); System.out.println(valeurLue); } catch (IOException e) { } break; } } //******************FIN DE LA FCT SerialEvent ()********************* // Initialisation l'applet public void init() { try { // On appel la fonction jbInit jbInit(); // On bloc le composant textfield pour qu'il soit inéditable TextField1.setEditable(false); } catch (Exception e) { e.printStackTrace(); } } //***********************FIN DE LA FCT init()************************** // Fonction d'initialisation private void jbInit() throws Exception { TextField1.setPreferredSize(new Dimension(110, 20)); TextField1.setSelectionStart(12); jLabel1.setDisplayedMnemonic('0'); jLabel1.setHorizontalAlignment(SwingConstants.LEFT); jLabel1.setHorizontalTextPosition(SwingConstants.LEFT); jLabel1.setText("Identifiant :"); jLabel1.setVerticalAlignment(SwingConstants.BOTTOM); jLabel1.setVerticalTextPosition(SwingConstants.BOTTOM); this.add(jLabel1); this.add(TextField1); } //*******************FIN DE LA FCT jbInit()**************************** // Démarrage de l'applet sur le port COM 4 public void start() { try { String port = "COM4"; // Initialisation du driver Win32Driver w32Driver = new Win32Driver(); w32Driver.initialize(); // Récupération de l'identifiant du port try { portId = CommPortIdentifier.getPortIdentifier(port); } catch (NoSuchPortException e) { } // Ouverture du port try { serialPort = (SerialPort) portId.open("ModeEvenement", 2000); } catch (PortInUseException e) { } // Récupération du flux try { fluxLecture = new BufferedReader(new InputStreamReader(serialPort.getInputStream())); } catch (IOException e) { } // Ajout du listener try { serialPort.addEventListener(this); } catch (TooManyListenersException e) { } // Paramétrage du port serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams( 9600, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN); } catch (UnsupportedCommOperationException e) { } //On recupere les exception de la fct start() } catch (Exception e) { e.printStackTrace(); } } //************************Fin de la fonction START********************** // Arrêter l'applet public void stop() { try { fluxLecture.close(); } catch (IOException e) { } serialPort.close(); super.stop(); } //*************************FIN DE LA FCT STOP()********************** } //**************************FIN DU PROGRAMME***************************