Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4.  
  5. #pragma warning(disable:4996)
  6.  
  7. //ram loader
  8.  
  9.  
  10. uint8_t raw_block[180];
  11.  
  12. uint8_t count;
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. uint8_t readHex(char c)
  20. {
  21. char res;
  22.  
  23. if (isdigit(c) != 0)
  24. res = c - 48; //convert ascii to digit
  25. else
  26. res = c - 55;
  27. return res;
  28.  
  29. }
  30.  
  31.  
  32. int main(int argc, char **argv)
  33. {
  34. uint8_t ch;
  35. char file_name[255];
  36. FILE *fp;
  37.  
  38. uint16_t count;
  39. uint16_t addr;
  40. uint8_t data;
  41. uint16_t sum;
  42. uint8_t sum_check;
  43. int cs;
  44. int k;
  45.  
  46. //printf("Argc: %d\n", argc);
  47.  
  48. if (argc == 2)
  49. {
  50.  
  51. strcpy(file_name, argv[1]);
  52.  
  53.  
  54.  
  55. fp = fopen(file_name, "rb"); // read mode
  56.  
  57. if (fp == NULL)
  58. {
  59. perror("Error while opening the file.\n");
  60. exit(EXIT_FAILURE);
  61. }
  62.  
  63.  
  64. while (!feof(fp))
  65. {
  66. uint16_t checksum = 0;
  67.  
  68. ch = fgetc(fp);
  69. //printf("%c", ch);
  70. //printf("%d",ch);
  71. //checksum += ch;
  72. int raw_data;
  73.  
  74. if (ch == 0xAA) //check sync1
  75. {
  76. //printf("1");
  77.  
  78.  
  79. ch = fgetc(fp);
  80. //checksum += ch;
  81.  
  82. if (ch == 0xAA) //check sync2
  83. {
  84. //printf("2\n");
  85.  
  86. ch = fgetc(fp);
  87. //checksum += ch;
  88.  
  89. if (ch == 0x04) //check size
  90. {
  91. //printf("3\n");
  92. ch = fgetc(fp);
  93. checksum += ch;
  94.  
  95. if (ch == 0x80) //check raw type
  96. {
  97. //printf("R\n");
  98.  
  99. ch = fgetc(fp);
  100. checksum += ch;
  101.  
  102. if (ch == 0x02) //check 2 bytes size
  103. {
  104.  
  105. //printf("R\n");
  106. cs = fgetc(fp);
  107. checksum += (uint8_t)cs;
  108.  
  109. raw_data = cs << 8;
  110.  
  111. cs = fgetc(fp);
  112. checksum += (uint8_t)cs;
  113.  
  114. raw_data = raw_data | cs;
  115.  
  116. checksum &= 0xFF;
  117. checksum = ~checksum & 0xFF;
  118.  
  119. ch = fgetc(fp);
  120. if (ch==checksum)
  121. printf("%d\n", raw_data);
  122. else printf("-10\n"); //crc error
  123. }
  124.  
  125. }
  126.  
  127.  
  128. }
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135. }//end of sync 1 check
  136. //else printf("Skip\n");
  137. }
  138.  
  139.  
  140. fclose(fp);
  141.  
  142.  
  143.  
  144.  
  145.  
  146. }
  147. //getch();
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement