This Question is Answered

1 "correct" answer available (4 pts) 1 "helpful" answer available (2 pts)
3 Replies Last post: Nov 27, 2009 4:17 AM by Caleb Jeffery  
Caleb Jeffery   8 posts since
Nov 26, 2009
Currently Being Moderated

Nov 26, 2009 7:15 AM

JNIWrapper for USBXpress Driver

Hi I am trying to write a wrapper around the USBXpress driver SiUSBXp.dll

 

I have successfully been able to talk to one of the functions within this driver but I am unable to get the SI_Write function to work. below is the wrapper funciton

 

// function SI_Write (Handle: THandle; Buffer: LPVOID; NumBytesToWrite:
    // dword; var NumBytesWritten: dword; lpOverlapped: POverlapped):
    // TSI_Status; stdcall external 'SiUSBXp.dll';
    public static long SI_Write(Handle handle, Pointer Buffer_LPVOID, UInt32 int32, Pointer pointer,
            Void void1) {
        Function add = USBXPRESS_LIB.getFunction("SI_Write");
        ULongInt result = new ULongInt();
        Parameter[] params = { handle, Buffer_LPVOID, int32, int32, void1 };
        add.invoke(result, params);
        // add.invoke(result,Handle_THandle,Buffer_LPVOID,NumBytesToWrite_dword,NumBytesWritten_dword,lpOverlapped_POverlapped);
        return result.getValue();
    }

 

and here is how I call this function within my code.

 

public void write(String command) throws USBXpressException {
        AnsiString cmd = new AnsiString(command);
        Pointer cmdp = new Pointer(cmd);
        UInt32 bytesWritten = new UInt32();
        UInt32 numberOfBytesToWrite = new UInt32(cmdp.getAlignedLength());
        long  ss;
        ss=USBXpressDLL.SI_Write(this.handle, new Pointer.OutOnly(cmdp), numberOfBytesToWrite, new Pointer(bytesWritten), new Pointer.Void(0));
          //assert(bytesWritten=dword(length(s)));
          //this.write=bytesWritten;
          if(ss!=USBXpressDLL.SI_SUCCESS){ throw new USBXpressException("Error Writing");}
    }

 

the command that I am sending requires to have a chr 13 value sent at the end I am having a feeling that the driver is looking for this character.

when I look at the pointer size it does not reflect the orginal string size of the command being sent.

 

What am I doing wrong?

 

I have done some more debugging and have found that the code is returning and error of Invalid parameter so the jniwrapper is calling the function correctly but I am passing an invalid parameter.

how do I find out what parameter is incorrect

 

here is a excert from the SiUSBXp.dll programers guide

SI_STATUS SI_Write (HANDLE Handle, LPVOID Buffer, DWORD NumBytesToWrite,
DWORD *NumBytesWritten, OVERLAPPED* o = NULL)
Parameters: 1. Handle—Handle to the device to write as returned by SI_Open.
2. Buffer—Address of a character buffer of data to be sent to the device.
3. NumBytesToWrite—Number of bytes to write to the device (0–4096 bytes).
4. NumBytesWritten—Address of a DWORD which will contain the number of bytes actually
written to the device.
5. (Optional) o—Address of an initialized OVERLAPPED object that can be used for
asynchronous writes.

I am sure i have the first 4 parameters setup correctly but am not sure how to represent the 5 param. (This param is always set to nil )

Any suggestions?

Serge Piletsky TeamDev Ltd. 1,066 posts since
Apr 24, 2006
Currently Being Moderated
1. Nov 26, 2009 5:43 PM in response to: Caleb Jeffery
Re: JNIWrapper for USBXpress Driver

Hi Caleb,

 

Thank you for the detailed description of the issue and for the sample code.

when I look at the pointer size it does not reflect the orginal string size of the command being sent.

That could be an answer. In fact the Parameter.getAlignedLength() method is not supposed to be used in that context. If you need to get a length of a string then you should use its getLength() method, for example:

AnsiString cmd = new AnsiString(command);
Pointer cmdp = new Pointer(cmd);//  char*
UInt32 numberOfBytesToWrite = new UInt32(cmd.getLength());

Another possible issue is that you are trying to pass a 'cmdp' variable (which is the pointer to a command) via Pointer.Only parameter "new Pointer.OutOnly(cmdp)" in your code. But did not find any indication that the "Buffer" is an output parameter. So, it seems that you just need to remove that extra pointer:

USBXpressDLL.SI_Write(this.handle, cmdp, numberOfBytesToWrite, new Pointer(bytesWritten), new Pointer.Void());

 

Please try these suggestions and let me know the results.

 

-Serge

More Like This

  • Retrieving data ...