Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hardware response VS Software response (Concurrency issue)  RSS feed

 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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

 
Dave Tolls
Ranch Hand
Posts: 2835
30
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23279
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Please Help
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!