2 messages in net.java.dev.jna.usersRe: [jna-users] Structure and JNA
FromSent OnAttachments
Avinash KachhyMar 17, 2009 6:27 am 
Timothy WallMar 17, 2009 7:59 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Structure and JNAActions...
From:Timothy Wall (twal@dev.java.net)
Date:Mar 17, 2009 7:59:17 am
List:net.java.dev.jna.users

On Mar 17, 2009, at 9:28 AM, Avinash Kachhy wrote:

Hello

I corrected an error I had before so now I am getting the correct return codes from the DLL. But, I feel that I am still not passing the pointer to struct param correctly. Which may be causing the DLL function to fail. Below is the original C def of the function and the structure in the .H file. typedef struct _SETUPPR { CHAR Port[5]; WORD Baud; BYTE Parity; BYTE Data; BYTE Stop; } SETUPPR; typedef SETUPPR FAR *PSETUPPR; WORD WINAPI PRT_OPENCOM (PSETUPPR);

My Java code public class SetupPR extends Structure { public byte[] port = "COM1".getBytes();

This should be "COM1\0".getBytes(), which will give you 5 bytes. You can't use the String directly because that would result in a "char*" field in the structure instead of a "char[5]" field.

public short baud = 9600; public byte parity = 'N'; public byte data = 8; public byte stop = 1; } public interface PR2Interface extends Library { public static SetupPR setup = new SetupPR(); PR2Interface INSTANCE = (PR2Interface) Native.loadLibrary("wpr50nt", PR2Interface.class); public short PRT_OPENCOM(SetupPR spr); }

in Main() public static void main(String[] args) { short retval;

PR2Interface.INSTANCE.setup.port = "COM1".getBytes(); PR2Interface.INSTANCE.setup.data = 8; PR2Interface.INSTANCE.setup.parity = 'N'; PR2Interface.INSTANCE.setup.stop = 1;

retval = PR2Interface.INSTANCE.PRT_OPENCOM(PR2Interface.INSTANCE.setup); System.out.println("PRT_OPENCOM returned "+retval); }

The last line above prints the error code 610, which means some error in opening com port. It should have been 0. I do have a com1 port. I am using a USB to serial adapter and a com1 defined in Win Vista 32 bit. I wonder if there is anything I did wrong in the above code, in mapping to JNA that would cause the failure. I have tried the port as a String, too and it has the same result.

I asked this before but then I did not even get valid returns as I mapped the return code to a byte instead of short.