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 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <stdio.h>
#include <stdlib.h>
FILE *hexfile ;
unsigned short nbdata = 0,
adresse = 0,
type = 0,
chk = 0 ;
unsigned short codehex[64] ;
/*==============================================================*/
/* */
/* P R O G R A M M E P R I N C I P A L */
/* */
/*==============================================================*/
int main(int argc, char *argv[])
{
unsigned char ctrlChk = 0 ;
int fret = 0 ;
unsigned short i = 0, numLigne = 0 ;
if((hexfile=fopen("fichier.hex", "r+"))==NULL)
{
printf("Erreur d'ouverture du fichier\n") ;
return 0 ;
}
while(!feof(hexfile))
{
numLigne++ ;
// Lecture du nombre de données, de l'adresse et du type
if((fret=fscanf(hexfile, ":%2hx%4hx%2hx", &nbdata, &adresse, &type))!=3)
{
printf("Erreur de syntaxe ligne #%d (%d)\n", numLigne, fret) ;
break ;
}
// Calcul du checksum
ctrlChk = (unsigned char)nbdata ;
ctrlChk += (unsigned char)((adresse>>8) & 0x00FF) ;
ctrlChk += (unsigned char)(adresse & 0x00FF) ;
ctrlChk += (unsigned char)(type) ;
// Lecture des données
for(i=0; i<nbdata; i++)
{
if((fret=fscanf(hexfile, "%2hx", &codehex[i]))!=1)
{
printf("Erreur de syntaxe ligne #%d (%d)\n", numLigne, fret) ;
break ;
}
ctrlChk += (unsigned char)codehex[i] ;
}
// Lecture du checksum de la ligne
if((fret=fscanf(hexfile, "%2hx\n", &chk))!=1)
{
printf("Erreur de syntaxe ligne #%d (%d)\n", numLigne, fret) ;
break ;
}
// Affichage des informations
printf("Ligne #%d\n", numLigne) ;
printf("Nb d'octets : %d\n", nbdata) ;
printf("Adresse : %.4Xh\n", adresse) ;
printf("Type : %.2d\n", type) ;
printf("Donnees : ") ;
for(i=0; i<nbdata; i++)
{
if(i && !(i%16))
printf("\n ") ;
printf("%.2Xh ", codehex[i]) ;
}
if(!nbdata) printf("-") ;
// Contrôle du checksum de ligne
if(chk==(0x100-ctrlChk))
printf("\nChecksum : %.2Xh\n", chk) ;
else
printf("\nChecksum : defaut\n") ;
printf("\n") ;
}
fclose(hexfile) ;
return 0 ;
} |
Partager