5

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).

4
  • I'm not fully understanding what you're asking. When you call the addPacketListener method as illustrated above, you pass in a new object (an anonymous inner class), whose processResponse method will be called when packets arrive. This is the callback - just fill in the "handle the response" part yourself. Commented Feb 21, 2012 at 0:36
  • @cutchin so I can write a separate MATLAB class that exports a method processResponse and pass an instance of that class to addPacketListener? I tried passing in a MATLAB object with a method processResponse and got the following error: XB.addPacketListener(testclass) No method 'addPacketListener' with matching signature found for class 'com.rapplogic.xbee.api.XBee' Commented Feb 21, 2012 at 0:39
  • I am not an expert at MATLAB integration, but I believe you are going about this backwards. Write your java code to fire up the XBee interface and receive packets first. Make sure it works from the command line and you can see packets arriving. From there, write a library (a .jar file) that you will import into matlib, and have it retrieve data from your class as described here. Commented Feb 21, 2012 at 0:47
  • I guess I should make sure you're using a fairly recent version of matlab > 2007aish Commented Feb 21, 2012 at 3:57

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You can definitely pass data back and forth from matlab to Java, quite easily in fact.
matlabcontrol is a great tool. Thanks!
-1

To interface MATLAB with Java follow the interface requirements in Bringing Java Classes and Methods into MATLAB Workspace.

Worst of it is to add the path to your Java jars classes to the javaclass path and then call the constructors, methods, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.