0

I call library native function OpenSSPComPortUSB() while running program get pop-up unbable to open the port \.\COM208. Why it return \.\COM208 as I have pass PortNumber =7; below is the Java code and my goal is to open port COM7 to start the serial communication with smart hoper.

    //  AccessITLLib.java


   public class AccessITLLib
    {


       public static class SSP_COMMAND extends Structure
       {
           SSP_FULL_KEY Key;
            public NativeLong BaudRate;
            public byte PortNumber;
                    public NativeLong Timeout;
                    public byte SSPAddress;
                    public byte RetryLevel;
                    public byte EncryptionStatus;
                    public byte CommandDataLength;
                    public byte[] CommandData = new byte[255];
                    public byte ResponseStatus;
                    public byte ResponseDataLength;
                    public byte[] ResponseData = new byte[255];
                    public byte IgnoreError;


       }



       public interface ITLLib extends Library
       {
           ITLLib INSTANCE = (ITLLib) Native.loadLibrary("ITLSSPproc",
                   ITLLib.class);

           public int OpenSSPComPort(SSP_COMMAND p);
           public int OpenSSPComPortUSB(SSP_COMMAND p);
           public int CloseSSPComPort();
           public int CloseSSPComPortUSB();
       }

       public static void main(String[] args)
       {
          SSP_COMMAND commandStruct = new SSP_COMMAND();
          commandStruct.BaudRate = new NativeLong(9600);
          commandStruct.PortNumber =7;
          commandStruct.Timeout = 500;
          commandStruct.RetryLevel = 3;
          commandStruct.IgnoreError = 1;
          commandStruct.SSPAddress =16;



          ITLLib.INSTANCE.CloseSSPComPort();
          ITLLib.INSTANCE.CloseSSPComPortUSB();
          ITLLib.INSTANCE.OpenSSPComPort(commandStruct);
          ITLLib.INSTANCE.OpenSSPComPortUSB(commandStruct);



       }
    }


 here are the c structures
    typedef struct{
        SSP_FULL_KEY Key;
        unsigned long BaudRate;
        unsigned long Timeout;
        unsigned char PortNumber;
        unsigned char SSPAddress;
        unsigned char RetryLevel;
        unsigned char EncryptionStatus;
        unsigned char CommandDataLength;
        unsigned char CommandData[255];
        unsigned char ResponseStatus;
        unsigned char ResponseDataLength;
        unsigned char ResponseData[255];
        unsigned char IgnoreError;
    }SSP_COMMAND;



    typedef struct{
        unsigned long long FixedKey;
        unsigned long long EncryptKey;
    }SSP_FULL_KEY;
  • Your baud rate is most certainly _not_ a 64-bit number; use `int` or `NativeLong` instead. Other references on the web seem to indicate that `PortNumber` is a `char`, which requires a `byte` field in JNA. Do you have a function/struct declaration from a C header file for comparison? – technomage Feb 06 '13 at 12:36
  • in c struct file baud rate is long and the PortNumber is char in JNA i hava change the BaudRate as NativeLong and PortNumber as byte but no change – AJAY MAHALLE Feb 06 '13 at 12:58
  • This might be a silly question, but have you tried using port number 6 instead of 7? I know nothing about this particular API but maybe the numeric identifier for ports counts from 0 rather than 1? – Ian Roberts Feb 06 '13 at 13:22

2 Answers2

1

This may not be an answer; but still gives you some suggestion.

When you try to connect to a PORT through a client; That port should be connected by the server program first and should listen for client.

So check that port is started by using the below command.

netstat -lpn | grep 8080

netstat -f is the command for windows

You'll get output something like this

tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 13098/java

Here my process id is 13098 and it is the process that is using port 8080

There is a possibility the port is started but firewall is blocking the client to attach and So you can stop iptable service to stop the firewall.

service iptables stop

If it is still not available to the client then that port is blocked at network level (e.g. Router)

How to know the port is blocked or not ?

Use telnet command from your client machine to know that port is listening or not.

telnet ip port (windows cmd)

We can find out a particular process is using what are all the ports root root 14223 14206 processName

lsof -p 14223 (process PID)

output:
processName 14223 root 5u IPv4 349373 0t0 UDP hostName.com:7575

IPv4 to say it is using ports and the port no is 7575

To make the port free from the server process; Kill the process using following command:

sudo kill 13098

Now port 8080 is free.

Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
  • thanks for reply but how to execute thiese commands in cmd as i am working on windows 32 bit system – AJAY MAHALLE Feb 06 '13 at 07:18
  • @AJAYMAHALLE most of these commands are linux based. only telnet command i gave is for windows. – Kanagavelu Sugumar Feb 06 '13 at 07:22
  • @AJAYMAHALLE is it your Server process is running under windows ?? and Client also Windows?? – Kanagavelu Sugumar Feb 06 '13 at 07:23
  • YES host is running on windows and the slave is the hardware device which is something like a LED connected to port COM7 – AJAY MAHALLE Feb 06 '13 at 07:30
  • Run your java program in debugging mode; and check which line(or why) the port no is coming is different? This might be becos of invalid initialization API or the way mentioning the port no is wrong in your API. – Kanagavelu Sugumar Feb 06 '13 at 07:57
  • @AJAYMAHALLE Also please try comment ITLLib.INSTANCE.CloseSSPComPort(); at the beginning of main() since what port this will close? It is new instance rit without any port assigned??. – Kanagavelu Sugumar Feb 06 '13 at 08:24
  • while debuging the value shows long BaudRate@0=2580 long PortNumber@8=7 and also printed the value showing BaudRate=9600;PortNumber=7; – AJAY MAHALLE Feb 06 '13 at 08:52
1

Your JNA structure definition doesn't match the layout of your native definition.

The order of fields in JNA must exactly match that of the native definition (most recent version of JNA actually requires that you implement a method that explicitly declares the field order).

You've inadvertently swapped the PortNumber and Timeout fields.

technomage
  • 9,861
  • 2
  • 26
  • 40
  • 1
    You'll want to compare `Structure.size()` with `sizeof(SSP_COMMAND)`; if they differ, you will need to make some adjustments to the JNA structure definition to ensure they match and that all the fields line up properly. – technomage Feb 08 '13 at 17:25
  • how to call these methods sizeof(SSP_COMMAND cmd) giving me error – AJAY MAHALLE Feb 09 '13 at 09:57
  • You should read [this question](http://stackoverflow.com/questions/3203162/what-does-sizeof-do) and all its responses. Note that use of `sizeof` is native code which requires compilation. – technomage Feb 09 '13 at 10:42