maxloop 发表于 2013-1-19 04:13:46

怎样用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();
}
  }
}
 
页: [1]
查看完整版本: 怎样用Java comm读取gps信息