#include "uart.h" //file paths and global variables #define incomplet_uart_path "/dev/ttyO" struct termios options; //The termios structure // *** FUNCTIONS *** int uartConf(uint8_t uartNumber, int baudRate) { char fullFileName[11]; //completing uart file path if ((uartNumber==1)||(uartNumber==2)||(uartNumber==4)||(uartNumber==5)) sprintf(fullFileName, incomplet_uart_path "%d", uartNumber); else{ perror("Wrong UART number. " \ "UART numbers availables 1, 2, 4 or 5.\n"); return UART_NUMBER_INCORRECT; } //openign uart file printf("Configuring UART%d.\n", uartNumber); if ((uartFile[uartNumber] = open(fullFileName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK))<0){ perror("UART: Failed to open the file.\n"); return -1; } //Sets the parameters associated with file tcgetattr(uartFile[uartNumber], &options); //cleaning flags options.c_ispeed = 0; options.c_lflag = 0; options.c_line = 0; options.c_oflag = 0; options.c_ospeed = 0 // Set up the communications options: options.c_cflag = baudRate | CS8 | CREAD | CLOCAL; // 8-bit, enable receiver, no modem control lines options.c_iflag = IGNPAR; //ignore partity errors, CR -> newline tcflush(uartFile[uartNumber], TCIOFLUSH); //discard file information not transmitted tcsetattr(uartFile[uartNumber], TCSANOW, &options); //changes occur immmediately printf("UART%d configurated.\n", uartNumber); return UART_FUNCTION_SUCCESSFUL; } int uartClose(uint8_t uartNumber) { printf("Closing UART%d.\n", uartNumber); close(uartFile[uartNumber]); return UART_FUNCTION_SUCCESSFUL; } int uartTransmit(uint8_t uartNumber, char message[]) { int count; //writing file if ((count = write(uartFile[uartNumber], message, (strlen(message))))<0){ //send the string perror("Failed to write to the output\n"); return -1; } tcflush(uartFile[uartNumber], TCOFLUSH); return UART_FUNCTION_SUCCESSFUL; } int uartReceive(uint8_t uartNumber) { int count; while((count = read(uartFile[uartNumber], (void*)receive[uartNumber], 100)) < 0); tcflush(uartFile[uartNumber], TCIFLUSH); return count; }