Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. /*
  2.  
  3. * Arduino-serial
  4.  
  5. * --------------
  6.  
  7. *
  8.  
  9. * A simple command-line example program showing how a computer can
  10.  
  11. * communicate with an Arduino board. Works on any POSIX system (Mac/Unix/PC)
  12.  
  13. *
  14.  
  15. *
  16.  
  17. * Compile with something like:
  18.  
  19. * gcc -o arduino-serial arduino-serial.c
  20.  
  21. *
  22.  
  23. * Created 5 December 2006
  24.  
  25. * Copyleft (c) 2006, Tod E. Kurt, tod@todbot.com
  26.  
  27. *
  28.  
  29. *
  30.  
  31. *
  32.  
  33. * Updated 8 December 2006:
  34.  
  35. * Justin McBride discoevered B14400 & B28800 aren't in Linux's termios.h.
  36.  
  37. * I've included his patch, but commented out for now. One really needs a
  38.  
  39. * real make system when doing cross-platform C and I wanted to avoid that
  40.  
  41. * for this little program. Those baudrates aren't used much anyway. :)
  42.  
  43. *
  44.  
  45. * Updated 26 December 2007:
  46.  
  47. * Added ability to specify a delay (so you can wait for Arduino Diecimila)
  48.  
  49. * Added ability to send a binary byte number
  50.  
  51. *
  52.  
  53. */
  54.  
  55.  
  56.  
  57. #include <stdio.h> /* Standard input/output definitions */
  58.  
  59. #include <stdlib.h>
  60.  
  61. #include <stdint.h> /* Standard types */
  62.  
  63. #include <string.h> /* String function definitions */
  64.  
  65. #include <unistd.h> /* UNIX standard function definitions */
  66.  
  67. #include <fcntl.h> /* File control definitions */
  68.  
  69. #include <errno.h> /* Error number definitions */
  70.  
  71. #include <termios.h> /* POSIX terminal control definitions */
  72.  
  73. #include <sys/ioctl.h>
  74.  
  75. #include <getopt.h>
  76.  
  77.  
  78.  
  79. void usage(void);
  80.  
  81. int serialport_init(const char* serialport, int baud);
  82.  
  83. int serialport_writebyte(int fd, uint8_t b);
  84.  
  85. int serialport_write(int fd, const char* str);
  86.  
  87. int serialport_read_until(int fd, char* buf, char until);
  88.  
  89.  
  90.  
  91. void usage(void) {
  92.  
  93. printf("Usage: arduino-serial -p <serialport> [OPTIONS]\n"
  94.  
  95. "\n"
  96.  
  97. "Options:\n"
  98.  
  99. " -h, --help Print this help message\n"
  100.  
  101. " -p, --port=serialport Serial port Arduino is on\n"
  102.  
  103. " -b, --baud=baudrate Baudrate (bps) of Arduino\n"
  104.  
  105. " -s, --send=data Send data to Arduino\n"
  106.  
  107. " -r, --receive Receive data from Arduino & print it out\n"
  108.  
  109. " -n --num=num Send a number as a single byte\n"
  110.  
  111. " -d --delay=millis Delay for specified milliseconds\n"
  112.  
  113. "\n"
  114.  
  115. "Note: Order is important. Set '-b' before doing '-p'. \n"
  116.  
  117. " Used to make series of actions: '-d 2000 -s hello -d 100 -r' \n"
  118.  
  119. " means 'wait 2secs, send 'hello', wait 100msec, get reply'\n"
  120.  
  121. "\n");
  122.  
  123. }
  124.  
  125.  
  126.  
  127. int main(int argc, char *argv[])
  128.  
  129. {
  130.  
  131. int fd = 0;
  132.  
  133. char serialport[256];
  134.  
  135. int baudrate = B115200; // default
  136.  
  137. char buf[20], dat[20], use[1];
  138.  
  139. int rc,n;
  140.  
  141.  
  142.  
  143. //baudrate = 9600;
  144.  
  145. fd = serialport_init("/dev/ttyACM0", baudrate);
  146.  
  147. if(fd==-1) return -1;
  148.  
  149. usleep(3000 * 1000 );
  150.  
  151.  
  152.  
  153. while(1) {
  154.  
  155. strcpy(dat, "00000000:\0");
  156.  
  157. gets(use);
  158.  
  159. if(use[0] == 'i') {
  160.  
  161. dat[0] = 'f';
  162.  
  163. dat[1] = 5;
  164.  
  165. }
  166.  
  167. else if(use[0] == 'k') {
  168.  
  169. dat[0] = 'b';
  170.  
  171. dat[1] = 5;
  172.  
  173. }
  174.  
  175. else if(use[0] == 'j') {
  176.  
  177. dat[2] = 'f';
  178.  
  179. dat[3] = 5;
  180.  
  181. }
  182.  
  183. else if(use[0] == 'l') {
  184.  
  185. dat[2] = 'b';
  186.  
  187. dat[3] = 5;
  188.  
  189. }
  190.  
  191.  
  192.  
  193. rc = serialport_write(fd, dat);
  194.  
  195. if(rc==-1) return -1;
  196.  
  197. //printf("Waiting until UART buffer clears: %d\n", tcdrain(fd));
  198.  
  199. n = serialport_read_until(fd, buf, ':');
  200.  
  201. printf("wrote %d bytes, read %d bytes: %s\n", rc, n, buf);
  202.  
  203. }
  204.  
  205.  
  206.  
  207. close(fd);
  208.  
  209.  
  210.  
  211. exit(EXIT_SUCCESS);
  212.  
  213. } // end main
  214.  
  215.  
  216.  
  217. int serialport_writebyte( int fd, uint8_t b)
  218.  
  219. {
  220.  
  221. int n = write(fd,&b,1);
  222.  
  223. if( n!=1)
  224.  
  225. return -1;
  226.  
  227. return 0;
  228.  
  229. }
  230.  
  231.  
  232.  
  233. int serialport_write(int fd, const char* str)
  234.  
  235. {
  236.  
  237. int len = strlen(str);
  238.  
  239. int n = write(fd, str, len);
  240.  
  241. if( n!=len )
  242.  
  243. return -1;
  244.  
  245. return n;
  246.  
  247. }
  248.  
  249.  
  250.  
  251. int serialport_read_until(int fd, char* buf, char until)
  252.  
  253. {
  254.  
  255. char b[1];
  256.  
  257. int i=0;
  258.  
  259. do {
  260.  
  261. int n = read(fd, b, 1); // read a char at a time
  262.  
  263. if( n==-1) return -1; // couldn't read
  264.  
  265. if( n==0 ) {
  266.  
  267. usleep( 10 * 1000 ); // wait 10 msec try again
  268.  
  269. continue;
  270.  
  271. }
  272.  
  273. buf[i] = b[0]; i++;
  274.  
  275. } while( b[0] != until );
  276.  
  277.  
  278.  
  279. buf[i] = 0; // null terminate the string
  280.  
  281. return i;
  282.  
  283. }
  284.  
  285.  
  286.  
  287. // takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
  288.  
  289. // and a baud rate (bps) and connects to that port at that speed and 8N1.
  290.  
  291. // opens the port in fully raw mode so you can send binary data.
  292.  
  293. // returns valid fd, or -1 on error
  294.  
  295. int serialport_init(const char* serialport, int baud)
  296.  
  297. {
  298.  
  299. struct termios toptions;
  300.  
  301. int fd;
  302.  
  303.  
  304.  
  305. //fprintf(stderr,"init_serialport: opening port %s @ %d bps\n",
  306.  
  307. // serialport,baud);
  308.  
  309.  
  310.  
  311. //fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
  312.  
  313. fd = open(serialport, O_RDWR | O_NOCTTY);
  314.  
  315. if (fd == -1) {
  316.  
  317. perror("init_serialport: Unable to open port ");
  318.  
  319. return -1;
  320.  
  321. }
  322.  
  323.  
  324.  
  325. if (tcgetattr(fd, &toptions) < 0) {
  326.  
  327. perror("init_serialport: Couldn't get term attributes");
  328.  
  329. return -1;
  330.  
  331. }
  332.  
  333. speed_t brate = baud; // let you override switch below if needed
  334.  
  335. switch(baud) {
  336.  
  337. case 4800: brate=B4800; break;
  338.  
  339. case 9600: brate=B9600; break;
  340.  
  341. // if you want these speeds, uncomment these and set #defines if Linux
  342.  
  343. //#ifndef OSNAME_LINUX
  344.  
  345. // case 14400: brate=B14400; break;
  346.  
  347. //#endif
  348.  
  349. case 19200: brate=B19200; break;
  350.  
  351. //#ifndef OSNAME_LINUX
  352.  
  353. // case 28800: brate=B28800; break;
  354.  
  355. //#endif
  356.  
  357. //case 28800: brate=B28800; break;
  358.  
  359. case 38400: brate=B38400; break;
  360.  
  361. case 57600: brate=B57600; break;
  362.  
  363. case 115200: brate=B115200; break;
  364.  
  365. }
  366.  
  367. cfsetispeed(&toptions, brate);
  368.  
  369. cfsetospeed(&toptions, brate);
  370.  
  371.  
  372.  
  373. // 8N1
  374.  
  375. toptions.c_cflag &= ~PARENB;
  376.  
  377. toptions.c_cflag &= ~CSTOPB;
  378.  
  379. toptions.c_cflag &= ~CSIZE;
  380.  
  381. toptions.c_cflag |= CS8;
  382.  
  383. // no flow control
  384.  
  385. toptions.c_cflag &= ~CRTSCTS;
  386.  
  387.  
  388.  
  389. toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
  390.  
  391. toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  392.  
  393.  
  394.  
  395. toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
  396.  
  397. toptions.c_oflag &= ~OPOST; // make raw
  398.  
  399.  
  400.  
  401. // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
  402.  
  403. toptions.c_cc[VMIN] = 0;
  404.  
  405. toptions.c_cc[VTIME] = 20;
  406.  
  407.  
  408.  
  409. if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
  410.  
  411. perror("init_serialport: Couldn't set term attributes");
  412.  
  413. return -1;
  414.  
  415. }
  416.  
  417.  
  418.  
  419. return fd;
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement