Odczyt i zapis danych binarnych przez port szeregowy

Szukałem więc i nie mogłem dokładnie znaleźć tego, czego potrzebowałem. Potrzebuję pomocy w czytaniu i zapisywaniu danych binarnych przez port szeregowy i będę wdzięczny za każdą radę, jaką możesz mieć. Proszę zauważyć, że zadałem pytanie podobne do tego, kiedy byłem na innym etapie tego projektu.

Poniżej znajdują się programy. Pierwszy program otwiera plik „test.jpg”, odczytuje go w trybie binarnym i zapisuje wynik w buforze. Następnie zamyka plik i ma wysłać ten plik przez port szeregowy.

Drugi program tworzy plik o nazwie „testout.jpg” i powinien czytać dane wysłane z poprzedniego programu.

Mam przeczucie, że problem w moim kodzie leży w drugim programie. Może też muszę użyć do tego fread? Próbowałem, ale nie wiem, jak go zaimplementować dla portu szeregowego, ponieważ jestem stosunkowo nowy w programowaniu.

Wielkie dzięki za twój czas.

Zapis seryjny:

    #include <stdio.h>   /* Standard input/output definitions */
    #include <string.h>  /* String function definitions */
    #include <unistd.h>  /* UNIX standard function definitions */
    #include <fcntl.h>   /* File control definitions */
    #include <errno.h>   /* Error number definitions */
    #include <termios.h> /* POSIX terminal control definitions */
    #include <stdlib.h>

    int main()
        {
                //writing
                int writeport = open_port("/dev/ttyUSB0");

                //open file

                FILE *file;
                char *buffer;
                int fileLen;

                file = fopen("test.jpg", "rb");

                //get file size

                fseek(file, 0, SEEK_END);
                fileLen = ftell(file);
                fseek(file, 0, SEEK_SET);

                buffer = (char *)malloc(fileLen + 1);

                //read file contents

                fread(buffer, fileLen, 1, file);
                fclose(file);

                int n = write(writeport, buffer, fileLen + 1);
                if (n < 0)
                        fputs("write() of bytes failed!\n", stderr);

                //closing ports
                close(writeport);
        }

        int open_port(char str[])
    {
        int fd = open(str, O_RDWR | O_NOCTTY | O_NONBLOCK); // ?? NDELAY or NONBLOCK?

      if (fd == -1)
      {
                        perror("open_port: Unable to open /dev/ttyS0 - ");
      }
      else
                        fcntl(fd, F_SETFL, 0);

          struct termios options;
          tcgetattr(fd, &options); //this gets the current options set for the port

          // setting the options

          cfsetispeed(&options, B9600); //input baudrate
          cfsetospeed(&options, B9600); // output baudrate
          options.c_cflag |= (CLOCAL | CREAD); // ?? enable receicer and set local mode
          //options.c_cflag &= ~CSIZE; /* mask the character size bits */
          options.c_cflag |= CS8;    /* select 8 data bits */
          options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // choosing raw input
          options.c_iflag &= ~INPCK; // disable parity check
          options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
          options.c_oflag |= OPOST; // ?? choosing processed output
          options.c_cc[VMIN] = 0; // Wait until x bytes read (blocks!)
          options.c_cc[VTIME] = 0; // Wait x * 0.1s for input (unblocks!)

          // settings for no parity bit
          options.c_cflag &= ~PARENB;
          options.c_cflag &= ~CSTOPB;
          options.c_cflag &= ~CSIZE;
          options.c_cflag |= CS8;

          tcsetattr(fd, TCSANOW, &options); //set the new options ... TCSANOW specifies all option changes to occur immediately

      return (fd);
    }

Serial read:

    #include <stdio.h>   /* Standard input/output definitions */
    #include <string.h>  /* String function definitions */
    #include <unistd.h>  /* UNIX standard function definitions */
    #include <fcntl.h>   /* File control definitions */
    #include <errno.h>   /* Error number definitions */
    #include <termios.h> /* POSIX terminal control definitions */

    int main()
        {
                //reading      
                int readport = open_port("/dev/ttyUSB1");

        //open resultant file

        FILE *file;
        //system("rm testout.jpg");
        file = fopen("testout.jpg", "wb");

                //trying to read one character at a time
                char buff;
                int n = 1;

           while (n > 0)
           {
                n = read(readport, &buff, 1);
                //printf("%c", buff, buff);

                **//I tried these three methods, with little success**

                //fprintf(file, "%c", buff);
                //fwrite(&buff, 1, 1, file);
                //write(file, &buff, 1);
           }

                //closing ports
                close(readport);
                fclose(file);
        }

        int open_port(char str[])
    {
        int fd = open(str, O_RDWR | O_NOCTTY | O_NONBLOCK); // ?? NDELAY or NONBLOCK?

      if (fd == -1)
      {
                        perror("open_port: Unable to open /dev/ttyS0 - ");
      }
      else
                        fcntl(fd, F_SETFL, 0);

          struct termios options;
          tcgetattr(fd, &options); //this gets the current options set for the port

          // setting the options

          cfsetispeed(&options, B9600); //input baudrate
          cfsetospeed(&options, B9600); // output baudrate
          options.c_cflag |= (CLOCAL | CREAD); // ?? enable receicer and set local mode
          //options.c_cflag &= ~CSIZE; /* mask the character size bits */
          options.c_cflag |= CS8;    /* select 8 data bits */
          options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // choosing raw input
          options.c_iflag &= ~INPCK; // disable parity check
          options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
          options.c_oflag |= OPOST; // ?? choosing processed output
          options.c_cc[VMIN] = 0; // Wait until x bytes read (blocks!)
          options.c_cc[VTIME] = 0; // Wait x * 0.1s for input (unblocks!)

          // settings for no parity bit
          options.c_cflag &= ~PARENB;
          options.c_cflag &= ~CSTOPB;
          options.c_cflag &= ~CSIZE;
          options.c_cflag |= CS8;

          tcsetattr(fd, TCSANOW, &options); //set the new options ... TCSANOW specifies all option changes to occur immediately

      return (fd);
    }

questionAnswers(1)

yourAnswerToTheQuestion