Advertisement
RybaSG

serial

Dec 13th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. // C library headers
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. // Linux headers
  6. #include <fcntl.h> // Contains file controls like O_RDWR
  7. #include <errno.h> // Error integer and strerror() function
  8. #include <termios.h> // Contains POSIX terminal control definitions
  9. #include <unistd.h> // write(), read(), close()
  10.  
  11. #include <iostream>
  12.  
  13.  
  14. int serial(const char *port, uint8_t *dataToSend, int dataSize, uint8_t *responseData)
  15. {
  16. // Open the serial port. Change device path as needed (currently set to an standard FTDI USB-UART cable type device)
  17. int serial_port = open(port, O_RDWR);
  18.  
  19. // Create new termios struc, we call it 'tty' for convention
  20. struct termios tty;
  21. memset(&tty, 0, sizeof tty);
  22.  
  23. // Read in existing settings, and handle any error
  24. if (tcgetattr(serial_port, &tty) != 0)
  25. {
  26. printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
  27. }
  28.  
  29. tty.c_cflag |= PARENB; // Clear parity bit, disabling parity (most common)
  30. tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
  31. tty.c_cflag |= CS8; // 8 bits per byte (most common)
  32. tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
  33. tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
  34.  
  35. tty.c_lflag &= ~ICANON;
  36. tty.c_lflag &= ~ECHO; // Disable echo
  37. tty.c_lflag &= ~ECHOE; // Disable erasure
  38. tty.c_lflag &= ~ECHONL; // Disable new-line echo
  39. tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
  40. tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
  41. tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes
  42.  
  43. tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
  44. tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
  45. // tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX)
  46. // tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)
  47.  
  48. tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
  49. tty.c_cc[VMIN] = 0;
  50.  
  51. // Set in/out baud rate to be 115200
  52. cfsetispeed(&tty, B115200);
  53. cfsetospeed(&tty, B115200);
  54.  
  55. // Save tty settings, also checking for error
  56. if (tcsetattr(serial_port, TCSANOW, &tty) != 0)
  57. {
  58. printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
  59. }
  60.  
  61. write(serial_port, dataToSend, dataSize);
  62.  
  63. uint8_t read_buf[1024];
  64. int readBytes = read(serial_port, &read_buf, sizeof(read_buf));
  65.  
  66. for(int i = 0; i < readBytes; i++)
  67. {
  68. responseData[i] = read_buf[i];
  69. }
  70. memset(read_buf, 0, sizeof(read_buf));
  71.  
  72. return readBytes;
  73. // close(serial_port);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement