Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Program writes a character to the serial port at 9600 bps 8N1 format */
- /* Baudrate - 9600 */
- /* Stop bits -1 */
- /* No Parity */
- #include <stdio.h>
- #include <fcntl.h> /* File Control Definitions */
- #include <termios.h> /* POSIX Terminal Control Definitions */
- #include <unistd.h> /* UNIX Standard Definitions */
- #include <errno.h> /* ERROR Number Definitions */
- #include <stdlib.h>
- int main(void) {
- int fd; /*File Descriptor*/
- struct termios SerialPortSettings; /* Create the structure */
- unsigned short bytes_written = 0; /* Value for storing the number of bytes written to the port */
- int i;
- char checksum = 0;
- char * dump_buf;
- int dump_numr = 0;
- FILE *dump;
- int dump_size = 0;
- dump = fopen("dump1.bin", "rb");
- if (dump == NULL)
- {
- printf("open read file error.\n");
- return 0;
- }
- // obtain file size:
- fseek (dump , 0L , SEEK_END);
- dump_size = ftell (dump);
- rewind (dump);
- printf("dump size = %d\n", dump_size);
- // allocate memory to contain the whole file:
- dump_buf = (char*) malloc (sizeof(char)*dump_size);
- if (dump_buf == NULL) {fputs ("Memory error",stderr); exit (2);}
- // copy the file into the buffer:
- dump_numr = fread (dump_buf,1,dump_size,dump);
- fclose(dump);
- if (dump_numr != dump_size) {fputs ("Reading error",stderr); exit (3);}
- //подсчет размера пакета
- char tmp;
- unsigned short *p = (unsigned short *)(dump_buf+4);
- *p = (unsigned short)dump_size - 7; //подсчет размера пакета
- tmp = dump_buf[5]; dump_buf[5] = dump_buf[4]; dump_buf[4] = tmp;
- //подсчет контрольной суммы
- for(i = 0; i <= dump_size - 1; i++)
- if(i != 11)
- dump_buf[11] += dump_buf[i];
- dump_buf[11] = 0 - dump_buf[11];
- fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd == -1)
- printf("\n Error! in Opening ttyUSB0 ");
- else
- printf("\n ttyUSB0 Opened Successfully ");
- /*---------- Setting the Attributes of the serial port using termios structure --------- */
- tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */
- cfsetispeed(&SerialPortSettings,B9600); /* Set Read Speed as 9600 */
- cfsetospeed(&SerialPortSettings,B9600); /* Set Write Speed as 9600 */
- cfmakeraw(&SerialPortSettings);
- SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */
- if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/
- printf("\n ERROR ! in Setting attributes");
- else
- printf("\n BaudRate = 9600 \n StopBits = 1 \n Parity = none\n");
- sleep(3);
- tcflush(fd, TCIOFLUSH);
- bytes_written = write(fd, dump_buf, dump_size);
- char c;
- char read_buffer[1024*1024]; /* Buffer to store the data received */
- int bytes_read = 0; /* Number of bytes read by the read() system call */
- i = 0;
- int j = 0;
- while(1) {
- if(read(fd,&c,1) > 0) {
- if(i > (dump_size - 1)) {
- if(!(j % 16))
- printf("\n%05Xh:%02hhX ", j, c), read_buffer[j] = c;
- else
- printf("%02hhX ", c), read_buffer[j] = c;
- j++;
- }
- i++;
- }
- if(j >= 262144)
- break;
- }
- putchar('\n');
- FILE * outputFile;
- outputFile = fopen("output.bin", "wb");
- fwrite(read_buffer, sizeof(char), j, outputFile);
- fclose(outputFile);
- close(fd);
- fclose(dump);
- free(dump_buf);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement