Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA-GPS接收器在控制台中发送奇怪/编码的帧_Java_Com_Gps_Encode_Nmea - Fatal编程技术网

JAVA-GPS接收器在控制台中发送奇怪/编码的帧

JAVA-GPS接收器在控制台中发送奇怪/编码的帧,java,com,gps,encode,nmea,Java,Com,Gps,Encode,Nmea,我有一个GPS接收器,可以给我发送NMEA帧。 我的代码检索这些,但形式非常奇怪: 我用它来查看我的接收器接收到的NMEA帧,没有问题 编辑-以下是我正在使用的代码: public class GPSFrame extends Observable implements Runnable { static Thread myThread=null; static BufferedReader br; static BufferedWriter wr; stat

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

我用它来查看我的接收器接收到的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);
        }
    }   
}
编辑:

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);
        }
    }   
}
当我第一次使用读取GPS帧,然后启动我的应用程序时,我可以在控制台中看到正确的GPS帧。但是,当我尝试先用我的应用程序读取GPS帧时,我已经对帧进行了编码。

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

提前感谢你

问候,


Tofuw

正如我在评论中所说,您没有正确阅读COM端口。我找到了一个库,可以帮助您了解如何从com端口读取。代码很旧,但我认为它仍然有用:

签出这些类:和

<> P>也有类似的C++线程,可能有帮助。p>
如果您找到了解决方案,请发布。看到它会很有趣:)

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

我的代码有点问题,因为我使用RandomAccessFile在COM端口上读取。使用这种方法,我可以读取接收器发送的帧,但不能正确读取。解决了这个问题,我必须
配置COM端口
,这在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.0201213,0.0,W*70 $GPGGA,100409.0004858.01754,N,00150.99913,E,1,15,0.7034.93,M,47.2,M,,*66

我认为默认情况下,Databits或配置为5或6。视情况而定。这就是为什么最好配置端口

配置COM端口的方法(或其中一种方法?)是使用SerialPort

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

这就是我找到此解决方案的地方(/!\French sites:D):

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

祝你有愉快的一天

托福


PS:感谢帮助我的Aphex和AlexWien

请将您的代码添加到问题中。@Aphex抱歉,我刚刚添加了我的代码。我认为您没有正确地从com端口读取。。。使用readUTF8()试试,或者查看oracle的示例:检查波特率,确保GPS以该速率发送?@AlexWien,我认为问题不在那里,波特率是1000ms。否则,当我第一次用PuTTY读取GPS帧,然后启动我的应用程序时,我可以正确地检索GPS帧。嗨@aphex,谢谢你的帮助。我刚刚发布了一个可能的解决方案:)