*
The moose likes Beginning Java and the fly likes Tried for hours and hours to work this out, data buffer from one class back to my main class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tried for hours and hours to work this out, data buffer from one class back to my main class?" Watch "Tried for hours and hours to work this out, data buffer from one class back to my main class?" New topic
Author

Tried for hours and hours to work this out, data buffer from one class back to my main class?

Gary Hill
Greenhorn

Joined: Nov 06, 2010
Posts: 15
Hi

I have tried for hours and hours to work this out as well as scanning the internet, but still am no closer as to how to do this?

Briefly the code receives data from the Serial Port on a PC (this i have working)

Eventually this data gets stored in = buffer.

I wish to use this data in another class to be sent to a jTextField (i think i know how to do this)

But do not know how to get the buffer data back to my main class ? (for testing purposes and to check it works)

The part i require help on is in "public static class SerialReader implements Runnable" and marked
>>>>>>>>>>>>>>>>>>>>>>> HOW TO RETURN THIS BUFFER TO MAIN? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

I have commented the code a lot so should help.

Thanks if you can help!

Regards Gary









Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

You have several choices, really. One is to create a method in the Main class that takes the buffer in as a parameter and does work with it. The 'least amount of work' would be something like this:

Then you call that method when you need to:

Of course, the method is static here as an example. In a real-life case you might need to make it an instance method with all the related necessity of making that instance available to the method which calls it. This is especially true if you will have a GUI or some other class/Object which needs to work with the data. Personally, I would model it as a Listener. Make an interface method which takes a byte[], then make Main implement the interface and do something with the byte[]. The make TwoWaySerialComm and SerialReader accept 'Listener's and have the SerialReader call the appropriate method when the data is read.



Steve
Gary Hill
Greenhorn

Joined: Nov 06, 2010
Posts: 15
Hi Steve,

Thanks for your reply, as this is my 1st Java project, the 2nd choice is hard to understand at the moment.

I tried the 1st one and did get it to print to the console the contents of buffer from my Main Class.

But then tried to print it to my jTextArea from my main method, but because its in a different method it couldn't find the symbol variable "buffer"
Which is what you said would happen, i think?

Regards

Gary

 
wood burning stoves
 
subject: Tried for hours and hours to work this out, data buffer from one class back to my main class?
 
Similar Threads
Two way communcation with the serial port (modem to modem)
Java Telephone API (JTAPI)
Java - TINI board - Timed reading
TINIm400 module and timed reading
Problem with jframe, serial communication, dynamic changing value and displaying it