Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass inputstream to another class  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have three classes that I am working with.
The first class opens the GPS and reads the data.
The second parses the data.
The third is going to be the parsed data in a GUI.

My question is how do I pass the global variable gpsData to classC from NMEA?


I am using code that I found online for the gps data.

It looks like the NMEA class is dependent on serialGps and serialGps is dependent on NMEA.

I need to pass the inputLine which sets the global variable "gpsData" contained in the NMEA class to a third class which will be my GUI.

  
This is the serialGps class

 

The NMEA class:


classC



 
Sheriff
Posts: 23499
46
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Greg Posey wrote:My question is how do I pass the global variable gpsData to classC from NMEA?



You already did it by declaring it public. But that's not the ideal way to do it, architecturally speaking. It would be more usual for NMEA to have a getter method which returns that variable.

And welcome to the Ranch!
 
Saloon Keeper
Posts: 8734
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Never use static fields for anything other than constants. You will very easily introduce bugs in your application if your classes can just pull application state out of thin air. When a method needs something that's not a constant and not part of the object state, pass it as a method argument. When an object needs something that's not a constant across multiple method calls, pass it as a constructor argument.
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Welcome to CodeRanch!

Never use static fields for anything other than constants. You will very easily introduce bugs in your application if your classes can just pull application state out of thin air. When a method needs something that's not a constant and not part of the object state, pass it as a method argument. When an object needs something that's not a constant across multiple method calls, pass it as a constructor argument.




So the flow of events is:

In my GUI, I have a start GPS button.
Pseudocode


I initialize the SerialGps class in my gui class.
The gps starts when "Start Gps" is clicked.

SerialGps creates a new instance of NMEA.
I need to get the nmea string from that instance of NMEA to my GUI class.

After that I have no idea what to do. Would I need a method? A constructor? I am clueless when it comes to the flow of java.
 
Stephan van Hulst
Saloon Keeper
Posts: 8734
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your GUI needs to register a StateListener with the SerialGps class, and update itself according to the events it receives.

Your problem becomes much easier if you create an event class per type of GPS message that can be parsed. A listener should then have a method per event type. The task that's responsible for reading GPS messages parses a message into an event, and notifies all listeners of the event.
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very simplified version of the GUI. Whipped it up in netbeans real quick.
Is it possible to take the string "nmea" from class NMEA and pass it to string "inputLine" in this class....or do I have to get it from SerialGps?

If someone could point me to some links that would help me, I would appreciate that.


 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below


should be



and then under the button1actionperformed, it should have three methods to execute/call:

 
Paul Clapham
Sheriff
Posts: 23499
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Greg Posey wrote:Is it possible to take the string "nmea" from class NMEA...



I looked all through that class, several times, and I don't see either a string with that constant value or a variable with that name. I even used the search in my browser and couldn't find such a thing. So could you review that question and clarify it?
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Greg Posey wrote:Is it possible to take the string "nmea" from class NMEA...



I looked all through that class, several times, and I don't see either a string with that constant value or a variable with that name. I even used the search in my browser and couldn't find such a thing. So could you review that question and clarify it?



 
Paul Clapham
Sheriff
Posts: 23499
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that's a local variable in a method of the NMEA class. It only exists when the method is running and it isn't accessible to anything outside of NMEA.
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:But that's a local variable in a method of the NMEA class. It only exists when the method is running and it isn't accessible to anything outside of NMEA.



I get that it is a local variable. Can I make a method or a constructor that would make it available in another class?
 
Stephan van Hulst
Saloon Keeper
Posts: 8734
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should probably create a class that encapsulates all the data of an NMEA sentence. The parsers should then return instances of this class. When you've parsed a sentence, pass it to a an object that's responsible for dispatching the sentence to the applicable GPS message handler method.
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You should probably create a class that encapsulates all the data of an NMEA sentence. The parsers should then return instances of this class. When you've parsed a sentence, pass it to a an object that's responsible for dispatching the sentence to the applicable GPS message handler method.




So in NMEA, create an instance of my getterSetter class and then access that instance of getterSetter in the gui?

 
Paul Clapham
Sheriff
Posts: 23499
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Greg Posey wrote:Can I make a method or a constructor that would make it available in another class?



But you would be passing that string to NMEA in the first place, via the getUpdatedStatus method. Or more accurately, you'd be passing a very similar string with a $ character at the front. So why don't you just hang onto it, rather than trying to get it back out of NMEA?
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Greg Posey wrote:Can I make a method or a constructor that would make it available in another class?



But you would be passing that string to NMEA in the first place, via the getUpdatedStatus method. Or more accurately, you'd be passing a very similar string with a $ character at the front. So why don't you just hang onto it, rather than trying to get it back out of NMEA?



How else would I get the gps values(time, lat, long, velocity, etc) to my GUI class?
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say I have 4 classes:

SerialGps
NMEA
getterSetter
GUI



I create a new instance of getterSetter in NMEA. NMEA parses string "nmea" and sets the values in the instance of getterSetter.
How would i access the instance of getterSetter in NMEA from GUI?
 
Paul Clapham
Sheriff
Posts: 23499
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Greg Posey wrote:How else would I get the gps values(time, lat, long, velocity, etc) to my GUI class?



Aha. This is what I have been missing for the whole thread -- your requirements. So far we've just been having random discussions about Java code which aren't getting anywhere.

So, let's start with those requirements. It appears you pass some data into the NMEA class, and it does some processing magic. I would have expected it to then provide some methods which allow you to get the results of that processing magic, but all I can see is that method which takes that data and returns a GpsState object. So I would expect that GpsState object to contain some useful data.

But you don't mention that class anywhere. Why not? It seems like it should contain what you need.
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my getterSetter class...well a snippet.




In nmea i initialized it with this line



Then in my gui, I was able to access it with




So my problem has been solved. I highly doubt my program conforms to proper java formats...but it is working ATM...although i do get sporadic parsing errors at times...
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate the insights you guys gave me. Definitely thankful for you putting up with me. im very new to JAVA and have a hard time asking the right questions.
 
Stephan van Hulst
Saloon Keeper
Posts: 8734
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took a look at the NMEA spec, and came up with a small library that handles parsing such sentences. Take a look:
 
Greg Posey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan, thanks for writing/finding that. I will have to spend some time to understand what is going on in that code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!