From c8aeab392d9c5f49543fbccc4d54c834fa3d1d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Proch=C3=A1zka?= Date: Mon, 20 Feb 2023 22:51:18 +0100 Subject: [PATCH] add test script --- Test/Makefile | 16 +++++++ Test/uart_jpeg.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 Test/Makefile create mode 100644 Test/uart_jpeg.c diff --git a/Test/Makefile b/Test/Makefile new file mode 100644 index 0000000..dae2797 --- /dev/null +++ b/Test/Makefile @@ -0,0 +1,16 @@ +CC = gcc +CFLAGS = -Wall -Wextra + +SRCS = uart_jpeg.c +OBJS = $(SRCS:.c=.o) + +all: uart_jpeg + +uart_jpeg: $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +clean: + rm -f $(OBJS) uart_jpeg \ No newline at end of file diff --git a/Test/uart_jpeg.c b/Test/uart_jpeg.c new file mode 100644 index 0000000..f97fe57 --- /dev/null +++ b/Test/uart_jpeg.c @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include + +int main() +{ + int fd; + unsigned char buffer[100000]; + unsigned char tempBuff[1]; + // Open the UART device + fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY); + if (fd == -1) + { + perror("Error opening UART device"); + return -1; + } + + // Configure the UART settings + struct termios options; + tcgetattr(fd, &options); + cfsetispeed(&options, B115200); + cfsetospeed(&options, B115200); + options.c_cflag &= ~PARENB; + options.c_cflag &= ~CSTOPB; + options.c_cflag &= ~CSIZE; + options.c_cflag |= CS8; + tcsetattr(fd, TCSANOW, &options); + + // Read a single byte from the UART device + + int fileCount = 1; + + while (1) + { + + if (read(fd, tempBuff, 1) != 1) + { + continue; + } + if (tempBuff[0] != (unsigned char)255) + { + continue; + } + if (read(fd, tempBuff, 1) != 1) + { + continue; + } + if (tempBuff[0] != (unsigned char)216) + { + continue; + } + + unsigned int currentIndex = 2; + buffer[0] = 255; + buffer[1] = 216; + while (1) + { + + if (read(fd, tempBuff, 1) != 1) + { + continue; + } + buffer[currentIndex++] = tempBuff[0]; + if (tempBuff[0] != 0xFF) + { + continue; + } + if (read(fd, tempBuff, 1) != 1) + { + continue; + } + buffer[currentIndex++] = tempBuff[0]; + if (tempBuff[0] == 0xD8) + { + printf("Corrupted File, start over...\n"); + memset(buffer, 0x00, sizeof(buffer)); + currentIndex = 2; + buffer[0] = 0xFF; + buffer[1] = 0xD8; + continue; + } + if (tempBuff[0] == 0xD9) + { + // END FOUND + char fileName[11] = {'0', '0', '0', '0', '0', '.', 'j', 'p', 'e', 'g', '\0'}; + char secondDigit = (fileCount % 10) + '0'; + char firstDigit = (fileCount / 10) + '0'; + fileName[0] = firstDigit; + fileName[1] = secondDigit; + + FILE *fp = fopen(fileName, "wb"); + if (fp == NULL) + { + printf("Error opening file!\n"); + return 1; + } + + for (unsigned int i = 0; i < currentIndex; i++) + { + unsigned char myChar = buffer[i]; + fwrite(&myChar, sizeof(unsigned char), 1, fp); + } + + // Close the file + fclose(fp); + printf("File %s saved!\n", fileName); + memset(buffer, 0x00, sizeof(buffer)); + fileCount++; + break; + } + } + } + + // Close the UART device + close(fd); + + return 0; +} \ No newline at end of file