PageRenderTime 13ms CodeModel.GetById 9ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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