Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. char receiveCommand() {
  2.  
  3. int rx_length;
  4. int uart0_filestream = -1;
  5. char rx = '-';
  6. time_t startTime;
  7. time_t actualTime;
  8.  
  9. uart0_filestream = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
  10. if (uart0_filestream == -1) {
  11. printf("[ERROR] UART open()\n");
  12. return '-';
  13. }
  14.  
  15. struct termios options;
  16. tcgetattr(uart0_filestream, &options);
  17. options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  18. options.c_iflag = IGNPAR;
  19. options.c_oflag = 0;
  20. options.c_lflag = 0;
  21. tcflush(uart0_filestream, TCIFLUSH);
  22. tcsetattr(uart0_filestream, TCSANOW, &options);
  23.  
  24. // Bytes empfangen
  25. if (uart0_filestream != -1) {
  26.  
  27. startTime = time(0);
  28.  
  29. while(rx == '-') {
  30. actualTime = time(0);
  31. // 1 second to answer, otherwise timeout
  32. if(difftime(actualTime, startTime) > 3) {
  33. //return timeout
  34. return '#';
  35. }
  36.  
  37. rx_length = read(uart0_filestream, &rx, 1);
  38. }
  39.  
  40. if (rx_length < 0) {
  41. printf("%c", rx);
  42. rx = '-';
  43. return rx;
  44.  
  45. } else if (rx_length == 0) {
  46. printf("%c", rx);
  47. rx = '-';
  48. return rx;
  49. }
  50.  
  51. return rx;
  52.  
  53. } //if uart0
  54.  
  55. close(uart0_filestream);
  56. return rx;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement