Advertisement
Guest User

GPS http://darylrobotproject.wordpress.com/

a guest
Oct 1st, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. package com.daryl.gps;
  2.  
  3. import gnu.io.CommPortIdentifier;
  4. import gnu.io.NoSuchPortException;
  5. import gnu.io.PortInUseException;
  6. import gnu.io.SerialPort;
  7. import gnu.io.SerialPortEvent;
  8. import gnu.io.SerialPortEventListener;
  9. import gnu.io.UnsupportedCommOperationException;
  10. import java.util.*;
  11. import java.io.*;
  12.  
  13. import com.daryl.utils.Nmea;
  14.  
  15. public class Gps implements SerialPortEventListener {
  16.  
  17.     private CommPortIdentifier portID = null; //Identifiant du port
  18.     private SerialPort serialPort; //Le port série
  19.     private BufferedReader fluxLecture; //Flux de lecture du port
  20.  
  21.     /*
  22.      * Méthode qui initialise le port série
  23.      */
  24.     public void ModeEvenement(String portCOM) {
  25.        
  26.         //Récupération de l'identifiant du port
  27.         try {
  28.             portID = CommPortIdentifier.getPortIdentifier(portCOM);
  29.         } catch (NoSuchPortException e) {System.out.println("NoSuchPortException: " + e.toString());}
  30.  
  31.         //Ouverture du port
  32.         try {
  33.             serialPort = (SerialPort) portID.open("ModeEvenement", 2000);
  34.         } catch (PortInUseException e) {System.out.println("PortInUseException: " + e.toString());}
  35.  
  36.         //Récupération du flux
  37.         try {
  38.             fluxLecture = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
  39.         } catch (IOException e) {System.out.println("IOException: " + e.toString());}
  40.  
  41.         //Ajout du listener
  42.         try {
  43.             serialPort.addEventListener(this);
  44.         } catch (TooManyListenersException e) {System.out.println("TooManyListenersException: " + e.toString());}
  45.        
  46.         //Paramétrage du port
  47.         serialPort.notifyOnDataAvailable(true);
  48.        
  49.         try {
  50.             serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
  51.         } catch (UnsupportedCommOperationException e) {}
  52.        
  53.         System.out.println("Port GPS " + portID.getName() + " ouvert, en attente de lecture..." );
  54.     }
  55.  
  56.     /*
  57.      * Méthode qui effectue la lecture de la trame sur le port COM
  58.      */
  59.     public void ReadSerialPort(){
  60.        
  61.         String reponse = new String();
  62.  
  63.         try {
  64.            
  65.             //On lit la trame
  66.             reponse = (String) fluxLecture.readLine();
  67.             String liste[] = reponse.split(",");
  68.            
  69.             if (liste[0].equals("$GPGGA")){
  70.                
  71.                 // Si pas de fix GPS
  72.                 if(liste[6].equals("0")) {
  73.                    
  74.                     System.out.println("Pas de Fix Satellite...");
  75.                    
  76.                 } else { // Sinon on affiche les coordonnées
  77.                    
  78.                     Nmea coordonne = new Nmea();
  79.                     coordonne.parse(reponse);
  80.                     //System.out.println(coordonne.parse(reponse));
  81.                     System.out.println("Latitude: "+ liste[2]+" "+liste[3]+", Longitude: "+ liste[4]+" "+liste[5]);
  82.                    
  83.                 }  
  84.                
  85.             }
  86.            
  87.         } catch (IOException e) {
  88.            
  89.             System.out.println("NMEA Error");
  90.             //e.printStackTrace();
  91.            
  92.         }
  93.        
  94.         //fluxLecture.close();
  95.         //serialPort.close();
  96.     }
  97.  
  98.     public void serialEvent(SerialPortEvent event) {
  99.        
  100.         switch (event.getEventType()) {
  101.        
  102.         case SerialPortEvent.DATA_AVAILABLE :
  103.             this.ReadSerialPort();
  104.             break;
  105.         default:
  106.             break;
  107.         }
  108.     }
  109.  
  110.     /*
  111.      * Methode qui scanne tous les ports COM et test si des données viennent du port COm scanné
  112.      */
  113.     @SuppressWarnings("rawtypes")
  114.     public void listPort(){
  115.        
  116.         Enumeration listePorts = CommPortIdentifier.getPortIdentifiers();
  117.  
  118.         while (listePorts.hasMoreElements()){
  119.            
  120.             portID = (CommPortIdentifier) (CommPortIdentifier) listePorts.nextElement();
  121.             if(portID.getPortType()==CommPortIdentifier.PORT_SERIAL){
  122.                
  123.                 System.out.println("Nom du PORT COM :"+portID.getName());
  124.                 System.out.println("Type de PORT COM :"+portID.getPortType());
  125.  
  126.                 if (portID.getName().equals("COM1")) {
  127.                     this.ModeEvenement(portID.getName());
  128.                 }
  129.  
  130.             }
  131.         }
  132.     }
  133.  
  134.     public static void main(String[] args) {
  135.  
  136.         Gps test = new Gps();
  137.         test.listPort();
  138.  
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement