Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.daryl.gps;
- import gnu.io.CommPortIdentifier;
- import gnu.io.NoSuchPortException;
- import gnu.io.PortInUseException;
- import gnu.io.SerialPort;
- import gnu.io.SerialPortEvent;
- import gnu.io.SerialPortEventListener;
- import gnu.io.UnsupportedCommOperationException;
- import java.util.*;
- import java.io.*;
- import com.daryl.utils.Nmea;
- public class Gps implements SerialPortEventListener {
- private CommPortIdentifier portID = null; //Identifiant du port
- private SerialPort serialPort; //Le port série
- private BufferedReader fluxLecture; //Flux de lecture du port
- /*
- * Méthode qui initialise le port série
- */
- public void ModeEvenement(String portCOM) {
- //Récupération de l'identifiant du port
- try {
- portID = CommPortIdentifier.getPortIdentifier(portCOM);
- } catch (NoSuchPortException e) {System.out.println("NoSuchPortException: " + e.toString());}
- //Ouverture du port
- try {
- serialPort = (SerialPort) portID.open("ModeEvenement", 2000);
- } catch (PortInUseException e) {System.out.println("PortInUseException: " + e.toString());}
- //Récupération du flux
- try {
- fluxLecture = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
- } catch (IOException e) {System.out.println("IOException: " + e.toString());}
- //Ajout du listener
- try {
- serialPort.addEventListener(this);
- } catch (TooManyListenersException e) {System.out.println("TooManyListenersException: " + e.toString());}
- //Paramétrage du port
- serialPort.notifyOnDataAvailable(true);
- try {
- serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
- } catch (UnsupportedCommOperationException e) {}
- System.out.println("Port GPS " + portID.getName() + " ouvert, en attente de lecture..." );
- }
- /*
- * Méthode qui effectue la lecture de la trame sur le port COM
- */
- public void ReadSerialPort(){
- String reponse = new String();
- try {
- //On lit la trame
- reponse = (String) fluxLecture.readLine();
- String liste[] = reponse.split(",");
- if (liste[0].equals("$GPGGA")){
- // Si pas de fix GPS
- if(liste[6].equals("0")) {
- System.out.println("Pas de Fix Satellite...");
- } else { // Sinon on affiche les coordonnées
- Nmea coordonne = new Nmea();
- coordonne.parse(reponse);
- //System.out.println(coordonne.parse(reponse));
- System.out.println("Latitude: "+ liste[2]+" "+liste[3]+", Longitude: "+ liste[4]+" "+liste[5]);
- }
- }
- } catch (IOException e) {
- System.out.println("NMEA Error");
- //e.printStackTrace();
- }
- //fluxLecture.close();
- //serialPort.close();
- }
- public void serialEvent(SerialPortEvent event) {
- switch (event.getEventType()) {
- case SerialPortEvent.DATA_AVAILABLE :
- this.ReadSerialPort();
- break;
- default:
- break;
- }
- }
- /*
- * Methode qui scanne tous les ports COM et test si des données viennent du port COm scanné
- */
- @SuppressWarnings("rawtypes")
- public void listPort(){
- Enumeration listePorts = CommPortIdentifier.getPortIdentifiers();
- while (listePorts.hasMoreElements()){
- portID = (CommPortIdentifier) (CommPortIdentifier) listePorts.nextElement();
- if(portID.getPortType()==CommPortIdentifier.PORT_SERIAL){
- System.out.println("Nom du PORT COM :"+portID.getName());
- System.out.println("Type de PORT COM :"+portID.getPortType());
- if (portID.getName().equals("COM1")) {
- this.ModeEvenement(portID.getName());
- }
- }
- }
- }
- public static void main(String[] args) {
- Gps test = new Gps();
- test.listPort();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement