psiubest
asked on
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;
}
}
}
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
System.exit(1);
}
portList = CommPortIdentifier.getPort
while (portList.hasMoreElements(
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SE
if (portId.getName().equals(P
try {sPort = (SerialPort) portId.open("GarApp", TIMEOUT);}
catch (PortInUseException e) {}
try {inputStream = sPort.getInputStream();}
catch (IOException e) {}
try {sPort.addEventListener(th
catch (TooManyListenersException
sPort.notifyOnDataAvailabl
try {
sPort.setSerialPortParams(
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationE
}
}
}
}
public void serialEvent(SerialPortEven
int newData = 0;
char mybyte;
switch(event.getEventType(
case SerialPortEvent.BI:
case SerialPortEvent.OUTPUT_BUF
break;
case SerialPortEvent.DATA_AVAIL
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
} catch(IOException ex) {
System.err.println(ex);
return;
}
}
break;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
J