• 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

Hardware response VS Software response (Concurrency issue)

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



for the code above i get the result in the wrong sequence
return returnBuffer
Haha, Device Found = Device A


But the expected output should be in the following sequence
Haha, Device Found = Device A
return returnBuffer


This is because the hardware response is quite slow, it takes around 2-3 seconds before the listener receive the data.
in order forcing to get the expected result, yes! by adding "Thread.sleep(3000)" before the return statement, but i know this is incorrect and worst coding.

What should i add in the code to make sure the "return" statement executed only after the "listener" receive all the data. Kindly need your help.

Thanks in advanced. your help is much appreciated

 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where's the Serial class from?

Can you make it so it blocks this thread?
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Dave Tolls,

basically im calling this Serial class from a third party external Jar called pi4j.

ehm... im not really understand the concept of Thread/concurrency/synchronized and im in the midst of learning it. so, do you mind to share and show me some example? especially on this code?

thanks a lot.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you show us the code the prints "return returnBuffer"? While the line that you flagged does to the deed, it doesn't actually print it, so we have no idea when that is happening.

Henry
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry Wong,

Thanks, Basically i just need to monitor the output, System.out.println(XX) will do.
Anyway, the class which calling DataTesting.sendBytes(int) method is as below



 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Help
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't see the code that prints "return returnBuffer"?

Anyway when you use swing, the action listener code runs on a thread called the EDT. You seem to be updating a swing component with data taht is being retrieved asynchronously by another thread. Your actionlistener code is not waiting for this thread to fetch the data so it runs and completes before the other thread has fetched the data to display. Generally you should not be blocking the EDT (to wait for the results) so you should read up on how to use the SwingWorker to execute background tasks and update the UI when the taks have completed.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard to provide much help without knowing how that "serial" thing actually works. For example, you send some data and then you wait for a response... does that Listener object guarantee you're going to get the entire response in one call? Or might you get the response in two parts, or even one byte at a time?

My feeling is that declaring the serial connection inside the "sendBytes" method is a bad idea. Seems to me that you're only going to have one connection to your serial device for the whole time the program you're running, aren't you? So the process would look like this: Send bytes to the serial connection, read bytes from the serial connection until you have everything you need. Hopefully you can avoid using that Listener because it's going to make you have to write some ugly multi-threaded code if you're stuck with it.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul Clapham, E Armitage, Dave Tolls, Henry Wong,

Sorry for the late reply, i was unable to connect to internet for the past few days.
Thanks for the help and solution.
It works successfully with the use of "SwingWorker"

Kindly find the sample code below which i have implemented. Thanks everyone.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic