

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
9 messages in net.java.dev.jna.usersRe: AW: AW: AW: [jna-users] Callback| From | Sent On | Attachments |
|---|---|---|
| Novatchkov Hristo | Mar 19, 2009 6:59 am | |
| Timothy Wall | Mar 19, 2009 7:08 am | |
| Novatchkov Hristo | Mar 19, 2009 8:19 am | |
| Timothy Wall | Mar 19, 2009 8:52 am | |
| Novatchkov Hristo | Mar 19, 2009 11:40 am | |
| Timothy Wall | Mar 19, 2009 1:02 pm | |
| Novatchkov Hristo | Mar 20, 2009 2:54 am | |
| Timothy Wall | Mar 20, 2009 5:59 am | |
| Hristo Novatchkov | Mar 22, 2009 5:30 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: AW: AW: AW: [jna-users] Callback | Actions... |
|---|---|---|
| From: | Hristo Novatchkov (hris...@univie.ac.at) | |
| Date: | Mar 22, 2009 5:30:52 am | |
| List: | net.java.dev.jna.users | |
I'm sorry for disturbing you again, but for some reason I'm not able to realize the callback functionality correctly. I'm trying to load a native library (ANT_DLL.dll), which implements wireless connection networks for sending and receiving sensor data between different network nodes. For such purposes the library includes a bunch of functions, amongst others for: 1) initializing the module (_ANT_Init()) 2) defining and opening channels between network nodes (_ANT_AssignChannel(), _ANT_SetChannelId (), _ANT_SetChannelRFFreq()) 3) processing incoming events (which has a callback function as parameter) (_ANT_AssignChannelEventFunction()) ...
In general, when I load the dll and call all that functions in my java program, the native calls - apart of the callback function - work fine. This can be seen by the fact that when there is a sensor module available (which is constantly sending data), the dll instantiates the creation of a protocol file, in which the received data is constantly written too. Though, when I try to receive the data also via the callback function of my program, I am facing some problems. Basically, I don't think that my callback method is ever called when some data is received since the testing printouts are never printer to the console. I have attached the source code of the java program and would be really grateful if somebody can tell me what I am doing wrongly. Thanks a million! P.S. In the documentation it is explicitly mentioned that the callback function has to be a C function. Though, if I have understood everything correctly, it shouldn't be a problem to realize it via the JNA callback functionality?
Timothy Wall schrieb:
The object that your create which implements the Callback interface is associated with a native function pointer, which is passed to the native function which registers the callback. At some future point, the native code will invoke the function pointer, which knows how to transfer control back to Java to the Callback object's callback method.
The specific semantics of what the callback parameters mean, or other parameters used when registering the callback, depend on the native API.
On Mar 20, 2009, at 5:55 AM, Novatchkov Hristo wrote:
I understand the general idea, though, the actual realization for such callback issues is not clear to me. In general, I'm still trying to understand what exactly happens when I call the method with one of the parameters creating a new callback object and also defining the callback method. For example is that parameter always executed (and thus a new callback object created)? Or just if there was some data received? But what happens if there is no data and the method is called?
-----Ursprüngliche Nachricht----- Von: Timothy Wall [mailto:twal...@dev.java.net] Gesendet: Donnerstag, 19. März 2009 21:03 An: use...@jna.dev.java.net Betreff: Re: AW: AW: [jna-users] Callback
I mean you have to keep a live reference to it. Whether that's in a class static variable or an instance field on an object you know won't be GC'd or something else doesn't really matter.
On Mar 19, 2009, at 2:40 PM, Novatchkov Hristo wrote:
By keeping a reference of the callback object, you mean that one has to define a global variable, which should then reference the aucResponseBuf Memory object (in my case that would happen in the second switch)? So something like:
//global class definition Memory save; ... //callback function definition ... // switch(ucChannel) save = aucResponseBuf; ...
package mobile.coaching;
import com.sun.jna.Callback; import com.sun.jna.Library; import com.sun.jna.Memory; import com.sun.jna.Native; import com.sun.jna.Platform;
public class InvokeNative {
//channel declaration private final byte channel = (byte) 0; //events definitions final byte EVENT_RX_ACKNOWLEDGED = (byte) 0x9B; final byte EVENT_RX_BROADCAST = (byte) 0x9A; final byte EVENT_RX_FAIL = (byte) 0x02; final byte EVENT_RX_SEARCH_TIMEOUT = (byte) 0x01;
//native object declaration private ANT_MOD ant_mod;
//buffer in which the received data is written final Memory aucResponseBuf = new Memory(13);
//callback object reference private static CHANNEL_EVENT_FUNCTION cef;
//declare DLL object public interface ANT_MOD extends Library {
//declare methods from DLL file
public boolean _ANT_Init(byte deviceNum, short usBaudrate);
public boolean _ANT_ResetSystem();
public boolean _ANT_AssignChannel(byte ucChannel, byte ucChannelType, byte
ucNetworkNumber);
public boolean _ANT_SetChannelId(byte ucChannel, short usDeviceNum, byte
ucDeviceType, byte ucTransmisssionType);
public boolean _ANT_SetChannelRFFreq(byte ucChannel, byte ucRFFreq);
public boolean _ANT_SetNetworkKey(byte ucNetNumber, String usBaudrate);
public boolean _ANT_OpenChannel(byte ucChannel);
//function with callback function parameter
public boolean _ANT_AssignChannelEventFunction(byte ucChannel,
CHANNEL_EVENT_FUNCTION pfChannelEvent, Memory aucResponseBuf);
public void _ANT_Close(); }
//declare callback object and function public interface CHANNEL_EVENT_FUNCTION extends Callback { public boolean callback(byte ucChannel, byte ucEvent); }
//STEP 1: method to be called to initialize object by calling native library public boolean initANT() { boolean check;
//load object ant_mod = (ANT_MOD) Native.loadLibrary("ANT_DLL", ANT_MOD.class);
byte deviceNum = (byte) 0; short usBaudrate = (short) 50000;
//initialize ANT module, works fine check = ant_mod._ANT_Init(deviceNum, usBaudrate); return check; }
// STEP 2: Callback object/function definitions and call as parameter // to be called before opening the channel public boolean checkEvent() {
// callback object is still null System.out.println(cef);
cef = new CHANNEL_EVENT_FUNCTION() {
public boolean callback(byte ucChannel, byte ucEvent) {
switch(ucEvent) { case EVENT_RX_ACKNOWLEDGED: System.out.println("Acked\n"); break; case EVENT_RX_BROADCAST: switch(ucChannel) { case channel:
save = aucResponseBuf; System.out.println("save" + save);
case EVENT_RX_FAIL: System.out.println("RX Fail"); break; case EVENT_RX_SEARCH_TIMEOUT: System.out.println("Search Timeout"); break; default: System.out.println("Other event" + ucEvent); break; } return true;
} };
boolean checkEvent = ant_mod._ANT_AssignChannelEventFunction(channel, cef,
aucResponseBuf);
// callback object is not null anymore // printout: mobile.coaching.InvokeNative$1@71727172 System.out.println(cef); return checkEvent; }
//STEP 3: open channel in order to receive constantly data public boolean openChannel() { byte channelType = (byte) 0x00; byte networkNumber = (byte) 0;
boolean checkAssign = ant_mod._ANT_AssignChannel(channel, channelType,
networkNumber);
//byte channel = (byte) 0; short usDNum = (short) 0; byte uscDType = (byte) 0; byte uscTType = (byte) 0;
boolean checkSet = ant_mod._ANT_SetChannelId(channel, usDNum, uscDType,
uscTType);
byte ucRFF = (byte) 66; ant_mod._ANT_SetChannelRFFreq(channel, ucRFF); boolean checkOpen = ant_mod._ANT_OpenChannel(channel);
if(checkAssign && checkSet && checkOpen) return true; else return false; }
public void closeAnt() {
ant_mod._ANT_Close();
}
}







