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

Windows Forms Discussion :

[C#] demande d'explication sur un sample msdn


Sujet :

Windows Forms

  1. #1
    Membre régulier Avatar de legillou
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    178
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 178
    Points : 93
    Points
    93
    Par défaut [C#] demande d'explication sur un sample msdn
    bonjour à tous

    Voila je cherche a comprendre un sample de la msdn concernant l'utilisation du port série (framework 2) et je m'interesse tout particulièrement à la lecture.

    Bon je vous ponds le code même si je sais que c'est un peu long

    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
    74
    75
    76
    77
    78
    private void Receive()
    {
    	try
    	{
    		byte[] receiveBuffer = new byte[128];
    		int bytesRead = 0;
    		int bufferIndex = 0;
    		int startPacketIndex = 0;
    		int expectedPacketLength = -1;
    		bool expectedPacketLengthIsSet = false;
    		int numBytesToRead = receiveBuffer.Length;
    		while (true)
    		{
    			if (expectedPacketLengthIsSet || 1 >= bytesRead)
    			{
    				//If the expectedPacketLength has been or no bytes have been read
    				//This covers the case that more then 1 entire packet has been read in at a time
    				// comPort
    				try
    				{
    					bytesRead += com.Read(receiveBuffer, bufferIndex, numBytesToRead);
    					bufferIndex = startPacketIndex + bytesRead;
    				}
    				catch (TimeoutException)
    				{
    					_timedOut = true;
    				}
    			}
     
    			if (1 < bytesRead)
    			{
    				//The buffer has the dataLength for the packet
    				if (!expectedPacketLengthIsSet)
    				{
     					//If the expectedPacketLength has not been set for this packet
    	 				expectedPacketLength = receiveBuffer[(1 + startPacketIndex) % receiveBuffer.Length] + 4;
    					expectedPacketLengthIsSet = true;
    				}
     
    				if (bytesRead >= expectedPacketLength)
    				{
    					//The buffer has atleast as many bytes for this packet
    	 				AddPacket(receiveBuffer, startPacketIndex);
    					expectedPacketLengthIsSet = false;
    					if (bytesRead == expectedPacketLength)
    					{
    						//The buffer contains only the bytes for this packet
    						bytesRead = 0;
    						bufferIndex = startPacketIndex;
    					}
    					else
    					{
    						//The buffer also has bytes for the next packet
    						startPacketIndex += expectedPacketLength;
    						startPacketIndex %= receiveBuffer.Length;
    						bytesRead -= expectedPacketLength;
    						bufferIndex = startPacketIndex + bytesRead;
    					}
    				}
    			}
     
    			bufferIndex %= receiveBuffer.Length;
    			numBytesToRead = bufferIndex < startPacketIndex ? startPacketIndex - bufferIndex : receiveBuffer.Length - bufferIndex;
    		}
    	}
    	catch (IOException)
    	{
    		// abort the thread
    		System.Threading.Thread.CurrentThread.Abort();
    	}
    	catch (ObjectDisposedException)
    	{
    		if (receiveThread != null)
    		{
    			receiveThread = null;
    		}
    	}
    }
    Je ne comprend pas grand chose a ce code je doit bien l'avouer mais j'espère que la question suivante va me debloquer quelque peu...

    Quel est le sens du caractère % dans la ligne suivante par exemple ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    expectedPacketLength = receiveBuffer[(1 + startPacketIndex) % receiveBuffer.Length] + 4;
    merci d'avance pour vos lumières

  2. #2
    Membre régulier
    Inscrit en
    Juin 2003
    Messages
    82
    Détails du profil
    Informations forums :
    Inscription : Juin 2003
    Messages : 82
    Points : 92
    Points
    92
    Par défaut
    Bonjour,

    Si je me trompe pas % correspond à l'opérateur MODULO c'est à dire le reste de la division entière.

    Exemple :

    8 % 4 = 0
    10 % 3 = 1 (3 * 3 et il reste 1)

    @+

  3. #3
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    oui oui % c'est bien le modulo : le reste de la division entière

Discussions similaires

  1. [filter2] Demande d'explication sur cette fonction
    Par ranell dans le forum Images
    Réponses: 3
    Dernier message: 10/05/2007, 09h19
  2. Demande d explication sur protocole FTP
    Par Mr_Chut dans le forum Réseau
    Réponses: 1
    Dernier message: 04/05/2007, 18h47
  3. Réponses: 4
    Dernier message: 09/10/2006, 22h12
  4. Réponses: 3
    Dernier message: 27/09/2006, 13h11
  5. [final]demande d'explication sur ce mot-clé
    Par Invité dans le forum Langage
    Réponses: 10
    Dernier message: 18/04/2006, 11h32

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