`
maxloop
  • 浏览: 3614 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
最近访客 更多访客>>
社区版块
存档分类
最新评论

怎样用Java comm读取gps信息

阅读更多

package com.comm.gps;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;

public class HFinder {

public static void main( String[] args) {
   String port="COM1";
receive(port);   

  } 
  public static void receive(String port)  { 
  try {
  File gpsFile=new File("C:/gps.txt");
  FileOutputStream fos=new FileOutputStream(gpsFile);
  OutputStreamWriter osw=new OutputStreamWriter(fos);
// DataOutputStream writer=new DataOutputStream(out);
  BufferedWriter writer=new BufferedWriter(osw);
SerialPort serialPort = ( SerialPort ) CommPortIdentifier.getPortIdentifier( port ).open( "GPS", 60 );

serialPort.setSerialPortParams( 4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE );

BufferedReader in = new BufferedReader( new InputStreamReader(serialPort.getInputStream()) );
while (true ) {
  String msg = in.readLine();
  //String wstr=msg+"\n";
  writer.write(msg);
  writer.newLine();
  System.out.println(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics