initial commit
This commit is contained in:
commit
d9ce836371
76
uart.c
Normal file
76
uart.c
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user