SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /* Program writes a character to the serial port at 9600 bps 8N1 format */
  3. /* Baudrate - 9600 */
  4. /* Stop bits -1 */
  5. /* No Parity */
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <fcntl.h>   /* File Control Definitions           */
  10. #include <termios.h> /* POSIX Terminal Control Definitions */
  11. #include <unistd.h>  /* UNIX Standard Definitions      */
  12. #include <errno.h>   /* ERROR Number Definitions           */
  13. #include <stdlib.h>
  14.  
  15. int main(void) {
  16.     int fd; /*File Descriptor*/
  17.     struct termios SerialPortSettings;  /* Create the structure                          */
  18.     unsigned short bytes_written = 0; /* Value for storing the number of bytes written to the port */
  19.     int i;
  20.     char checksum = 0; 
  21.     char * dump_buf;
  22.     int dump_numr = 0;
  23.     FILE *dump;
  24.     int dump_size = 0;
  25.     dump = fopen("dump1.bin", "rb");
  26.     if (dump == NULL)
  27.         {
  28.             printf("open read file error.\n");
  29.             return 0;
  30.         }
  31.     // obtain file size:
  32.     fseek (dump , 0L , SEEK_END);
  33.     dump_size = ftell (dump);
  34.     rewind (dump);
  35.     printf("dump size = %d\n", dump_size);
  36.     // allocate memory to contain the whole file:
  37.     dump_buf = (char*) malloc (sizeof(char)*dump_size);
  38.     if (dump_buf == NULL) {fputs ("Memory error",stderr); exit (2);}
  39.     // copy the file into the buffer:
  40.     dump_numr = fread (dump_buf,1,dump_size,dump);
  41.     fclose(dump);
  42.     if (dump_numr != dump_size) {fputs ("Reading error",stderr); exit (3);}
  43.  
  44.     //подсчет размера пакета
  45.     char tmp;
  46.     unsigned short *p = (unsigned short *)(dump_buf+4);
  47.     *p = (unsigned short)dump_size - 7; //подсчет размера пакета
  48.  
  49.     tmp = dump_buf[5]; dump_buf[5] = dump_buf[4]; dump_buf[4] = tmp;
  50.    
  51.  
  52.     //подсчет контрольной суммы
  53.     for(i = 0; i <= dump_size - 1; i++)
  54.             if(i != 11)
  55.             dump_buf[11] += dump_buf[i];
  56.     dump_buf[11] = 0 - dump_buf[11];
  57.  
  58.     fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  59.  
  60.     if (fd == -1)
  61.         printf("\n  Error! in Opening ttyUSB0  ");
  62.     else
  63.         printf("\n  ttyUSB0 Opened Successfully ");
  64.  
  65.     /*---------- Setting the Attributes of the serial port using termios structure --------- */
  66.     tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */
  67.     cfsetispeed(&SerialPortSettings,B9600); /* Set Read  Speed as 9600                       */
  68.     cfsetospeed(&SerialPortSettings,B9600); /* Set Write Speed as 9600                       */  
  69.     cfmakeraw(&SerialPortSettings);
  70.   SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly   */
  71.     if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/
  72.         printf("\n  ERROR ! in Setting attributes");
  73.     else
  74.         printf("\n  BaudRate = 9600 \n  StopBits = 1 \n  Parity   = none\n");
  75.     sleep(3);
  76.   tcflush(fd, TCIOFLUSH);
  77.     bytes_written = write(fd, dump_buf, dump_size);
  78.  
  79.    
  80.     char c;
  81.     char read_buffer[1024*1024];   /* Buffer to store the data received              */
  82.     int  bytes_read = 0;    /* Number of bytes read by the read() system call */
  83.      i = 0;
  84.      int j = 0;
  85.      while(1) {
  86.         if(read(fd,&c,1) > 0) {
  87.             if(i > (dump_size - 1)) {
  88.               if(!(j % 16))
  89.                   printf("\n%05Xh:%02hhX ", j, c), read_buffer[j] = c;
  90.               else
  91.                   printf("%02hhX ", c), read_buffer[j] = c;  
  92.               j++;
  93.             }
  94.             i++;
  95.         }
  96.         if(j >= 262144)
  97.             break;
  98.       }
  99.       putchar('\n');
  100.     FILE * outputFile;
  101.     outputFile = fopen("output.bin", "wb");
  102.     fwrite(read_buffer, sizeof(char), j, outputFile);
  103.  
  104.     fclose(outputFile);
  105.     close(fd);
  106.     fclose(dump);
  107.     free(dump_buf);
  108.     return 0;
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top