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();
}
}
}
本文介绍了一个简单的Java程序,该程序通过串口接收GPS模块发送的数据,并将这些数据写入本地文件。程序使用了javax.comm包来实现串口通信,并设置波特率为4800,数据位为8,停止位为1,无校验位。
3950

被折叠的 条评论
为什么被折叠?



