This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!

Brett Phagan

+ Follow
since Aug 05, 2016
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Brett Phagan

I have a USB Observable that notify's observers when one of our devices is connected or disconnected.
Currently, in the Update method, I check a field in USB to see if connected or disconnected.
If connected, I get the vendorID and productID fields from the USB class.
I then connect to the USB device and access data.

However, I feel like this is tightly coupled since I need to check fields and pass info back and forth.

My thought is to send the 3 pieces of info from the USB so I can create a new object to access the data to be more loosely coupled.

My issue is how to pass the 3 pieces of info to observer.
Boolean connectUSB
int vendorID
int productID

from what I can tell, only one Object argument can be sent to Observers.
Should I create a new Object that contains this info and pass that?
Or is there some other way to pass the info.
I use getters now, but this makes it seem like it's tightly coupled.

I could possibly pass the hid object, but I plan to add serial UART option instead of USB down the road.

I have my app running. but it is very tightly coupled and I am trying to loosen it up.

I am very much a greenhorn so please be gentle.

5 years ago
Nevermind, as I suspected it was a simple fix.
After some testing, I was able to get it working by adding the resources folder to the MANIFEST.MF.
5 years ago
Thanks for the help. I managed to get everything working fine when running the app from Eclipse.

I created an runnable jar file from this and it is not quite working.

The gui comes up but no usb connection.
When I run from cmd line with console enabled, I get a resource not found error.
See console text below.

I tried exporting several ways but similar issue.
When I open the jar file, I see a resources\win32-x86-64 folder with the hidapi.dll in it.
I am sure it is something simple I am overlooking, but I can't find it.
FYI, the jar file is on my desktop.

Manifest-Version: 1.0
Class-Path: .
Main-Class: mxUSB.MXBootTool

C:\Users\r12528\Desktop>java -jar iMX_Boot_Utility.jar
Launching application...
Operating System : Windows 7 6.1
Java Version: 1.8.0_91
Java Home: C:\Program Files\Java\jre1.8.0_91
Showing GUI.
Loading hidapi...
Exception in thread "AWT-EventQueue-0" org.hid4java.HidException: Hidapi did not
initialise: Unable to load library 'hidapi': Native library (win32-x86-64/hidap
i.dll) not found in resource path ([file:/C:/Users/r12528/Desktop/iMX_Boot_Utili
       at org.hid4java.HidDeviceManager.<init>(
       at org.hid4java.HidServices.<init>(
       at org.hid4java.HidServices.<init>(
       at org.hid4java.HidServices.<init>(
       at org.hid4java.HidManager.getHidServices(
       at mxUSB.MXConnectUSB.<init>(
       at mxUSB.MXBootTool.createAndShowGUI(
       at mxUSB.MXBootTool$
       at java.awt.event.InvocationEvent.dispatch(Unknown Source)
       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
       at java.awt.EventQueue.access$500(Unknown Source)
       at java.awt.EventQueue$ Source)
       at java.awt.EventQueue$ Source)
       at Method)
rivilege(Unknown Source)
       at java.awt.EventQueue.dispatchEvent(Unknown Source)
       at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
       at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
       at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
       at Source)

5 years ago
Ok. So I have been on and off this app the past couple weeks and have been trying to do some reading.

I have made progress on the USB side. I now have the pieces working that will montior USB HID connects and disconnects, and notify my main Class when it's one of our devices.
I have been able to read a lot of data from the hid device and process it into Strings, String Array[]s and String ArrayLists.

All the Strings are displaying nicely in console.

Now I want to display in gui. I have created a gui that has 1 Jlabel and 2 JTextAreas.
I modify the JLabel depending on if a board is connected or not.

Some info goes to one JTextArea, some info goes to the other.

I have the gui up, but not able to update the gui.

Not sure if it is some kind of threading issue or just not able to the gui.

Below is the code for my main class (I tried to remove unneed stuff) and the gui class.
At some point I will want to use the buttons in the gui, but not initially.

Any pointers would be greatly appreciated.


5 years ago
I did some reading and searching to try to get my head around this concept.
Most examples are 1 observable to many observers. I seem to need multiple observables with multiple observers and Board to be both.
I can get part of it when I have one Observable. I get twisted around when I try to use a second Observable.

I think I have the USB part. The hid4java comes with a HidServicesListener interface.

Here's where I confuse myself:
I can setup Board to listen for USB hid connect/disconnect.
I can setup Board to listen for Button presses on GUI.
So far so good.
Some button presses requires data to be sent/received from the USB device by Board. Can Board access the USB hid object from the button listener?
This last part is what I am unsure about.
And, Board needs to notify GUI whenever hid device is connected or disconnected to update GUI to reflect current connect status.
Can Board access the GUI from the USB Listener.

Board seems to be the traffic cop for everything. Listeners for USB and GUI. And be able to access the USB from GUI Listener and GUI from USB Listener.
Hope I am making sense.

I am trying to understand and use these properly without falling back to tightly coupling them like I did initially. Trying to do it better this time.

Thinking out loud here:
My thought is to separate the GUI into 2 pieces where JFrame is the Observable with buttons and the JPanels text areas are setup as Observers and added to the JFrame.
Then break up the Board class with an Observer as the starting point and create an Observable object within to send data to GUI Text areas. The object would also instantiate a USB hid object to allow talking to the device.
Unless it's possible for an object to be obseravle and observer at the same time.
Does that make sense or am I off base?


5 years ago
Thanks for pointing me in the direction.
I had run across a Lynda training "Foundations of Programming: Design Patterns" last night. It looked like what I wanted but wasn't sure until your reply. I will go through training and see if this works for me.
I will report back when I get through it.
Many thanks.
6 years ago
I am new to Java Programming and trying to figure out the best way to implement my app.
I have it working except for USB listener. I have to check for the USB device each time to see if connected or disconnected.
I have tried reading and searching but can't quite grasp this concept.

I have a GUI Class that has some button listeners. I am able to get this to work.
I have a USB Class (hid4java) that has listeners for devices connect and disconnect. This works as a separate app but not sure how to implement this into app.
I have my own Board Class that determines which device is connected and what data to send and how to process the received data to be displayed.

What works:
main in GUI, creates instance of Board and Board creates an instance of USB.
When app launched, opens gui and if board there, gets data and displays. Any button presses are handled properly, but USB needs to be tested every time since I currently don't have a listener running.

I want to add the USB listener.

I want the GUI to update itself with Board data whenever a USB device is connected or disconnected.
I want the same GUI to have buttons to trigger Board to retrieve data from USB device.

My dilemma is how to instantiate so each listener in USB and GUI Classes can respond and access the Board Class.
I am new to OO programming and figured out some of the basics. Not clear how I can have 3 objects that can access methods within each other.

Hopefully I explain correctly.

6 years ago