• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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









 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Gary Hill
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

reply
    Bookmark Topic Watch Topic
  • New Topic