Newbie - need direction on adding a comport listener to batik application
I need some direction for adding a commport listener to an existing gui application and displaying parsed pieces of the commport data within the current svg document. I am new to java and I am having problems working on this new project. I have between 25 and 30 java books that I am using as reference and reading in my spare time along with many websites, however; I am really stuck.
Here is what I am working with:
Batik - an open source SVG browser/toolkit from apache
javax.comm - for reading form the comm port
I am trying to set up a GPS listener (using the serial port) that can be turned on and off that will display one of the GPS sentence strings in the loaded document. I get the basics and can follow all the examples I find but I have not been able to put the pieces together. For instance I can write a class that will do a print line for the specific string I am looking for, and I can parse it to the specific items in the string. However, I do not understand what I need to do to build/implement a listener for the commport or how to send data to the document.
Any ideas or sources of information that might help in my understanding would be greatly appreciated.
[1218 byte] By [
mordancya] at [2007-10-1 23:10:57]

Update to help others that might have the same issue that I had since it was next to impossible for me to find the information.
I posted this in the original post in the forum after working on the problems for a couple weeks trying to resolve it myself. I am still trying to learn java and while I wish I was getting the "gist" of Java programming quicker, I am gaining ground every day. Nothing in Java functions, is implemented, or is used the way I was taught in school. I forsee many more problems with my project as I still have lots to learn about property files, threads, classpaths, and ant scripts.
It was easy to write a class that read the GPS data form the serial port seperate from my project. I modifed the simple read that comes with Sun's javax.comm and was able to read the data and store it in variables as well as spit out the results to the console. However, when I added the class to the batik project, I recieved all sorts of errors including "Caught java.lang.NullPointerException: name can't be null while loading driver " and multiple null pointer errors. My assumption was that I was not referenceing the classpath properly in the ant scripts, there was a security restriction in batik, an issue with loading the drive, or it was a reflection of my poor java skills (having done no real java development up to this point).
So after lost of reading and searching the net and finding that this is a common problem with the sun javax.comm and tons of people ask for help with little result. I finally found one reference to someone using ibm's comm library & driver. I downloaded and installed the files from ibm and I also uninstalled all the files from Sun's javax.comm. You can find it by searching google for ibm-javacomm-win32-x86.zip.
All the variations of the GPS reader classes I created now function in my project. I am working on breaking it up into seperate classes to optimize functionallity, adatability, and to be more object oriented. Here is an example of one of them. I hope this class file and the information about the ibm comm helps someone else. Let me know if anyone finds it useful.
///////////////////START OF CODE///////////////////
import java.io.*;
import java.util.*;
import javax.comm.*;
public class ReadGPS implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
static InputStream inputStream;
static SerialPort serialPort;
static Thread readThread;
static BufferedReader reader;
static String tempString;
//Variables for storing NMEA strings
static String gpgga;
static String gprmc;
static String gpgsa;
static String gpgll;
static String gpgsv;
static String gpvtg;
public static void main(String[] args) {
booleanportFound = false;
StringdefaultPort = "COM3";
if (args.length > 0) {
defaultPort = args[0];
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier)portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port: " + defaultPort);
portFound = true;
ReadGPS reader = new ReadGPS();
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}
public ReadGPS() {
try {
serialPort = (SerialPort) portId.open("GPSReader", 2000);
} catch (PortInUseException e) {System.out.println("error 1");}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {System.out.println("error 2");}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println("error 3");}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println("error 4");}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
//for testing System.out.println("ReadGPS.run() starting...");
Thread.sleep(200);
//for testing System.out.println("...ReadGPS.run() started");
} catch (InterruptedException e) {System.out.println("error 5");}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
while (reader.ready()){
String currLine = reader.readLine();
try{
//Change this code to use the Split() function
StringTokenizer st = new StringTokenizer(currLine, ",");
tempString = st.nextToken();
//for testing System.out.println(currLine);
//check value of first token in currLine assign to variable
if (tempString.equals("$GPGGA"))
gpgga = new String(currLine.toString());
else if(tempString.equals("$GPRMC"))
gprmc = new String(currLine.toString());
else if(tempString.equals("$GPGSA"))
gpgsa = new String(currLine.toString());
else if(tempString.equals("$GPGLL"))
gpgll = new String(currLine.toString());
else if(tempString.equals("$GPGSV"))
gpgsv = new String(currLine.toString());
else if(tempString.equals("$GPVTG"))
gpvtg = new String(currLine.toString());
else
System.out.println("invalid line: " +currLine.toString());
}catch (Exception e) {}//System.out.println("error 6\n" + e);}
}
} catch (IOException e) {System.out.println("error 7");}
break;
}
}
public String getGPGGA(){
return gpgga;
}
public String getGPRMC(){
return gprmc;
}
public String getGPGSA(){
return gpgsa;
}
public String getGPGLL(){
return gpgll;
}
public String getGPGSV(){
return gpgsv;
}
public String getGPVTG(){
return gpvtg;
}
}
///////////////////END OF CODE///////////////////