Microsoft
Software
Hardware
Network
Question : GPP Java Program
Hi people, i am a newbie in this things of Java.
I have one program, which i pretend to modify, but first i need to know how it works. Well, i know what he does, but i dont know how is structured, like variables, classes etc etc etc. So, what i want is if someone can comment all the next code. To have and idea how to work it out after. Thanks a lot.
import java.io.*;
import java.util.*;
import javax.comm.*;
public class GpsReader implements SerialPortEventListener {
private InputStream inputStream;
private SerialPort sPort;
private int bcount = 0;
static OutputStreamWriter outW = null;
static CommPortIdentifier portId;
static Enumeration portList;
public static final String FILENAME = "NMEA.txt";
public static final String PORTNAME = "COM5";
public static final int BAUDRATE = 4800;
public static final int TIMEOUT = 2000;
public static void main(String[] args) {
GpsReader g = new GpsReader();
g.mymain();
}
void mymain(){
try {
outW = new OutputStreamWriter(
new FileOutputStream (FILENAME));
}
catch (IOException e) {
System.err.println("Fichei
ro de escrita não aberto\n" + e.toString());
System.exit(1);
}
portList = CommPortIdentifier.getPort
Identifier
s();
while (portList.hasMoreElements(
)) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SE
RIAL) {
if (portId.getName().equals(P
ORTNAME)) {
try {sPort = (SerialPort) portId.open("GarApp", TIMEOUT);}
catch (PortInUseException e) {}
try {inputStream = sPort.getInputStream();}
catch (IOException e) {}
try {sPort.addEventListener(th
is);}
catch (TooManyListenersException
e) {}
sPort.notifyOnDataAvailabl
e(true);
try {
sPort.setSerialPortParams(
BAUDRATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationE
xception e) {}
}
}
}
}
public void serialEvent(SerialPortEven
t event) {
int newData = 0;
char mybyte;
switch(event.getEventType(
)) {
case SerialPortEvent.BI:
case SerialPortEvent.OUTPUT_BUF
FER_EMPTY:
break;
case SerialPortEvent.DATA_AVAIL
ABLE:
while(newData != -1) {
try {
newData = inputStream.read();
if (newData == -1) {break;}
mybyte = (char)newData;
outW.write(mybyte);
bcount = bcount + 1;
if(mybyte == '$') {System.out.print("\r"+bco
unt+" bytes");}
} catch(IOException ex) {
System.err.println(ex);
return;
}
}
break;
}
}
}
Answer : GPP Java Program
Here's the code with some comments and references to online documentation. Ask if you need specific info on some constructs. In order to really understand this, you'll have to read the documentation of the classes used. I would think that this code is still "work in progress"; it will do what's required but needs further work.
HTH, J
/* make names available, avoid full qualification:
http://java.sun.com/docs/b
ooks/jls/s
econd_edit
ion/html/
p
ackages.do
c.html#702
09
*/
import java.io.*;
import java.util.*;
import javax.comm.*;
/* class to copy GPS info from serial port into file */
public class GpsReader implements SerialPortEventListener {
private InputStream inputStream; /*
http://java.sun.com/j2se/1
.4.2/docs/
api/java/i
o/
InputStr
eam.html
*/
private SerialPort sPort; /*
http://java.sun.com/produc
ts/javacom
m/javadocs
/javax/com
m/
SerialPo
rt.html
*/
private int bcount = 0; /* byte count */
static OutputStreamWriter outW = null; /* class to write an output file:
http://java.sun.com/j2se/1
.5.0/docs/
api/java/i
o/
OutputSt
reamWriter
.html
*/
static CommPortIdentifier portId; /* which serial port to use:
http://java.sun.com/produc
ts/javacom
m/javadocs
/javax/com
m/
CommPort
Identifier
.html
*/
static Enumeration portList; /* list of all available serial ports */
public static final String FILENAME = "NMEA.txt"; // file to write output to; full path would be better
public static final String PORTNAME = "COM5"; // which serial port to use; probably an USB serial device
public static final int BAUDRATE = 4800; // baud rate - NMEA standard
public static final int TIMEOUT = 2000; //
/* main program: Create a new GPSReader object and run its mymain method */
public static void main(String[] args) {
GpsReader g = new GpsReader();
g.mymain();
}
/* mymain method - main method of GPSReader class */
void mymain(){
/* create/open output file FILENAME = NMEA.TXT */
try {
outW = new OutputStreamWriter(
new FileOutputStream (FILENAME));
}
/* if this fails, issue an error message and exit with error code 1 */
catch (IOException e) {
System.err.println("Fichei
ro de escrita não aberto\n" + e.toString());
System.exit(1);
}
// find all available communication ports and copy them to port list */
portList = CommPortIdentifier.getPort
Identifier
s();
// iterate through all ports and check whether it's a serial port
while (portList.hasMoreElements(
))
{
portId = (CommPortIdentifier) portList.nextElement();
// if it's a serial port, check whether it's the port specified as PORTNAME (COM5:)
if (portId.getPortType() == CommPortIdentifier.PORT_SE
RIAL) {
if (portId.getName().equals(P
ORTNAME)) {
// yes, it's our port; try to open it and assign name "GarApp"
try {sPort = (SerialPort) portId.open("GarApp", TIMEOUT);}
// no further error handling
catch (PortInUseException e) {}
// make the port available as stream to easy further processing
try {inputStream = sPort.getInputStream();}
catch (IOException e) {}
// assign an event handler - in this case, the SerialEvent method further down.
// check
http://java.sun.com/produc
ts/javacom
m/javadocs
/javax/com
m/
SerialPo
rtEvent.ht
ml
try {sPort.addEventListener(th
is);}
catch (TooManyListenersException
e) {}
// we're interested only if there's new data available
sPort.notifyOnDataAvailabl
e(true);
/* a number of events is not handled, including error events and protocol handling events. This should really be done for
production quality code.
*/
// set comm parameters
try {
sPort.setSerialPortParams(
BAUDRATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationE
xception e) {}
}
}
}
// we're done initializing; the event handler will process all data coming from the serial port.
}
/* process all serial port events. In this case, write the received data to the NMEA.TXT file. */
/* Check out
http://java.sun.com/produc
ts/javacom
m/javadocs
/javax/com
m/
SerialPo
rtEvent.ht
ml
for all events */
public void serialEvent(SerialPortEven
t event) {
int newData = 0;
char mybyte;
/* a number of events is not handled, including error events and protocol handling events. This should really be done for
production quality code.
*/
// we're not interested in BREAK signals or if the output buffer is empty. Code probably isn't necessary since these
switch(event.getEventType(
)) {
case SerialPortEvent.BI:
case SerialPortEvent.OUTPUT_BUF
FER_EMPTY:
break;
// more interesting: if there's new data available
case SerialPortEvent.DATA_AVAIL
ABLE:
// while there's more data avilable on the serial port
while(newData != -1) {
// read one byte from the InputStream associated with the serial port
try {
newData = inputStream.read();
// stop if there's no more data
if (newData == -1) {break;}
// translate into a byte
mybyte = (char)newData;
// write to file
outW.write(mybyte);
bcount = bcount + 1;
// if the byte is a dollar sign, issue a progress message to the console; for debugging only
if(mybyte == '$') {System.out.print("\r"+bco
unt+" bytes");}
// if something goes wrong, issue an error message and exit
} catch(IOException ex) {
System.err.println(ex);
return;
}
}
break;
}
}
}
Random Solutions
Script on IE causes it to run slowly
Mail sent out from the wrong mail box
Seperating connections for use with certain applications, Cisco VPN, Bandwidth Throttling.
Wired, can't ping only if I clear arp table
SBS 2003 with exchange spam relaying - queue filling up
USE OF CROSSOVER CABLE TO DIRECTLY TRANSFER FILES
File extention association
Run As Gives Access Denied
Drive Mapping Failures - Seemingly No Pattern
Cisco Networking: How to determine if CIDR address is Network or Broadcast