Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Java interface in MATLAB

I am trying to write a MATLAB GUI that uses the XBee-API interface to talk to an XBee wireless radio on board an Arduino. The GUI mostly does data collection and parses incoming packets.

I want to do an addPacketListener as defined in the Developer's Guide (see section "Receiving Packets") to wait for packets to process. In Java, this is how it would be done:

xbee.addPacketListener(new PacketListener() {
    public void processResponse(XBeeResponse response) {
        // handle the response
    }
});

In MATLAB, I would do an addlistener() and set its callback to do my processing.

I don't know much at all about Java, so I was wondering if it's possible to set up the processResponse code a la MATLAB: can I do something like as follows:

function processResponse
    #% do response here
end 

>> xbee.addPacketListener(@processResponse)

I'm not sure if this made complete sense; basically what I'm trying to accomplish is to execute a callback once new packets are available for my XBee (which in Java is handled by addPacketListener).

like image 245
Dang Khoa Avatar asked Nov 05 '22 05:11

Dang Khoa


1 Answers

Let me know if I'm wrong, but it looks like you basically want to intercept XBee's calls to PacketListener.processResponse(XBeeResponse x) and have MATLAB process the contents of the incoming XBeeResponse object. I'm not much of a MATLAB guru, but is it even possible to implement a Java class using MATLAB code, and then pass that class back to Java? My guess is probably not, but I could be wrong.

I think the issue you're dealing with here stems from the fact that it's easy to make MATLAB calls on Java objects, but not the other way around. What I'd do is make a really simple implementation of PacketListener using Java code, and then use a third-party library like matlabcontrol to make function calls from your Java PacketListener implementation back out to MATLAB.

I had to deal with this issue on a large software project that required a GUI written in Java to make calls on back-end code written in MATLAB, so I can vouch for matlabcontrol's capabilities. Another important resource when controlling MATLAB from within Java, should you go this route, is undocumentedmatlab.com.

EDIT

One other thing to be wary of is threading on the Java side. You can have any number of Java threads making MatLab calls simultaneously, but MatLab will "sequentialize" all of the calls from Java. Let's say Java needs to call the MatLab functions "f1" and "f2". You set up two Java threads and each one calls either f1 or f2, simultaneously. MatLab will force one to wait for the other one to finish before starting the second function call, and it's completely indeterminate which one will actually get called first.

The reason I'm saying this is, whatever Java Thread calls the processResponse(XBeeResponse x) function will be blocked if it makes a call to MatLab and MatLab is already busy doing something else. It might be best to have your PacketListener cache incoming XBeeResponse instances so that XBee doesn't get stuck waiting for MatLab to process data. Just something to keep in mind.

like image 67
CodeBlind Avatar answered Nov 07 '22 21:11

CodeBlind