Version initiale
Dependencies: mbed
Fork of Le_Pont_V10116 by
Modbus.cpp@2:a10c8133d71c, 2018-03-29 (annotated)
- Committer:
- CS
- Date:
- Thu Mar 29 15:41:22 2018 +0000
- Revision:
- 2:a10c8133d71c
- Parent:
- 0:8b3c6f593515
Publish of "Le_Pont_V11003" in the team folder
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CS | 0:8b3c6f593515 | 1 | /***********************************************************************************/ |
CS | 0:8b3c6f593515 | 2 | // |
CS | 0:8b3c6f593515 | 3 | // Gestion du Modbus |
CS | 0:8b3c6f593515 | 4 | // |
CS | 0:8b3c6f593515 | 5 | //************************************************************************************/ |
CS | 0:8b3c6f593515 | 6 | |
CS | 0:8b3c6f593515 | 7 | #include <mbed.h> |
CS | 0:8b3c6f593515 | 8 | #include "Modbus.h" |
CS | 0:8b3c6f593515 | 9 | #include "Port_Serie.h" |
CS | 0:8b3c6f593515 | 10 | |
CS | 0:8b3c6f593515 | 11 | |
CS | 0:8b3c6f593515 | 12 | //volatile U8 Numero_Ordre_En_Cours_U8 ; |
CS | 0:8b3c6f593515 | 13 | volatile Ordre_Modbus_t Ordres[TAILLE_PILE_ORDRES] ; |
CS | 0:8b3c6f593515 | 14 | |
CS | 0:8b3c6f593515 | 15 | |
CS | 0:8b3c6f593515 | 16 | Serial PC7(USBTX, USBRX) ; |
CS | 0:8b3c6f593515 | 17 | |
CS | 0:8b3c6f593515 | 18 | /************ Initialisation du Modbus ************************/ |
CS | 0:8b3c6f593515 | 19 | void vModbus_Init(int Baudrate) |
CS | 0:8b3c6f593515 | 20 | { |
CS | 0:8b3c6f593515 | 21 | U8 Numero_Ordre_U8 ; |
CS | 0:8b3c6f593515 | 22 | |
CS | 0:8b3c6f593515 | 23 | vPort_Serie_Init( Baudrate ) ; |
CS | 0:8b3c6f593515 | 24 | |
CS | 0:8b3c6f593515 | 25 | for ( Numero_Ordre_U8 = 0 ; Numero_Ordre_U8 < TAILLE_PILE_ORDRES ; Numero_Ordre_U8++ ) |
CS | 0:8b3c6f593515 | 26 | { |
CS | 0:8b3c6f593515 | 27 | // Effacement des ordres |
CS | 0:8b3c6f593515 | 28 | Ordres[Numero_Ordre_U8].Etat_U8 = ARRET ; |
CS | 0:8b3c6f593515 | 29 | Ordres[Numero_Ordre_U8].Nb_Caracteres_Recus_U8 = 0 ; |
CS | 0:8b3c6f593515 | 30 | Ordres[Numero_Ordre_U8].Nb_Caracteres_A_Emettre_U8 = 0 ; |
CS | 0:8b3c6f593515 | 31 | Ordres[Numero_Ordre_U8].Code_Erreur = 0 ; |
CS | 0:8b3c6f593515 | 32 | } |
CS | 0:8b3c6f593515 | 33 | |
CS | 0:8b3c6f593515 | 34 | |
CS | 0:8b3c6f593515 | 35 | PC7.printf("\r\n Modbus : Init ") ; |
CS | 0:8b3c6f593515 | 36 | } |
CS | 0:8b3c6f593515 | 37 | |
CS | 0:8b3c6f593515 | 38 | |
CS | 0:8b3c6f593515 | 39 | /************* Demarrage du Modbus ****************************/ |
CS | 0:8b3c6f593515 | 40 | void vModbus_Start() |
CS | 0:8b3c6f593515 | 41 | { |
CS | 0:8b3c6f593515 | 42 | //Ordres[Numero_Ordre_En_Cours_U8].Etat_U8 = ATTENTE ; |
CS | 0:8b3c6f593515 | 43 | vPort_Serie_Ouvre() ; |
CS | 0:8b3c6f593515 | 44 | //vPort_Serie_Reception ( 0 ) ; |
CS | 0:8b3c6f593515 | 45 | PC7.printf("\r\n Modbus : Start ") ; |
CS | 0:8b3c6f593515 | 46 | } |
CS | 0:8b3c6f593515 | 47 | |
CS | 0:8b3c6f593515 | 48 | static const U8 aucCRCHi[] = { |
CS | 0:8b3c6f593515 | 49 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 50 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
CS | 0:8b3c6f593515 | 51 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 52 | 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 53 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 54 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
CS | 0:8b3c6f593515 | 55 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
CS | 0:8b3c6f593515 | 56 | 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
CS | 0:8b3c6f593515 | 57 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 58 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
CS | 0:8b3c6f593515 | 59 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 60 | 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 61 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 62 | 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 63 | 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 64 | 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 65 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 66 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
CS | 0:8b3c6f593515 | 67 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 68 | 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 69 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
CS | 0:8b3c6f593515 | 70 | 0x00, 0xC1, 0x81, 0x40 |
CS | 0:8b3c6f593515 | 71 | }; |
CS | 0:8b3c6f593515 | 72 | |
CS | 0:8b3c6f593515 | 73 | static const U8 aucCRCLo[] = { |
CS | 0:8b3c6f593515 | 74 | 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, |
CS | 0:8b3c6f593515 | 75 | 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, |
CS | 0:8b3c6f593515 | 76 | 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, |
CS | 0:8b3c6f593515 | 77 | 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, |
CS | 0:8b3c6f593515 | 78 | 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, |
CS | 0:8b3c6f593515 | 79 | 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, |
CS | 0:8b3c6f593515 | 80 | 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, |
CS | 0:8b3c6f593515 | 81 | 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, |
CS | 0:8b3c6f593515 | 82 | 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, |
CS | 0:8b3c6f593515 | 83 | 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, |
CS | 0:8b3c6f593515 | 84 | 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, |
CS | 0:8b3c6f593515 | 85 | 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, |
CS | 0:8b3c6f593515 | 86 | 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, |
CS | 0:8b3c6f593515 | 87 | 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, |
CS | 0:8b3c6f593515 | 88 | 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, |
CS | 0:8b3c6f593515 | 89 | 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, |
CS | 0:8b3c6f593515 | 90 | 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, |
CS | 0:8b3c6f593515 | 91 | 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, |
CS | 0:8b3c6f593515 | 92 | 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, |
CS | 0:8b3c6f593515 | 93 | 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, |
CS | 0:8b3c6f593515 | 94 | 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, |
CS | 0:8b3c6f593515 | 95 | 0x41, 0x81, 0x80, 0x40 |
CS | 0:8b3c6f593515 | 96 | }; |
CS | 0:8b3c6f593515 | 97 | |
CS | 0:8b3c6f593515 | 98 | |
CS | 0:8b3c6f593515 | 99 | U16 CRC16( U8 * pucFrame, U8 usLen ) |
CS | 0:8b3c6f593515 | 100 | { |
CS | 0:8b3c6f593515 | 101 | U8 ucCRCHi = 0xFF; |
CS | 0:8b3c6f593515 | 102 | U8 ucCRCLo = 0xFF; |
CS | 0:8b3c6f593515 | 103 | int iIndex; |
CS | 0:8b3c6f593515 | 104 | |
CS | 0:8b3c6f593515 | 105 | while( usLen-- ) |
CS | 0:8b3c6f593515 | 106 | { |
CS | 0:8b3c6f593515 | 107 | //PC7.printf("/ %X /", *pucFrame) ; |
CS | 0:8b3c6f593515 | 108 | iIndex = ucCRCLo ^ *( pucFrame++ ); |
CS | 0:8b3c6f593515 | 109 | ucCRCLo = ( U8 )( ucCRCHi ^ aucCRCHi[iIndex] ); |
CS | 0:8b3c6f593515 | 110 | ucCRCHi = aucCRCLo[iIndex]; |
CS | 0:8b3c6f593515 | 111 | } |
CS | 0:8b3c6f593515 | 112 | //PC7.printf("\n\r CRC: %X",(ucCRCHi << 8 | ucCRCLo)) ; |
CS | 0:8b3c6f593515 | 113 | return ( U16 )( ucCRCHi << 8 | ucCRCLo ); |
CS | 0:8b3c6f593515 | 114 | } |
CS | 0:8b3c6f593515 | 115 | |
CS | 0:8b3c6f593515 | 116 | /************* Traitement du Modbus ****************************/ |
CS | 0:8b3c6f593515 | 117 | void vModbus() |
CS | 0:8b3c6f593515 | 118 | { |
CS | 0:8b3c6f593515 | 119 | U8 Numero_Ordre_U8 ; |
CS | 0:8b3c6f593515 | 120 | U16 Valeur_U16 ; |
CS | 0:8b3c6f593515 | 121 | U8 Ordres_Arretes_U8 = 0 ; |
CS | 0:8b3c6f593515 | 122 | U8 Index_CRC_U8 ; |
CS | 0:8b3c6f593515 | 123 | U8 Index_Caractere_U8 ; |
CS | 0:8b3c6f593515 | 124 | U16 Index_Memoire_U16 ; |
CS | 0:8b3c6f593515 | 125 | |
CS | 0:8b3c6f593515 | 126 | for ( Numero_Ordre_U8 = 0 ; Numero_Ordre_U8 < TAILLE_PILE_ORDRES ; Numero_Ordre_U8++ ) |
CS | 0:8b3c6f593515 | 127 | { |
CS | 0:8b3c6f593515 | 128 | |
CS | 0:8b3c6f593515 | 129 | if ( Ordres[Numero_Ordre_U8].Etat_U8 == RECU ) |
CS | 0:8b3c6f593515 | 130 | {// Puisque la trame est complete, on peut la traiter |
CS | 0:8b3c6f593515 | 131 | //PC7.printf("\n\rOrdre : %i recu",Numero_Ordre_U8) ; |
CS | 0:8b3c6f593515 | 132 | if ( Numero_Ordre_U8 < ( TAILLE_PILE_ORDRES - 1 ) ) |
CS | 0:8b3c6f593515 | 133 | {// Enclenche l'ordre suivant |
CS | 0:8b3c6f593515 | 134 | vPort_Serie_Reception ( Numero_Ordre_U8 + 1 ) ; |
CS | 0:8b3c6f593515 | 135 | } |
CS | 0:8b3c6f593515 | 136 | else |
CS | 0:8b3c6f593515 | 137 | {// Enclenche l'ordre 0 |
CS | 0:8b3c6f593515 | 138 | vPort_Serie_Reception ( 0 ) ; |
CS | 0:8b3c6f593515 | 139 | } |
CS | 0:8b3c6f593515 | 140 | // Adresse |
CS | 0:8b3c6f593515 | 141 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[0] = SLAVE_ID ; |
CS | 0:8b3c6f593515 | 142 | // Fonction |
CS | 0:8b3c6f593515 | 143 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[1] = Ordres[Numero_Ordre_U8].Trame_Recue_aU8[1] ; |
CS | 0:8b3c6f593515 | 144 | |
CS | 0:8b3c6f593515 | 145 | if (( Ordres[Numero_Ordre_U8].Trame_Recue_aU8[1] == MB_FUNC_READ_HOLDING_REGISTER ) |
CS | 0:8b3c6f593515 | 146 | || ( Ordres[Numero_Ordre_U8].Trame_Recue_aU8[1] == MB_FUNC_READ_INPUT_REGISTER )) |
CS | 0:8b3c6f593515 | 147 | {// Fonction 3 ou 4, Lecture de registres |
CS | 0:8b3c6f593515 | 148 | // Nombre d'octets |
CS | 0:8b3c6f593515 | 149 | //PC7.printf("\n\rOrdre Func: %i",Ordres[Numero_Ordre_U8].Trame_Recue_aU8[1]) ; |
CS | 0:8b3c6f593515 | 150 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[2] = 2 * Ordres[Numero_Ordre_U8].Trame_Recue_aU8[5] ; |
CS | 0:8b3c6f593515 | 151 | // Registre de départ = Hi * 256 + Lo + Base |
CS | 0:8b3c6f593515 | 152 | Index_Memoire_U16 = REG_INPUT_START + (U16)(Ordres[Numero_Ordre_U8].Trame_Recue_aU8[2]) * 256 |
CS | 0:8b3c6f593515 | 153 | + (U16)Ordres[Numero_Ordre_U8].Trame_Recue_aU8[3] ; |
CS | 0:8b3c6f593515 | 154 | //PC7.printf("\r\n %i :",Index_Memoire_U16) ; |
CS | 0:8b3c6f593515 | 155 | Index_Caractere_U8 = 0 ; |
CS | 0:8b3c6f593515 | 156 | while ( Index_Caractere_U8 < Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[2] ) |
CS | 0:8b3c6f593515 | 157 | { |
CS | 0:8b3c6f593515 | 158 | // Octet de poids fort du registre |
CS | 0:8b3c6f593515 | 159 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[ Index_Caractere_U8 + 3] = (U8) (Memoire_S16 [ Index_Memoire_U16 ] >> 8 ) ; |
CS | 0:8b3c6f593515 | 160 | // Octet de poids faible |
CS | 0:8b3c6f593515 | 161 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[ Index_Caractere_U8 + 4] = (U8) ( Memoire_S16 [ Index_Memoire_U16 ] & 0xFF ) ; |
CS | 0:8b3c6f593515 | 162 | //PC7.printf("/ %X / %X /",Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[ Index_Caractere_U8 + 3],Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[ Index_Caractere_U8 + 4]) ; |
CS | 0:8b3c6f593515 | 163 | Index_Caractere_U8 = Index_Caractere_U8 + 2 ; |
CS | 0:8b3c6f593515 | 164 | Index_Memoire_U16++ ; |
CS | 0:8b3c6f593515 | 165 | } |
CS | 0:8b3c6f593515 | 166 | |
CS | 0:8b3c6f593515 | 167 | // Calcul de la position du CRC |
CS | 0:8b3c6f593515 | 168 | Index_CRC_U8 = 3 + Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[2] ; |
CS | 0:8b3c6f593515 | 169 | // Calcul du CRC |
CS | 0:8b3c6f593515 | 170 | Valeur_U16 = CRC16 ( (U8 *) &(Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[0]) , Index_CRC_U8 ) ; |
CS | 0:8b3c6f593515 | 171 | |
CS | 0:8b3c6f593515 | 172 | // CRC Poids fort |
CS | 0:8b3c6f593515 | 173 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[ Index_CRC_U8 ] = (U8) ( Valeur_U16 & 0xFF ) ; |
CS | 0:8b3c6f593515 | 174 | // CRC Poids faible |
CS | 0:8b3c6f593515 | 175 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[ Index_CRC_U8 + 1 ] = (U8) ( Valeur_U16 >> 8 ) ; |
CS | 0:8b3c6f593515 | 176 | // Longueur de trame à emettre |
CS | 0:8b3c6f593515 | 177 | Ordres[Numero_Ordre_U8].Nb_Caracteres_A_Emettre_U8 = Index_CRC_U8 + 2 ; |
CS | 0:8b3c6f593515 | 178 | } |
CS | 0:8b3c6f593515 | 179 | else if ( Ordres[Numero_Ordre_U8].Trame_Recue_aU8[1] == MB_FUNC_WRITE_MULTIPLE_REGISTERS ) |
CS | 0:8b3c6f593515 | 180 | {// Fonction 16 (0x10) écriture multiple |
CS | 0:8b3c6f593515 | 181 | //PC7.printf("\n\rOrdre Func: %i",Ordres[Numero_Ordre_U8].Trame_Recue_aU8[1]) ; |
CS | 0:8b3c6f593515 | 182 | // Registre de départ poids fort |
CS | 0:8b3c6f593515 | 183 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[2] = Ordres[Numero_Ordre_U8].Trame_Recue_aU8[2] ; |
CS | 0:8b3c6f593515 | 184 | // Registre de départ poids faible |
CS | 0:8b3c6f593515 | 185 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[3] = Ordres[Numero_Ordre_U8].Trame_Recue_aU8[3] ; |
CS | 0:8b3c6f593515 | 186 | // Nombre de registres poids fort |
CS | 0:8b3c6f593515 | 187 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[4] = Ordres[Numero_Ordre_U8].Trame_Recue_aU8[4] ; |
CS | 0:8b3c6f593515 | 188 | // Nombre de registres poids faible |
CS | 0:8b3c6f593515 | 189 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[5] = Ordres[Numero_Ordre_U8].Trame_Recue_aU8[5] ; |
CS | 0:8b3c6f593515 | 190 | |
CS | 0:8b3c6f593515 | 191 | // Registre de départ = Hi * 256 + Lo + Base |
CS | 0:8b3c6f593515 | 192 | Index_Memoire_U16 = REG_INPUT_START + (U16)(Ordres[Numero_Ordre_U8].Trame_Recue_aU8[2]) * 256 |
CS | 0:8b3c6f593515 | 193 | + (U16)Ordres[Numero_Ordre_U8].Trame_Recue_aU8[3] ; |
CS | 0:8b3c6f593515 | 194 | Index_Caractere_U8 = 0 ; |
CS | 0:8b3c6f593515 | 195 | while ( Index_Caractere_U8 < Ordres[Numero_Ordre_U8].Trame_Recue_aU8[6] ) |
CS | 0:8b3c6f593515 | 196 | { |
CS | 0:8b3c6f593515 | 197 | // Ecriture des registres |
CS | 0:8b3c6f593515 | 198 | Memoire_S16 [ Index_Memoire_U16 ] = (S16) (Ordres[Numero_Ordre_U8].Trame_Recue_aU8[ 7 + Index_Caractere_U8 ]) * 256 |
CS | 0:8b3c6f593515 | 199 | + (S16) Ordres[Numero_Ordre_U8].Trame_Recue_aU8[ 8 + Index_Caractere_U8 ]; |
CS | 0:8b3c6f593515 | 200 | Index_Caractere_U8 = Index_Caractere_U8 + 2 ; |
CS | 0:8b3c6f593515 | 201 | Index_Memoire_U16++ ; |
CS | 0:8b3c6f593515 | 202 | |
CS | 0:8b3c6f593515 | 203 | } |
CS | 0:8b3c6f593515 | 204 | // Calcul du CRC |
CS | 0:8b3c6f593515 | 205 | Valeur_U16 = CRC16 ( (U8 *) &(Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[0]) , 6 ) ; |
CS | 0:8b3c6f593515 | 206 | // Calcul de la position du CRC |
CS | 0:8b3c6f593515 | 207 | Index_CRC_U8 = 6 ; |
CS | 0:8b3c6f593515 | 208 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[ Index_CRC_U8] = (U8) ( Valeur_U16 & 0xFF ) ; |
CS | 0:8b3c6f593515 | 209 | Ordres[Numero_Ordre_U8].Trame_Reponse_aU8[ Index_CRC_U8 + 1 ] = (U8) ( Valeur_U16 >> 8 ); |
CS | 0:8b3c6f593515 | 210 | // Longueur de trame à emettre |
CS | 0:8b3c6f593515 | 211 | Ordres[Numero_Ordre_U8].Nb_Caracteres_A_Emettre_U8 = 8 ; |
CS | 0:8b3c6f593515 | 212 | } |
CS | 0:8b3c6f593515 | 213 | // La trame de réponse est terminée |
CS | 0:8b3c6f593515 | 214 | Ordres[Numero_Ordre_U8].Etat_U8 = TRAITE ; |
CS | 0:8b3c6f593515 | 215 | //PC7.printf("\n\rOrdre %i Etat: %i",Numero_Ordre_U8,TRAITE) ; |
CS | 0:8b3c6f593515 | 216 | // Lancement de l'émission de la réponse |
CS | 0:8b3c6f593515 | 217 | vPort_Serie_Emission ( Numero_Ordre_U8 ) ; |
CS | 0:8b3c6f593515 | 218 | } |
CS | 0:8b3c6f593515 | 219 | |
CS | 0:8b3c6f593515 | 220 | else if ( Ordres[Numero_Ordre_U8].Etat_U8 == FIN ) |
CS | 0:8b3c6f593515 | 221 | {// La réponse est émise, cloture l'ordre |
CS | 0:8b3c6f593515 | 222 | Ordres[Numero_Ordre_U8].Etat_U8 = ARRET ; |
CS | 0:8b3c6f593515 | 223 | //PC7.printf("\n\rOrdre %i Etat: %i",Numero_Ordre_U8,FIN) ; |
CS | 0:8b3c6f593515 | 224 | } |
CS | 0:8b3c6f593515 | 225 | else if ( Ordres[Numero_Ordre_U8].Etat_U8 == EMISSION ) |
CS | 0:8b3c6f593515 | 226 | {// L'emission de la réponse n'est pas terminée, re-essaie |
CS | 0:8b3c6f593515 | 227 | //PC7.printf("\n\rOrdre %i Etat: %i",Numero_Ordre_U8,EMISSION) ; |
CS | 0:8b3c6f593515 | 228 | vPort_Serie_Emission ( Numero_Ordre_U8 ); |
CS | 0:8b3c6f593515 | 229 | } |
CS | 0:8b3c6f593515 | 230 | else if ( Ordres[Numero_Ordre_U8].Etat_U8 == ARRET ) |
CS | 0:8b3c6f593515 | 231 | {// Compte les ordres arretés |
CS | 0:8b3c6f593515 | 232 | //PC7.printf("\n\rOrdre %i Etat: %i",Numero_Ordre_U8,ARRET) ; |
CS | 0:8b3c6f593515 | 233 | Ordres_Arretes_U8++ ; |
CS | 0:8b3c6f593515 | 234 | } |
CS | 0:8b3c6f593515 | 235 | else if ( Ordres[Numero_Ordre_U8].Etat_U8 == RECEPTION ) |
CS | 0:8b3c6f593515 | 236 | {// Controle si les temps ne sont pas dépassés |
CS | 0:8b3c6f593515 | 237 | //PC7.printf("\n\rOrdre %i Etat: %i",Numero_Ordre_U8,RECEPTION) ; |
CS | 0:8b3c6f593515 | 238 | //Ordres[Numero_Ordre_U8].Etat_U8 = cControle_Reception( Numero_Ordre_U8 ); |
CS | 0:8b3c6f593515 | 239 | } |
CS | 0:8b3c6f593515 | 240 | else if ( Ordres[Numero_Ordre_U8].Etat_U8 == ATTENTE ) |
CS | 0:8b3c6f593515 | 241 | {// Controle si les temps ne sont pas dépassés |
CS | 0:8b3c6f593515 | 242 | //PC7.printf("\n\rOrdre %i Etat: %i",Numero_Ordre_U8,ATTENTE) ; |
CS | 0:8b3c6f593515 | 243 | } |
CS | 0:8b3c6f593515 | 244 | else if ( Ordres[Numero_Ordre_U8].Etat_U8 == TIMEOUT ) |
CS | 0:8b3c6f593515 | 245 | {// Les temps sont dépassés |
CS | 0:8b3c6f593515 | 246 | Ordres[Numero_Ordre_U8].Etat_U8 = ARRET; |
CS | 0:8b3c6f593515 | 247 | } |
CS | 0:8b3c6f593515 | 248 | //PC7.printf("\r\n Modbus : %i \t %i \r\n",Numero_Ordre_U8,Ordres[Numero_Ordre_U8].Etat_U8 ); |
CS | 0:8b3c6f593515 | 249 | } |
CS | 0:8b3c6f593515 | 250 | if (Ordres_Arretes_U8 >= TAILLE_PILE_ORDRES ) |
CS | 0:8b3c6f593515 | 251 | {// Si tous les ordres sont arretés, le prochain aura l'index 0 |
CS | 0:8b3c6f593515 | 252 | vPort_Serie_Reception ( 0 ) ; |
CS | 0:8b3c6f593515 | 253 | } |
CS | 0:8b3c6f593515 | 254 | } |
CS | 0:8b3c6f593515 | 255 | |
CS | 0:8b3c6f593515 | 256 | extern void vModbus_Stop() |
CS | 0:8b3c6f593515 | 257 | { |
CS | 0:8b3c6f593515 | 258 | } |
CS | 0:8b3c6f593515 | 259 | extern void vModbus_Reset() |
CS | 0:8b3c6f593515 | 260 | { |
CS | 0:8b3c6f593515 | 261 | } |
CS | 0:8b3c6f593515 | 262 | |
CS | 0:8b3c6f593515 | 263 |