langage C - PB unsigned char

langage C - PB unsigned char - C - Programmation

Marsh Posté le 27-03-2013 à 21:05:13    

Bonjour,  
 
je debute en c.  
Je recois des donnees d'une liaison rs232 que je mets dans une variable unsigned pour le traitement.  
quand je teste si la variable est vide, ca me repond tout le temps non pourquoi?  
 

Code :
  1. //declaration
  2. unsigned char tampon[256];
  3. unsigned char data[256];
  4. int size;
  5. UARTRead(portrs232, data, size); //lecture de la donnee rs232
  6. memcpy(tampon, data, size); //copy dans le tampon
  7. int tamponRempli = sizeof(tampon); // taille du tampon  
  8. if(tamponRempli <=0)
  9. {
  10.     //traitement si vide;
  11.     printf("tampon vide" );
  12. }
  13. else {
  14.     //traitement si plein;
  15.     printf("tampon plein" );
  16.     //vider le tampon
  17.     //comment vider ?
  18. }


 
tampon est toujours plein, pourquoi ??  :(  
meme si je ne recois pas de donnees (au depart)  
tampon n'est pas vide  
comment verifier la taille du tampon ?  
ou et le reinitailiser a vide
 
 
 
merci

Reply

Marsh Posté le 27-03-2013 à 21:05:13   

Reply

Marsh Posté le 27-03-2013 à 21:35:28    

Bonsoir !
 
Le sizeof renverra toujours la même valeur, c'est à dire 256. La valeur renvoyée correspond à la taille, en octets, de la variable tampon.
 
Dans votre cas, vous ne pourrez avoir cette opération qu'en testant la valeur de retour de UARTRead, qui, si elle respecte l'état de l'art des fonctions "read", devrait renvoyer le nombre d'octets effectivement lus.
 
Le réinitialiser à vide ne sert pas à grand chose, mais vous pourriez le faire avec un "memset(tampon, 0, 256);", qui remettrait tous les octets à zéro, mais n'apporterait rien de plus au déroulement du programme.
Bonne continuation !

Reply

Marsh Posté le 27-03-2013 à 21:36:16    

Mouais, effectivement erreur de débutant...
 
Je ne connais pas vraiment le prototype de la fonction UARTRead(), mais je parie ce que tu veux qu'elle doit retourner un entier. C'est cet entier qui te dis si tu as reçu ou non quelque chose. L'instruction "sizeof" te donne la taille en octet qu'occupe ta variable en mémoire (peu importe ce qu'elle contient), taille connue à la compilation, donc constante. Dans ton cas, ça vaut 256, ce qui fait que ta condition juste après sera toujours fausse.
 
Edit: [:bouchon2]


Message édité par tpierron le 27-03-2013 à 21:37:12
Reply

Marsh Posté le 27-03-2013 à 21:50:22    

Merci
 
je viens de comprendre
j'initailise ma variable maintenant

Code :
  1. unsigned char tampon[256] = {0}


 
et je vais tester si la premiere valeur vaut 0
 
merci bcp :)

Reply

Marsh Posté le 28-03-2013 à 12:07:13    

Ta fonction UARTRead(portrs232, data, size), je ne connais pas son prototype, mais si c'est similaire à ce qui se passe sur d'autres systèmes, c'est ainsi que ça marche:
int UARTRead(device *dev, unsigned char buffer, int len)
On lui passe en paramètre le tampon (buffer) ou écrire les données lues sur le device, et la taille maximale de données inscriptibles.
Et la valeur retournée est le nombre d'octets de données écrites.
 
Et ça s'emploie plus ou moins ainsi
int read = 0;
unsigned char buffer[256];
read = UARTRead(portrs232, buffer, int sizeof(buffer));
if (read < 0) { /* une erreur en lecture à gérer */ }
else if (read == 0) { /* rien à lire - buffer vide */ }
else { /* on a lu read octets, les traiter, et faire une boucle de lecture pour lire la suite si read est maximal, ie vaut 256 */ }
 
Et il n'y a aucune raison de réinitialiser le tampon a vide, à chaque lecture, on écrase ce qui est dans le tampon avec les nouveaux octets lus.
 
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 29-04-2013 à 17:42:06    

je vais faire l'installation de la librairie MesaLib ecrtitr en c  
 
lorsque  Je lance la commande make pour la compilation j'obtien l'eureur suivante :
usr/include/drm/nouveau_pushbuf.h: In function ‘OUT_RING’:
/usr/include/drm/nouveau_pushbuf.h:72: error: ‘struct nouveau_channel’ has no member named ‘cur’
/usr/include/drm/nouveau_pushbuf.h: In function ‘AVAIL_RING’:
/usr/include/drm/nouveau_pushbuf.h:93: error: ‘struct nouveau_channel’ has no member named ‘end’
/usr/include/drm/nouveau_pushbuf.h:93: error: ‘struct nouveau_channel’ has no member named ‘cur’
/usr/include/drm/nouveau_pushbuf.h: In function ‘WAIT_RING’:
/usr/include/drm/nouveau_pushbuf.h:100: error: ‘struct nouveau_channel’ has no member named ‘cur’
/usr/include/drm/nouveau_pushbuf.h:100: error: ‘struct nouveau_channel’ has no member named ‘end’
/usr/include/drm/nouveau_pushbuf.h: In function ‘OUT_RELOC’:
/usr/include/drm/nouveau_pushbuf.h:114: error: ‘struct nouveau_channel’ has no member named ‘cur’
/usr/include/drm/nouveau_pushbuf.h: In function ‘OUT_RELOC2’:
/usr/include/drm/nouveau_pushbuf.h:124: error: ‘struct nouveau_channel’ has no member named ‘cur’
make[6]: *** [nouveau_screen.o] Error 1
 
avec la la contenu des fcichiers suivant :
 
------------- nouveau_pushbhf.h-------------
 
#ifndef __NOUVEAU_PUSHBUF_H__
#define __NOUVEAU_PUSHBUF_H__
 
#include <assert.h>
#include <string.h>
 
#include "nouveau/nouveau_channel.h"
#include "nouveau_bo.h"
#include "nouveau_grobj.h"
int
nouveau_pushbuf_flush(struct nouveau_channel *, unsigned min);
 
int
nouveau_pushbuf_marker_emit(struct nouveau_channel *chan,
       unsigned wait_dwords, unsigned wait_relocs);
 
void
nouveau_pushbuf_marker_undo(struct nouveau_channel *chan);
 
int
nouveau_pushbuf_emit_reloc(struct nouveau_channel *, void *ptr,
      struct nouveau_bo *, uint32_t data, uint32_t data2,
      uint32_t flags, uint32_t vor, uint32_t tor);
 
int
nouveau_pushbuf_submit(struct nouveau_channel *chan, struct nouveau_bo *bo,
         unsigned offset, unsigned length);
 
/* Push buffer access macros */
static __inline__ int
MARK_RING(struct nouveau_channel *chan, unsigned dwords, unsigned relocs)
{
 return nouveau_pushbuf_marker_emit(chan, dwords, relocs);
}
 
static __inline__ void
MARK_UNDO(struct nouveau_channel *chan)
{
 nouveau_pushbuf_marker_undo(chan);
}
 
static __inline__ void
OUT_RING(struct nouveau_channel *chan, unsigned data)
{
 *(chan->cur++) = (data);
}
 
static __inline__ void
OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned size)
{
 //memcpy(chan->cur, data, size * 4);
 //chan->cur += size;
}
 
static __inline__ void
OUT_RINGf(struct nouveau_channel *chan, float f)
{
 union { uint32_t i; float f; } c;
 c.f = f;
 OUT_RING(chan, c.i);
}
 
static __inline__ unsigned
AVAIL_RING(struct nouveau_channel *chan)
{
 return chan->end - chan->cur;
//return NULL;
}
 
static __inline__ void
WAIT_RING(struct nouveau_channel *chan, unsigned size)
{
 if (chan->cur + size > chan->end)
  nouveau_pushbuf_flush(chan, size);
}
 
static __inline__ void
FIRE_RING(struct nouveau_channel *chan)
{
 nouveau_pushbuf_flush(chan, 0);
}
 
static __inline__ int
OUT_RELOC(struct nouveau_channel *chan, struct nouveau_bo *bo,
   unsigned data, unsigned flags, unsigned vor, unsigned tor)
{
 return nouveau_pushbuf_emit_reloc(chan, chan->cur++, bo,
       data, 0, flags, vor, tor);
//return NULL;
}
 
static __inline__ int
OUT_RELOC2(struct nouveau_channel *chan, struct nouveau_bo *bo,
    unsigned data, unsigned data2, unsigned flags,
    unsigned vor, unsigned tor)
{
 return nouveau_pushbuf_emit_reloc(chan, chan->cur++, bo,
       data, data2, flags, vor, tor);
//return NULL;
}
 
/* Raw data + flags depending on FB/TT buffer */
static __inline__ int
OUT_RELOCd(struct nouveau_channel *chan, struct nouveau_bo *bo,
    unsigned data, unsigned flags, unsigned vor, unsigned tor)
{
 return OUT_RELOC(chan, bo, data, flags | NOUVEAU_BO_OR, vor, tor);
}
 
/* FB/TT object handle */
static __inline__ int
OUT_RELOCo(struct nouveau_channel *chan, struct nouveau_bo *bo,
    unsigned flags)
{
 return OUT_RELOC(chan, bo, 0, flags | NOUVEAU_BO_OR,
    chan->vram->handle, chan->gart->handle);
}
 
/* Low 32-bits of offset */
static __inline__ int
OUT_RELOCl(struct nouveau_channel *chan, struct nouveau_bo *bo,
    unsigned delta, unsigned flags)
{
 return OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_LOW, 0, 0);
}
 
/* Low 32-bits of offset + GPU linear access range info */
static __inline__ int
OUT_RELOCr(struct nouveau_channel *chan, struct nouveau_bo *bo,
    unsigned delta, unsigned size, unsigned flags)
{
 return OUT_RELOC2(chan, bo, delta, size, flags | NOUVEAU_BO_LOW, 0, 0);
}
 
/* High 32-bits of offset */
static __inline__ int
OUT_RELOCh(struct nouveau_channel *chan, struct nouveau_bo *bo,
    unsigned delta, unsigned flags)
{
 return OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_HIGH, 0, 0);
}
 
#endif
 
------------- nouveau_grobj.h------------
#define __NOUVEAU_GROBJ_H__
 
#include "nouveau_channel.h"
 
struct nouveau_grobj {
 struct nouveau_channel *channel;
 int grclass;
 uint32_t handle;
 
 enum {
  NOUVEAU_GROBJ_UNBOUND = 0,
  NOUVEAU_GROBJ_BOUND = 1,
  NOUVEAU_GROBJ_BOUND_EXPLICIT = 2
 } bound;
 int subc;
};
 
int nouveau_grobj_alloc(struct nouveau_channel *, uint32_t handle,
          int class, struct nouveau_grobj **);
int nouveau_grobj_ref(struct nouveau_channel *, uint32_t handle,
        struct nouveau_grobj **);
void nouveau_grobj_free(struct nouveau_grobj **);
void nouveau_grobj_autobind(struct nouveau_grobj *);
 
#endif
-----------------nouveau_chanel.h-------------------
 
#ifndef __NOUVEAU_CHANNEL_H__
#define __NOUVEAU_CHANNEL_H__
 
struct nouveau_subchannel {
 struct nouveau_grobj *gr;
 unsigned sequence;
};
 
struct nouveau_channel {
 uint32_t *cur;
 uint32_t *end;
 
 struct nouveau_device *device;
 int id;
 
 struct nouveau_grobj *nullobj;
 struct nouveau_grobj *vram;
 struct nouveau_grobj *gart;
 
 void *user_private;
 void (*hang_notify)(struct nouveau_channel *);
 void (*flush_notify)(struct nouveau_channel *);
 
 struct nouveau_subchannel subc[8];
 unsigned subc_sequence;
};
 
int
nouveau_channel_alloc(struct nouveau_device *, uint32_t fb, uint32_t tt,
        int pushbuf_size, struct nouveau_channel **);
 
void
nouveau_channel_free(struct nouveau_channel **);
 
#endif
 
merci d'avance de m'aider à resoudre cette probleme.
 

Reply

Marsh Posté le 29-04-2013 à 17:47:50    

Bonsoir ?
 
Et quel est le rapport avec le sujet initial ? Ne savez-vous pas lire la charte et ouvrir un nouveau sujet ?

Reply

Marsh Posté le 29-04-2013 à 17:48:44    

Merci d'éditer ton post pour mettre les balises de code adéquates, ou mieux : le supprimer et créer un sujet dédié.


---------------
last.fm
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed