![](/img/trans.png)
[英]JAVA - Retrieve NMEA frame from a GPS Receptor -More details about RandomAccessFile WriteBytes method
[英]JAVA - GPS RECEPTOR sending strange/encoded frames in console
我有一個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框架時,我已經對框架進行了編碼。
我不知道為什么我無法檢索這種形式的框架。 有人可以指導我解決這個問題嗎?
預先感謝您!
問候,
豆腐
正如我在評論中所說,您未正確從COM端口讀取信息。 我找到了一個庫,可以幫助您弄清楚如何從com端口讀取內容。 該代碼已經很老了,但我認為它仍然有用: http : //javanmea.sourceforge.net/
檢出以下類: NMEAReader和CustomReader 。
還有一個類似的c ++線程可能會有所幫助。 從COM PORT C ++接收NMEA0183數據
如果找到解決方案,請發布它。 看到它會很有趣:)
我已經找到解決問題的方法! :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
我認為默認情況下是數據位還是配置為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幫助我
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.