/firmware/toggleDTR.c

http://github.com/makerbot/G3Firmware · C · 58 lines · 41 code · 11 blank · 6 comment · 7 complexity · 2f6d0b06f10a46aa14ac7008f6ab739d MD5 · raw file

  1. #include <sys/ioctl.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <termios.h>
  8. void setDTR(int fd, int high) {
  9. unsigned int result = 0;
  10. ioctl( fd, TIOCMGET, &result );
  11. if (high) result |= TIOCM_DTR;
  12. else result &= ~TIOCM_DTR;
  13. ioctl( fd, TIOCMSET, &result );
  14. printf( "setDTR( %i )\n", high );
  15. }
  16. void main(int argc, char** argv) {
  17. unsigned long i = 0;
  18. int fd;
  19. if (argv[1] != 0) {
  20. fd = open(argv[1], O_RDWR || O_NONBLOCK);
  21. } else {
  22. fd = open("/dev/ttyUSB0", O_RDWR || O_NONBLOCK);
  23. }
  24. printf("opened: %d\n",fd);
  25. // adjust serial communuication parameters
  26. struct termios ComParams;
  27. tcgetattr(fd, &ComParams);
  28. ComParams.c_cflag &= ~CBAUD; // baud rate = 9600 bd
  29. ComParams.c_cflag |= B57600;
  30. tcsetattr( fd, TCSANOW, &ComParams );
  31. // play with RTS & DTR
  32. int iFlags;
  33. // turn on RTS
  34. iFlags = TIOCM_RTS;
  35. ioctl(fd, TIOCMBIS, &iFlags);
  36. // turn off RTS
  37. iFlags = TIOCM_RTS;
  38. ioctl(fd, TIOCMBIC, &iFlags);
  39. // turn on DTR
  40. iFlags = TIOCM_DTR;
  41. ioctl(fd, TIOCMBIS, &iFlags);
  42. sleep(1);
  43. // turn off DTR
  44. iFlags = TIOCM_DTR;
  45. ioctl(fd, TIOCMBIC, &iFlags);
  46. close(fd);
  47. }