IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Utilisation des ports COM en Java avec RXTX


Sujet :

Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 100
    Points : 48
    Points
    48
    Par défaut Utilisation des ports COM en Java avec RXTX
    Bonjour.
    Pour mon projet BTS je doit faire un programme en java qui récupère des infos venant d'un lecteur de carte branché en RS232 et qui va lire dans une base de donnée MSSQL si c'est info sont juste.
    Pour utiliser les ports RS232 j'utilise RXTXcomm sur eclipse sur windows mais j'ai du mal a l'utiliser avez vous des exemples de code pour l'utilisation de ce package? Merci

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Février 2009
    Messages
    43
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 43
    Points : 49
    Points
    49
    Par défaut Apparemment
    Salut,

    Apparemment mon message perso d'hier n'est pas arrivé.
    Voilà un exemple sur le port "COM4" et une fréquence passée en paramètre (19200 par exemple).
    Le programme lis le texte qui arrive sur le port et le réécrit dans un fichier.

    Tiens-moi au courant si tu as des problèmes avec ce code.
    PS : n'oublies pas les 2 dll si tu bosses avec windows.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
     
    import gnu.io.CommPort;
    import gnu.io.CommPortIdentifier;
    import gnu.io.SerialPort;
     
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
     
    public class COMListener {
     
    	private int rate=19200;
     
    	public COMListener() {
    		super();
    	}
     
    	void connect(String portName) throws Exception {
    		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(57600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    				//serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    				serialPort.setSerialPortParams(rate, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
     
     
    				InputStream in = serialPort.getInputStream();
    				OutputStream out = serialPort.getOutputStream();
     
    				(new Thread(new SerialReader(in))).start();
    				(new Thread(new SerialWriter(out))).start();
     
    			} else {
    				System.out
    						.println("Error: Only serial ports are handled by this example.");
    			}
    		}
    	}
     
    	/** */
    	public static class SerialReader implements Runnable {
    		InputStream in;
     
    		public SerialReader(InputStream in) {
    			this.in = in;
    		}
     
    		public void run() {
    			PrintWriter out = null;
    			byte[] buffer = new byte[8];
    			int len = -1;
    			try {
    				out = new PrintWriter(new BufferedWriter(new FileWriter(
    						"C:\\eck4.txt",true)));
     
    				while ((len = this.in.read(buffer)) > -1) {
    					String next=new String(buffer,0,len);
    					System.out.print(next);
    					out.print(next);
    					out.flush();
    				}
    			} catch (IOException e) {
    				if (out != null)
    					out.close();
    				e.printStackTrace();
    			}
    		}
    	}
     
    	/** */
    	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();
    			}
    		}
    	}
     
    	public static void main(String[] args) {
    		String rate=args[0];
     
    		COMListener l=new COMListener();
    		if (rate!=null && !rate.trim().equals("")){
    			try{
    				l.rate=Integer.parseInt(rate);
    			}
    			catch (NumberFormatException nfe){
    				System.out.println("BAUDRATE n'est pas un nombre");
    			}
    		}
     
     
    		try {
    			l.connect("COM4");
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 100
    Points : 48
    Points
    48
    Par défaut
    salut non j'ai pas eu ton mess hier merci pour ta reponse je vais essayer sa!!
    merci

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 100
    Points : 48
    Points
    48
    Par défaut
    j'ai bien mis les dll mais quand j'essaye le code que l'importation gnu.io.Commport gnu.io.commportidentifier et gnu.io.serialport ne peut pas être résolue et je trouve pas pourquoi

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Février 2009
    Messages
    43
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 43
    Points : 49
    Points
    49
    Par défaut classpath
    C'est probablement le RXTXComm.jar qui n'est pas dans ton classpath.

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 100
    Points : 48
    Points
    48
    Par défaut
    comment faire pour le mettre dans le CLASSPATH??

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 100
    Points : 48
    Points
    48
    Par défaut
    c'est bon j'ai fait une variable d'environnement CLASSPATH et j'ai mis le chemin de mon RXTXcomm.jar mais que mis celui la faut-il que je mette un autre chemin en plus??

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Février 2009
    Messages
    43
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 43
    Points : 49
    Points
    49
    Par défaut Oui non ca dépends
    Salut,

    Je te conseille pas de modifier ta variable d'environnement Classpath juste pour ça, à moins que tu me précise un peu le contexte.

    Pour compiler un :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    javac -classpath .;RXTXComm.jar Maclass.java
    devrait être plus adapté....ou passe par un IDE genre eclipse pour éviter les manips.

    A+

  9. #9
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 100
    Points : 48
    Points
    48
    Par défaut
    merci ouai c'est bon j'arrive a l'utiliser aurais tu par has

  10. #10
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 100
    Points : 48
    Points
    48
    Par défaut
    merci ouai c'est bon j'arrive a l'utiliser aurais tu par hasard une doc avec toute les fonction de rxtx stp?

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Février 2009
    Messages
    43
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 43
    Points : 49
    Points
    49
    Par défaut Doc
    Salut,

    Si mes souvenirs sont bons à partir du wiki http://rxtx.qbang.org/wiki/index.php/Main_Page tu dois pouvoir trouver ton bonheur mais là comme ça j'ai rien.

    Bon courage.
    N'oublie pas de passer le post en résolu CIAO

  12. #12
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2010
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2010
    Messages : 3
    Points : 4
    Points
    4
    Par défaut Imcompréhension totale
    Bonjour,
    Voici un morceau de code que j'ai du mal à comprendre Pourriez vous me le renvoyer avec des commentaires dessus pour que je puisse le comprendre pour pouvoir travailler dessus après ??? Vous me sauveriez la vie !!

  13. #13
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2010
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2010
    Messages : 3
    Points : 4
    Points
    4
    Par défaut
    Voici le code :

    import gnu.io.CommPort;
    import gnu.io.CommPortIdentifier;
    import gnu.io.SerialPort;

    import java.io.FileDescriptor;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import gnu.io.RXTXCommDriver;

    public class projettut
    {
    public projettut()
    {
    super();
    }
    //connexion au port:

    void connect ( String portName ) throws Exception
    {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    //générer une liste des ports disponibles. Elle choisit ensuite un port dans la liste et appelle
    // CommPortIdentifier.open pour créer un objet CommPort qui est casté ensuite en SerialPort.

    if ( portIdentifier.isCurrentlyOwned() )
    // si le port est deja connecté
    {
    System.out.println("Error: Port is currently in use");
    }
    else
    {
    CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

    if ( commPort instanceof SerialPort )
    {
    //si le port est présent mais pas connecté
    SerialPort serialPort = (SerialPort) commPort;
    serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

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

    (new Thread(new SerialReader(in))).start();
    (new Thread(new SerialWriter(out))).start();

    }
    else
    // si le port n'est pas présent
    {
    System.out.println("Error: Only serial ports are handled by this example.");
    }
    }
    }

    /** */
    public static class SerialReader implements Runnable
    {
    InputStream in;

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

    public void run ()
    {
    byte[] buffer = new byte[1024];
    int len = -1;
    try
    {
    while ( ( len = this.in.read(buffer)) > -1 )
    {
    System.out.print(new String(buffer,0,len));//tampon
    }
    }
    catch ( IOException e )
    {
    e.printStackTrace();
    }
    }
    }

    /** */
    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();
    }
    }
    }

    public static void main ( String[] args )
    {
    try
    {
    (new projettut()).connect("COM3");
    }
    catch ( Exception e )
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

  14. #14
    Membre régulier Avatar de Sylvain__A_
    Homme Profil pro
    Développeur Java
    Inscrit en
    Octobre 2008
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Octobre 2008
    Messages : 100
    Points : 94
    Points
    94
    Par défaut
    Bonjour,

    Tu aurais du mettre le code entre des balises "codes", pour qu'on ai la couleur ...

    Sur cette discussion, j'ai posé pas mal de lien qui pourrait t'aider :

    http://www.developpez.net/forums/d87...nuseexception/

    Le tutos de developpez, même s'il utilise l'api java.comm qui est désuète, n'en reste pas moins à jour car rxtx est très proche.

    http://christophej.developpez.com/tu...java/javacomm/

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 14/11/2014, 12h16
  2. Réponses: 2
    Dernier message: 16/07/2014, 12h09
  3. Mise à jour de la liste des ports COM détectées
    Par chourmo dans le forum Composants VCL
    Réponses: 2
    Dernier message: 23/12/2005, 16h11
  4. Utilisation du port COM
    Par chourmo dans le forum Composants VCL
    Réponses: 10
    Dernier message: 13/06/2005, 12h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo