T12.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <termios.h>
  6. int main() {
  7. // 1. Open the serial port
  8. int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_SYNC);
  9. if (fd < 0) {
  10. perror("Error opening serial port");
  11. return 1;
  12. }
  13. struct termios tty;
  14. // 2. Get current terminal attributes
  15. if (tcgetattr(fd, &tty) != 0) {
  16. perror("Error from tcgetattr");
  17. close(fd);
  18. return 1;
  19. }
  20. // 3. Apply raw mode configuration
  21. cfmakeraw(&tty);
  22. // 4. Set additional custom settings (Example: 115200 Baud rate)
  23. cfsetispeed(&tty, B115200);
  24. cfsetospeed(&tty, B115200);
  25. // VMIN and VTIME define reading behavior
  26. tty.c_cc[VMIN] = 1; // Block until at least 1 byte is read
  27. tty.c_cc[VTIME] = 0; // No inter-character timer
  28. // 5. Apply the modified attributes immediately
  29. if (tcsetattr(fd, TCSANOW, &tty) != 0) {
  30. perror("Error from tcsetattr");
  31. close(fd);
  32. return 1;
  33. }
  34. printf("Serial port configured in raw mode.\n");
  35. // ... perform your read/write operations here ...
  36. close(fd);
  37. return 0;
  38. }