【问题标题】:JAVA - GPS RECEPTOR sending strange/encoded frames in consoleJAVA - GPS 接收器在控制台中发送奇怪/编码的帧
【发布时间】:2013-12-17 10:17:23
【问题描述】:

我有一个 GPS 接收器,它向我发送 NMEA 帧。 我的代码检索了这些,但形式非常奇怪:

我正在使用PuTTY查看我的接收器接收到的NMEA帧,没有问题。

编辑 - 这是我正在使用的代码:

public class GPSFrame extends Observable implements Runnable
{
    static Thread myThread=null;
    static BufferedReader br;
    static BufferedWriter wr;
    static PrintWriter out;
    static InputStreamReader isr;
    static OutputStreamWriter osw;
    static java.io.RandomAccessFile port; 


    /**  CONSTRUCTOR **/
    public  GPSFrame()
    {    
         myThread=new Thread(this);
    }

    public void start()
    {
        try 
        {
            port=new java.io.RandomAccessFile("COM5","rwd");
            port.writeBytes("\r\n");
            port.writeBytes("c,31,0,0,5\r\n");
            port.writeBytes("T,1000,1\r\n");
        }
        catch (Exception e){ System.out.println("start "+e.toString()); }
        // The thread start automatically run() method
        myThread.start();
    }

/**********************************************************************************************
 *************************** RETRIEVE GPS FRAMES AND SEND TO SERVEUR **************************
 **********************************************************************************************/
    public void run() 
    {
        System.out.println("lecture COM...");
        // INFINIT LOOP - GPSFrame is always listening for the GPS receptor
        for(;;)
        {
            String st = null;
            try 
            {
                st=port.readLine();
                String[]gpsframe=st.split(",");

                /* IMPORTANT - DON'T FORGET SETCHANGED() or GPSFrame'll never
                 * notify UPDATE() ServerBoard method - We'll never see any changes */
                setChanged();
                notifyObservers(st);

            } 
            catch (IOException e){ System.out.println(e.getMessage()); }
            // Show in console
            System.out.println(st);
        }
    }   
}

编辑:

当我第一次使用 PuTTY 读取 GPS 帧时,然后启动我的应用程序,我可以在控制台中看到正确的 GPS 帧。但是当我尝试先用我的应用程序读取 GPS 帧时,我已经对帧进行了编码。

我不知道为什么我无法检索此表单中的帧。 有人可以指导我解决这个问题吗?

提前感谢您!

问候,

豆腐

【问题讨论】:

  • 请将您的代码添加到问题中。
  • @aphex 抱歉,我刚刚添加了我的代码。
  • 我认为您没有正确地从 com 端口读取...尝试使用 readUTF8() 或从 oracle 中查看此示例:blogs.oracle.com/hinkmond/entry/rpi_and_java_embedded_gpio8
  • 检查波特率,确定 GPS 以该速率发送?
  • @AlexWien,我认为问题不存在,波特率为1000ms。否则,当我第一次使用 PuTTY 读取 GPS 帧,然后启动我的应用程序时,我可以正确检索 GPS 帧。

标签: java com gps encode nmea


【解决方案1】:

正如我在评论中所说,您没有正确地从 COM 端口读取数据。我找到了一个可以帮助您弄清楚如何从 com 端口读取数据的库。代码很旧,但我认为它仍然有用: http://javanmea.sourceforge.net/

查看这些课程:NMEAReaderCustomReader

还有一个类似的 c++ 线程可能会有所帮助。 Receive NMEA0183 data from COM PORT C++

如果您找到解决方案,请发布。看到它会很有趣:)

【讨论】:

  • 嗨@aphex,感谢您的帮助。我刚刚发布了一个可能的解决方案:)
【解决方案2】:

我找到了解决问题的方法! :D

我的代码有点问题,因为我使用 RandomAccessFile 在我的 COM 端口上读取。使用这种方法,我可以读取接收器发送的帧,但不能正确读取。解决了,我必须 CONFIGURE the COM PORT,这 RandomAccessFile 是不可能的

我做了一个配置测试:

  • 使用 DATABITS_5 或 DATABITS_6,我收到了这样的帧:

    $%() :2 ")1"2

  • 但是使用 DATABITS_7 或 DATABITS_8,我收到了很好的帧:

    $GPRMC,100409.000,A,4858.018,N,00150.999,E,0.0,0.0,201213,0.0,W*70 $GPGGA,100409.000,4858.01754,N,00150.99913,E,1,15,0.7,034.93,M,47.2,M,,*66

我认为默认情况下,Databits 或配置为 5 或 6。这取决于。这就是为什么最好自己配置端口。

配置 COM 端口的方式(或方式之一?)是使用 SerialPort

这是一个非常有用的解决方案(在此示例中,我们正在使用事件模式读取数据,但您可以使用流模式 - 请参阅下面的第二个链接) :

public class GPScom implements SerialPortEventListener
{
    private String portcom;
    private CommPortIdentifier portid=null; 
    private SerialPort serialport; 
    private BufferedReader fluxgps; // Reading flow port where the GPS is connected

        public static void main(String[]args)
    {
        // Driver initialization
        Win32Driver driver=new Win32Driver();
        driver.initialize();

        GPScom gpscom=new GPScom();
        gpscom.listPort();
    }

    // Scanning all available ports
    public void listPort()
    {
        Enumeration listport=CommPortIdentifier.getPortIdentifiers();
        int typeport;
        String GPSPortCOM;

        while(listport.hasMoreElements())
        {
            portid=(CommPortIdentifier)(CommPortIdentifier)listport.nextElement();
            if(portid.getPortType()==CommPortIdentifier.PORT_SERIAL)
            {
                System.out.println("Port Name : "+portid.getName());
                System.out.println("User : "+portid.getCurrentOwner());
                System.out.println("Use ? : "+portid.isCurrentlyOwned());
                System.out.println("Port type : "+portid.getPortType());

                this.ModeEvenement(portid.getName());
            }
        }
    }

    // Initialization of the port
    public void ModeEvenement(String portcom)
    {
        // Retrieve ID Port
        try{portid=CommPortIdentifier.getPortIdentifier(portcom);}
        catch(NoSuchPortException e){System.out.println(e);}

        // Open Port
        try{serialport=(SerialPort)portid.open("ModeEvenement",2000);}
        catch(PortInUseException e){System.out.println(e);}

        // Retrieve data flow
        try{fluxgps=new BufferedReader(new InputStreamReader(serialport.getInputStream()));}
        catch(IOException e){System.out.println(e);}

        // Add listener
        try{serialport.addEventListener(this);}
        catch(TooManyListenersException e){System.out.println(e);}

        // Configure Port
        serialport.notifyOnDataAvailable(true);
        try{serialport.setSerialPortParams(4800, SerialPort.DATABITS_6, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);}
        catch(UnsupportedCommOperationException e){System.out.println(e);}

        System.out.println("Port is open, waiting for the reading");
    }

    // Here we are reading 7 frames for test
    public void ReadSerialPort()
    {
        int i=7;
        String reponse=new String();

        try
        {
            System.out.println("i="+i);
            while(i!=0)
            {
                System.out.println("Reading the COM port \n");
                reponse=(String)fluxgps.readLine();
                System.out.println(reponse);
                i--;
                System.out.println("i="+i);
            }           
        }
        catch(IOException e){System.out.println(e);}

        // On ferme le flux de lecture
        try{fluxgps.close();}
        catch(IOException e){System.out.println(e);}

        serialport.close();
    }

    public void serialEvent(SerialPortEvent event)
    {
        // We are only reading data if available
        switch(event.getEventType())
        {
            case SerialPortEvent.DATA_AVAILABLE:
                this.ReadSerialPort(); // Launching the reading if data are available
                break;
            default:
                break; // Else, do nothing
        }
    }   
}

这是我找到这个解决方案的地方(/!\法语网站:D):

如果你遇到和我一样的问题,希望对你有帮助

祝你有美好的一天!!!

豆腐

PS : 感谢帮助我的 Aphex 和 AlexWien

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2022-08-03
    • 2012-07-24
    • 2023-03-18
    • 2011-02-01
    相关资源
    最近更新 更多