JAVA - GPS接收器在控制台中发送奇怪/编码的帧 [英] JAVA - GPS RECEPTOR sending strange/encoded frames in console
问题描述
我有一个GPS接收器,它发送给我NMEA帧。
我的代码检索这些,但是以一种非常奇怪的形式:
我使用 然后启动我的应用程序,我可以在控制台中看到正确的GPS帧。但是当我尝试用我的应用程序读取GPS帧时,我已经编码帧。
我不知道为什么我无法检索框架。
有人可以指导我解决这个问题吗?
感谢您提前!
谨此,
Tofuw
我的问题 ! :D
我的代码有点问题,因为我使用RandomAccessFile读取我的COM端口。使用这种方法,我可以读取受体发出的框架,但不正确。解决了,我必须 配置COM PORT
,这是不可能与RandomAccessFile 。
我做了一个配置测试:
-
使用DATABITS_5或DATABITS_6,框架类型:
$%()
:2)1
-
但是使用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, 1,15,0.7,034.93,M,47.2,M ,, * 66
我认为默认情况下,Databits或配置与5或6.它依赖。这就是为什么它最好自己配置端口。
配置COM端口的方式(或方式之一)是使用 SerialPort
这里是一个解决方案,这是真正有用的(在这个例子中,我们读取数据与事件模式,但您可以使用流模式 - 请参阅下面的第2个链接):
public class GPScom implements SerialPortEventListener
{
private String portcom;
private CommPortIdentifier portid = null;
private SerialPort serialport;
private BufferedReader fluxgps; //读取连接GPS的流程端口
public static void main(String [] args)
{
//驱动程序初始化
Win32Driver driver = new Win32Driver ();
driver.initialize();
GPScom gpscom = new GPScom();
gpscom.listPort();
}
//扫描所有可用的端口
public void listPort()
{
枚举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());
}
}
}
//初始化端口
public void ModeEvenement(String portcom)
{
/ / Retrieve ID Port
try {portid = CommPortIdentifier.getPortIdentifier(portcom);}
catch(NoSuchPortException e){System.out.println(e);}
打开端口
try {serialport =(SerialPort)portid.open(ModeEvenement,2000);}
catch(PortInUseException e){System.out.println(e);}
//检索数据流
try {fluxgps = new BufferedReader(new InputStreamReader(serialport.getInputStream()));}
catch(IOException e){System.out.println(e); }
//添加监听器
try {serialport.addEventListener(this);}
catch(TooManyListenersException e){System.out.println(e);}
//配置端口
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(端口已打开,正在等待读取);
}
//这里我们为测试读取7帧
public void ReadSerialPort()
{
int i = 7;
String reponse = new String();
try
{
System.out.println(i =+ i);
while(i!= 0)
{
System.out.println(读取COM端口\\\
);
reponse =(String)fluxgps.readLine();
System.out.println(reponse);
i--;
System.out.println(i =+ i);
}
}
catch(IOException e){System.out.println(e);}
//在ferme le flux de lecture
try {fluxgps.close();}
catch(IOException e){System.out.println(e);}
serialport.close();
}
public void serialEvent(SerialPortEvent event)
{
//只读数据
switch(event.getEventType())
{
case SerialPortEvent.DATA_AVAILABLE:
this.ReadSerialPort(); //如果数据可用,启动读取
break;
默认值:
break; // Else,do nothing
}
}
}
这是我找到此解决方案的地方(/!\\法国网站:D):
- 阅读COM端口
- a href =http://christophej.developpez.com/tutoriel/java/javacomm/#L2.3.3 =nofollow>如何将javax.comm用于串行端口
如果你遇到和我一样的问题,我希望它会帮助你。
!!!
Tofuw
PS:感谢Aphex和AlexWien帮助我
I have a GPS receptor, which send me NMEA frames. My code retrieve these ones, but in a really strange form :
I am using PuTTY to see the NMEA frames received by my receptor, and there is no problem.
EDIT - Here is the code I am using :
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);
}
}
}
EDIT :
When I first read GPS Frames with PuTTY then launch my application, I can see correct GPS Frames in console. But when I try to read the GPS Frame with my application first, I have encoded Frames.
I don't know why I can't retrieve the frames in this form. Can someone guide me to resolve this problem please ?
Thanks to you in advance !
Regards,
Tofuw
I've found a solution for my problem ! :D
My code is a little problematic because I used RandomAccessFile to read on my COM Port. With this method I can read the frame sended by the receptor, but not properly. The solved it, I have to CONFIGURE the COM PORT
, which is not possible with RandomAccessFile.
I did a configuration test :
With DATABITS_5 or DATABITS_6, I received these sort of frame :
$%() :2 ")1"2
But with DATABITS_7 or DATABITS_8, I received good frame :
$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
I think by default, Databits or configured with 5 or 6. It depends. That is why it is better configure up ourselves the Port.
The way (or one of the way ?) to configure the COM Port, is to use SerialPort.
Here is a solution which is really helpfull (in this example we are reading data with an event mode, but you can use the flow mode - please see the 2nd links below for it) :
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
}
}
}
This is where I found this solution (/!\ French sites :D ):
I hope it will help you if you encounter the same problem as me
Have a nice day !!!
Tofuw
PS : Thanks to Aphex and AlexWien who helped me
这篇关于JAVA - GPS接收器在控制台中发送奇怪/编码的帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!