commit d9ce836371d07161cf3a0263abbd0095ee9454ef Author: Florian Loeffler Date: Fri Feb 19 10:09:41 2021 +0100 initial commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..77008af --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: + gcc uart.c + +clean: + rm uart diff --git a/a.out b/a.out new file mode 100755 index 0000000..59841f2 Binary files /dev/null and b/a.out differ diff --git a/uart b/uart new file mode 100755 index 0000000..59841f2 Binary files /dev/null and b/uart differ diff --git a/uart.c b/uart.c new file mode 100644 index 0000000..a322424 --- /dev/null +++ b/uart.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include + +int main() { + +int uart0_filestream = -1; +uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); +if (uart0_filestream == -1) { + printf("[ERROR] UART open()\n"); +} + +struct termios options; +tcgetattr(uart0_filestream, &options); + options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; + options.c_iflag = IGNPAR; + options.c_oflag = 0; + options.c_lflag = 0; +tcflush(uart0_filestream, TCIFLUSH); +tcsetattr(uart0_filestream, TCSANOW, &options); + + +// Bytes senden +unsigned char BUF_TX[20]; +unsigned char *TX; + +TX = &BUF_TX[0]; +*TX++ = 'R'; +/* +*TX++ = 'a'; +*TX++ = 's'; +*TX++ = 'p'; +*TX++ = 'b'; +*TX++ = 'e'; +*TX++ = 'r'; +*TX++ = 'r'; +*TX++ = 'y'; +*TX++ = ' '; +*TX++ = 'P'; +*TX++ = 'i'; +*/ +if (uart0_filestream != -1) { + int out = write(uart0_filestream, &BUF_TX[0], (TX - &BUF_TX[0])); // + if (out < 0) { + printf("[ERROR] UART TX\n"); + } else { + //printf("[STATUS: TX %i Bytes] %s\n", out, BUF_TX); + } +} // if uart0 + +sleep(1); + +// Bytes empfangen +if (uart0_filestream != -1) { + unsigned char BUF_RX[50]; + int rx_length = read(uart0_filestream, (void*)BUF_RX, 50); + + if (rx_length < 0) { + printf("[ERROR] UART RX\n"); + } else if (rx_length == 0) { + printf("[ERROR] UART RX - no data\n"); + } else { + BUF_RX[rx_length] = '\0'; + //printf("[STATUS: RX %i Bytes] %s\n", rx_length, BUF_RX); + printf("%s", BUF_RX); + } //rx_length check +} //if uart0 + +close(uart0_filestream); + +return 0; +}