Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char receiveCommand() {
- int rx_length;
- int uart0_filestream = -1;
- char rx = '-';
- time_t startTime;
- time_t actualTime;
- uart0_filestream = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
- if (uart0_filestream == -1) {
- printf("[ERROR] UART open()\n");
- return '-';
- }
- 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 empfangen
- if (uart0_filestream != -1) {
- startTime = time(0);
- while(rx == '-') {
- actualTime = time(0);
- // 1 second to answer, otherwise timeout
- if(difftime(actualTime, startTime) > 3) {
- //return timeout
- return '#';
- }
- rx_length = read(uart0_filestream, &rx, 1);
- }
- if (rx_length < 0) {
- printf("%c", rx);
- rx = '-';
- return rx;
- } else if (rx_length == 0) {
- printf("%c", rx);
- rx = '-';
- return rx;
- }
- return rx;
- } //if uart0
- close(uart0_filestream);
- return rx;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement