the Below attached receiving and storing code written on beaglebone. Which receives and write content to file and file is stored in the beaglebone.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#define UART_BUFFER_SIZE 8192
void receiveAndStoreStringOverUART(const char *folderPath, const char *filename) {
// Open UART port
int uart_fd = open(“/dev/ttyS1”, O_RDWR | O_NOCTTY);
struct termios uart_config;
if (uart_fd < 0) {
perror(“Error opening UART port”);
return;
}
// Configure UART settings (adjust as needed)
if (tcgetattr(uart_fd, &uart_config) < 0) {
perror("Error getting UART configuration");
close(uart_fd);
return;
}
// Set baud rate to 9600
cfsetispeed(&uart_config, B9600);
cfsetospeed(&uart_config, B9600);
// Set data format to 8N1 (8 data bits, no parity, 1 stop bit)
uart_config.c_cflag &= ~PARENB;
uart_config.c_cflag &= ~CSTOPB;
uart_config.c_cflag &= ~CSIZE;
uart_config.c_cflag |= CS8;
// Disable hardware flow control
uart_config.c_cflag &= ~CRTSCTS;
// Enable receiver and local mode
uart_config.c_cflag |= CREAD | CLOCAL;
// Set input mode to non-canonical and disable echo
uart_config.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// Set the minimum number of characters for non-canonical read
uart_config.c_cc[VMIN] = 1;
// Set the timeout in deciseconds for non-canonical read
uart_config.c_cc[VTIME] = 0;
// Apply UART configuration
if (tcsetattr(uart_fd, TCSANOW, &uart_config) < 0) {
perror("Error setting UART configuration");
close(uart_fd);
return;
}
// Read the string from UART
char buffer[UART_BUFFER_SIZE];
ssize_t bytesRead;
while ((bytesRead = read(uart_fd, buffer, sizeof(buffer))) > 0) {
// Print the received string
printf("Received string: %.*s\n", (int)bytesRead, buffer);
// Construct the full path of the file
char filePath[UART_BUFFER_SIZE + strlen(folderPath) + strlen(filename) + 1];
snprintf(filePath, sizeof(filePath), "%s%s", folderPath, filename);
// Open the file for writing in binary mode to preserve special characters
FILE *file = fopen(filePath, "ab");
if (file != NULL) {
// Write the received string to the file
size_t bytesWritten = fwrite(buffer, 1, bytesRead, file);
if (bytesWritten < bytesRead) {
perror("Error writing to file");
} else {
printf("Stored %zu bytes of data in %s\n", bytesWritten, filename);
}
// Close the file
fclose(file);
} else {
perror("Error opening file for writing");
}
}
if (bytesRead < 0) {
perror("Error reading from UART");
}
// Close UART port
close(uart_fd);
printf("UART port closed\n");
}
int main(void) {
// Specify the folder path where you want to store the received files
const char *storageFolderPath = “/home/debian/BLE/”;
// Specify the filename to be used for storing the received data
const char *receivedFilename = "PRO_REC.TXT";
// Receive and store the string over UART
receiveAndStoreStringOverUART(storageFolderPath, receivedFilename);
return 0;
}
I 6 files case, i am sending files one by one in teraterm. The content of files printed both on teraterm and minicom.
i will attach c code for it. Please go through it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#define UART_BUFFER_SIZE 8192
typedef struct {
const char *filename;
} FileDetails;
void receiveAndStoreFilesOverUART(const char *folderPath, const FileDetails *files, int numFiles) {
// Open UART port
int uart_fd = open(“/dev/ttyS1”, O_RDWR | O_NOCTTY);
struct termios uart_config;
if (uart_fd < 0) {
perror(“Error opening UART port”);
return;
}
// Configure UART settings (adjust as needed)
if (tcgetattr(uart_fd, &uart_config) < 0) {
perror("Error getting UART configuration");
close(uart_fd);
return;
}
// Set baud rate to 9600
cfsetispeed(&uart_config, B9600);
cfsetospeed(&uart_config, B9600);
// Set data format to 8N1 (8 data bits, no parity, 1 stop bit)
uart_config.c_cflag &= ~PARENB;
uart_config.c_cflag &= ~CSTOPB;
uart_config.c_cflag &= ~CSIZE;
uart_config.c_cflag |= CS8;
// Disable hardware flow control
uart_config.c_cflag &= ~CRTSCTS;
// Enable receiver and local mode
uart_config.c_cflag |= CREAD | CLOCAL;
// Set input mode to non-canonical and disable echo
uart_config.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// Set the minimum number of characters for non-canonical read
uart_config.c_cc[VMIN] = 1;
// Set the timeout in deciseconds for non-canonical read
uart_config.c_cc[VTIME] = 0;
// Apply UART configuration
if (tcsetattr(uart_fd, TCSANOW, &uart_config) < 0) {
perror("Error setting UART configuration");
close(uart_fd);
return;
}
// Receive and store each file
for (int i = 0; i < numFiles; i++) {
// Read the file content from UART
char buffer[UART_BUFFER_SIZE];
ssize_t bytesRead;
// Construct the full path of the file
char filePath[strlen(folderPath) + strlen(files[i].filename) + 1];
snprintf(filePath, sizeof(filePath), "%s%s", folderPath, files[i].filename);
// Open the file for writing in binary mode to preserve special characters
FILE *file = fopen(filePath, "wb");
if (file != NULL) {
while ((bytesRead = read(uart_fd, buffer, sizeof(buffer))) > 0) {
// Write the received content to the file
size_t bytesWritten = fwrite(buffer, 1, bytesRead, file);
if (bytesWritten < bytesRead) {
perror("Error writing to file");
break;
}
}
if (bytesRead < 0) {
perror("Error reading from UART");
}
// Close the file
fclose(file);
} else {
perror("Error opening file for writing");
}
}
// Close UART port
close(uart_fd);
printf("UART port closed\n");
}
int main(void) {
// Specify the folder path where you want to store the received files
const char *storageFolderPath = “/home/debian/BLE/”;
// Specify the filenames of the received files
FileDetails files[] = {
{"1.OP_REC.TXT"},
{"2.PAT_REC.TXT"},
{"3.JAR_REC.TXT"},
{"4.DIA_ID.TXT"},
{"5.DIA_REC.TXT"},
{"6.PRO_REC.TXT"}
};
int numFiles = sizeof(files) / sizeof(files[0]);
// Receive and store the files over UART
receiveAndStoreFilesOverUART(storageFolderPath, files, numFiles);
return 0;
}
this not write content to files and file not stores to beaglebone.
Regards,
Avinash