IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

[JNA] Soucis Signed char mavariable :1 dans des structures


Sujet :

Entrée/Sortie Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Février 2012
    Messages
    133
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2012
    Messages : 133
    Par défaut [JNA] Soucis Signed char mavariable :1 dans des structures
    Bonjour à tous,

    J'ai un soucis un peu bizarre, je trouve...

    Je vous mes pour commencer le code et je vais essayer de bien m'exprimer pour mon problème .

    D'abord mes Header en C à respecter pour ma part :

    datatypes.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
     
    #ifndef _DATATYPE_H_
    #define _DATATYPE_H_
    typedef signed char INT8;
    typedef short       INT16;
    typedef int         INT32;
    typedef unsigned char  UINT8;
    typedef unsigned short UINT16;
    typedef unsigned int   UINT32;
     
    typedef union {
    	INT32  i;
    	UINT32 u;
    	float  f;
    	char * s;
    } Value;
     
    /* Types d'IO */
    typedef enum { IOT_U1, IOT_U2, IOT_U8, IOT_I8, IOT_U16, IOT_I16, IOT_U32, IOT_I32, IOT_F32, IOT_STRING, IOT_MAX } IoTypeE;
     
    typedef struct {
    	UINT32 rxMsg, txMsg;
    	UINT16 rxError, rxDoublet, rxTimeout, comReset;
    } L2Statistic;
     
    #endif /* _DATATYPE_H_ */
    io_protovol.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
     
    #ifndef _IO_PROTOCOL_H_
    #define _IO_PROTOCOL_H_
    #include "datatypes.h"
    #include <stdio.h>
    #include <stdlib.h>
     
    #define MAX_BLOC    128
     
     
    /* Io Dynanmic state Description */
     
    typedef struct IoDyn {
    	UINT8 def:1, // io defined
    		init:1,  // when server: value received from client
    		invalid:1, //
    		overflow:1;
    	Value value;
    } IoDyn;
     
    /* Io Bloc Description */
    typedef struct {
    	IoTypeE iot;
    	UINT8   out:1;   
    	UINT32  ioAddress;
    	UINT8   ioCount;   
    	UINT8   ioPerAddress; 
    	UINT8	ioFunction; 
    	IoDyn * ioDynP;   
    } IoBloc;
     
    typedef struct {
    	UINT16 linkAddr;
    	UINT16 asduAddr; 
    	int port;        
    	// UINT8 txTrace:1;
    	IoBloc * ioBlocP[MAX_BLOC];
    	UINT8 active : 1,
    		init : 1;   
    	int blocReady; 
    	L2Statistic statistic;
     
    } RtuData;
     
    //Focntion de test
    void plop(RtuData *rtu);
    void plopStat(L2Statistic *stat);
    void plopValue(Value *value);
     
     
    #endif // _IO_PROTOCOL_H_
    a.c
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    #include "io_protocol.h"
     
    void plop(RtuData *rtu){
        int i = 0;
        while(i<100){
            printf("\n out : %%s    byte : %d ",rtu->statistic->rxMsg);
            i++;
        }
        printf("\n\n");
    }
    void plopStat(L2Statistic *stat){
        printf("\n\n stat : %d \n",stat->rxMsg);
    }
    void plopValue(Value *value){
        printf("\n\n Value : %s",value->s);
    }
    /*int main(void){
        RtuData a;// = malloc(sizeof(RtuData));
        int i  = 0;
        while(i<100){
            a.ioBlocP[i] = malloc(sizeof(IoBloc));
            if(i%2 == 0)a.ioBlocP[i]->out = 1;
            else a.ioBlocP[i]->out = 0;
            a.ioBlocP[i]->ioDynP = malloc(sizeof(IoDyn));
            a.ioBlocP[i]->ioDynP->value.s = "plopa";
            i++;
        }
     
        i=0;
        while(i<100){
            printf("\n p : %d %s",a.ioBlocP[i]->out,&a.ioBlocP[i]->ioDynP->value.s);
            free(a.ioBlocP[i]->ioDynP);
            free(a.ioBlocP[i]);
            i++;
        }
        return 1;
    }*/
    Donc mtn mon équivalent en Java

    datatype.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.bastin.myproject.protocol.interfaces;
     
    /**
     *
     * @author Entreprise
     */
    public interface DataTypes extends com.sun.jna.Library{
     
        public static class Value extends com.sun.jna.Union{
            public static class ByValueValue extends Value implements com.sun.jna.Union.ByValue{ };
            public int i;
            public int u;
            public float f;
            public String s = null;
            public static class ByReferenceValue extends Value implements com.sun.jna.Union.ByReference{ };
        }
     
        /**
            * 
            * Types d'IO 
            */
        public interface IoTypeE{
            public static final int IOT_U1 = 1;
            public static final int IOT_U2 = 2;
            public static final int IOT_U8 = 3;
            public static final int IOT_I8 = 4;
            public static final int IOT_U6 = 5;
            public static final int IOT_I6 = 6;
            public static final int IOT_U32 = 7;
            public static final int IOT_I32 = 8;
            public static final int IOT_F32 = 9;
            public static final int IOT_STRING = 10;
            public static final int IOT_MAX = 11;
        }
     
        public static class L2Statistic extends com.sun.jna.Structure{
            public static class ByStatisticValue extends L2Statistic implements com.sun.jna.Structure.ByValue{ };
            public int rxMsg, txMsg;
            public short rxError, rxDoublet, rxTimeout, comReset;
            public static class ByStatisticReference extends L2Statistic implements com.sun.jna.Structure.ByReference{ };
        }    
    }
    StructureProtocol.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.bastin.myproject.protocol.interfaces;
     
    import com.bastin.myproject.protocol.interfaces.DataTypes.L2Statistic.ByStatisticReference;
    import com.bastin.myproject.protocol.interfaces.DataTypes.Value.ByReferenceValue;
    import com.bastin.myproject.protocol.interfaces.StructureProtocol.IoBloc.ByReferenceIoBloc;
    import com.bastin.myproject.protocol.interfaces.StructureProtocol.IoDyn.ByReferenceIoDyn;
    import com.bastin.myproject.protocol.interfaces.StructureProtocol.RtuData.ByReferenceRtuData;
     
    /**
     *
     * @author Entreprise
     */
    public interface StructureProtocol extends com.sun.jna.Library{
     
        public static final int MAX_BLOC = 128;
     
        /* 
         * 
         * Io Dynanmic state Description
         */
     
        public static class IoDyn extends com.sun.jna.Structure implements DataTypes{
            public static class ByValueIoDyn extends IoDyn implements com.sun.jna.Structure.ByValue { };
     
            public boolean def, init,invalid,overflow;
            public ByReferenceValue value = new ByReferenceValue();
     
            public static class ByReferenceIoDyn extends IoDyn implements com.sun.jna.Structure.ByReference {
            };         
        }
     
        /* 
         * 
         * Io Bloc Description 
         */
        public static class IoBloc extends com.sun.jna.Structure{
            public static class ByIoBloc extends IoBloc implements com.sun.jna.Structure.ByValue { };
            public int iot = DataTypes.IoTypeE.IOT_U8;
            public boolean out;
            public int ioAddress = 0;
            public byte ioCount = 0;
            public byte ioPerAddress = 0;
            public byte ioFunction = 0;
            public ByReferenceIoDyn ioDynP = new ByReferenceIoDyn();
            public static class ByReferenceIoBloc extends IoBloc implements com.sun.jna.Structure.ByReference {
            };
        }        
     
        public static class RtuData extends com.sun.jna.Structure{  
            public static class ByRtuData extends RtuData implements com.sun.jna.Structure.ByValue { };
            public short linkAddr; 
    	public short asduAddr; 
    	public int port; 
    	public ByReferenceIoBloc []ioBlocP = new ByReferenceIoBloc[MAX_BLOC];
    	public boolean active, init;  
    	public int blocReady; // 0: start general control; >0 general control in progress; -1 completed
    	public ByStatisticReference statistic = new ByStatisticReference(); 
            public static class ByReferenceRtuData extends RtuData implements com.sun.jna.Structure.ByReference {
            };
        }      
     
        public void plop(ByReferenceRtuData rtu);
        public void plopStat(ByStatisticReference stat);
        void plopValue(ByReferenceValue value);
     
        public interface WinStrucutreProtocol extends StructureProtocol{
        } 
    }
    ManagementProtocol.java

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.bastin.myproject.protocol.management;
     
    import com.bastin.myproject.protocol.interfaces.StructureProtocol;
    import com.bastin.myproject.protocol.interfaces.StructureProtocol.RtuData.ByReferenceRtuData;
    import com.sun.jna.Native;
    import com.sun.jna.Platform;
     
    /**
     *
     * @author Entreprise
     */
    public class ManagmentProtocol {
        static {       
            System.setProperty("jna.library.path","Protocols"); 
        }
        private StructureProtocol lib;
     
        public ManagmentProtocol(){
            this.lib = (StructureProtocol) Native.loadLibrary("Structure",Platform.isWindows() ? StructureProtocol.WinStrucutreProtocol.class : StructureProtocol.class);
        }
     
        public StructureProtocol getLib() {
            return lib;
        }
     
        public static void main(String[] args){
            ManagmentProtocol m = new ManagmentProtocol();
     
            ByReferenceRtuData r = new StructureProtocol.RtuData.ByReferenceRtuData();
            r.active = true;
            for(int i = 0; i<100; i++){
                r.ioBlocP[i] = new StructureProtocol.IoBloc.ByReferenceIoBloc();
                if(i % 2 == 0)r.ioBlocP[i].out = false;
                else r.ioBlocP[i].out = true;
                r.ioBlocP[i].ioCount = 11;
                //System.out.println("  ici :" + r.ioBlocP[i].out);
                r.ioBlocP[i].ioDynP.value.s = "ploooy";
                r.ioBlocP[i].ioDynP.value.setType(String.class);
                r.ioBlocP[i].ioDynP.def = true;
            }
            r.statistic.rxMsg = 12;
            m.getLib().plop(r);
        }  
    }
    Voilà, si il vous faut une explication sur quoi que ce soit, pas de soucis ^^.

    Alors voilà, comme vous pouvez le voir dans le Main, je crée une instance de RtuData et de là je sais atteindre toutes les variables que je veux et utiliser ses paramètres à partir de fonction contenue dans une DLL ou SO comme par exemple le paramètre r.ioBlocP[i].ioCount.

    Je sais donc tout faire sauf utiliser la structure 'L2Statistic' et l'union 'Value'.
    Si côté C, c'est unevaleur, j'obtient l'affichage par défaut 0 pour int ou null pour String et si je le mes en pointeur j'ai une erreur/crash de JRE...
    Donc en conclusion les valeurs ne passent pas.

    Mais par contre la fonction plopStat et PlopValue marche très bien et le main en C marche aussi très bien...Donc erreur à mon avis dans le code Java...

    Alors voilà pourquoi Value et L2Statistic ne fonctionne-t-il pas alors que IoDyn par exemple marche parfaitement!?

    Voilà si vous pouvez m'aider et je m'excuse si je ne suis pas très clair dans mes propos.

    Cordialement John[BE],

  2. #2
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Février 2012
    Messages
    133
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2012
    Messages : 133
    Par défaut
    Re-bonjour,

    J'ai avancé sur mon problème. cela vient des paramètres 'signed char name :1;'

    En fait dès qu'il y en a plusieurs ça fous le bordel ^^. Le premier influence sur les autres...

    Si il n'y en a que un ca marche nikel....donc c'est un problème de lecture de bit, je pense...

    Pour avancer, j'ai déplacé les variable de ce genre dans le fond des structures en C et Java pour que cela n'interfère plus pour l'instant sur les autres données.

    Structure RtuData c:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    typedef struct {
    	UINT16 linkAddr; // all protocols
    	UINT16 asduAddr; // protocol cei10x
    	int port;        // communication port
    	// UINT8 txTrace:1;
    	IoBloc * ioBlocP[MAX_BLOC]; // could be dynamic for client USC/TGAM
    // dynamic
    	int blocReady;
    	L2Statistic statistic;
        UINT8 active : 1;
    	UINT8 init : 1;
    	//void * protocolDP; // protocol specific
    } RtuData;
    Structure RtuData Java:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
        public static class RtuData extends com.sun.jna.Structure{  
            public static class ByRtuData extends RtuData implements com.sun.jna.Structure.ByValue { };
            public short linkAddr; // all protocols
    	public short asduAddr; // protocol cei10x
    	public int port;        // communication 
    	public ByReferenceIoBloc []ioBlocP = new ByReferenceIoBloc[MAX_BLOC];
            // dynamic
    	public int blocReady; 
    	public ByStatisticValue statistic = new ByStatisticValue(); 
    	public boolean active;
            public boolean init; 
            public static class ByReferenceRtuData extends RtuData implements com.sun.jna.Structure.ByReference {
            };
        }
    De même pour la structure IoDyn.

    Mtn il faut que je trouve comment résoudre se problème...

Discussions similaires

  1. des objets dans des structures
    Par dhbmedanis dans le forum Débuter
    Réponses: 9
    Dernier message: 25/05/2012, 16h56
  2. Charger les infos d'un fichier dans des structures
    Par Sa3k_ dans le forum Débuter
    Réponses: 4
    Dernier message: 13/06/2011, 20h25
  3. divers soucis dans des procedures qui paraissent simple
    Par aucunexperience dans le forum Macros et VBA Excel
    Réponses: 12
    Dernier message: 17/11/2009, 19h57
  4. Soucis d'affichage dans des fenêtres de W 2000 Pro
    Par ALT dans le forum Windows 2000/Me/98/95
    Réponses: 3
    Dernier message: 17/09/2008, 14h33
  5. Réponses: 6
    Dernier message: 27/10/2006, 14h27

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo