This discussion is archived
0 Replies Latest reply: May 7, 2009 3:38 AM by 800393 RSS

Reading from Serial Port?

800393 Newbie
Currently Being Moderated
Hi,

I am reading from GPS device which is attached on USB port. The device is a data logger, it logs the data in SBP format. One can download the logged data by issuing command. The downloaded data is written to a file and then converted to gpx or kml using gpsbabel. The code for reading the data from the device and writing it to the file is given below. Can anyone let me know whats wrong in it since I am not able to convert that file to any other format. Is the code for reading and writing to file is proper
switch(event.getEventType()) 
             {
                  case SerialPortEvent.DATA_AVAILABLE:
                          int c = 0;
               System.out.println("In data available");
               buffer = new byte[1024];
                          try
                          {
                               int availableBytes = in.available();
                      
                               if(lineCount == 0) {
                                    while((c = in.read()) != 10) {
                                         if(c != 13 && lineCount == 0) {
                                              readBuffer.append((char) c);
                                              System.out.println("Scanned input received:" + readBuffer.toString());
                                         } else {
                                              lineCount = lineCount + 1;
                                              c = in.read();
                                              readBuffer.append((char) c);
                                              break;
                                         }
                                    }
                               } else {
                                    if(availableBytes > 0) {
                                         c = in.read(buffer, 0, availableBytes);
                                         //System.out.println("Available Bytes => " + availableBytes);
                                         fout.write(buffer, offset, c);
                                         offset += c;
                                         System.out.println("Offset => " + offset);
                                    }
                               }
                         }
                          catch ( IOException e )
                          {
                               e.printStackTrace();
                               System.exit(-1);
                          }             
                  }
Can some one let me know if something is wrong in this code. Is the use of inputstream read method and fileoutputstream write method is proper. Its very urgent can someone let me know.

Thanks & Regards
Sunil