Simple single-color 8x8x8 LED Cube with AVRs
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

unixSerial.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. *
  3. * unixSerial.c
  4. *
  5. * POSIX compatible serial port library
  6. * Uses 8 databits, no parity, 1 stop bit, no handshaking
  7. *
  8. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  9. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  10. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  11. *
  12. * This file is part of LED-Cube.
  13. *
  14. * LED-Cube is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * LED-Cube is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <termios.h>
  32. #include <dirent.h>
  33. #define SEARCH "tty."
  34. #define BAUD B19200
  35. int fd = -1;
  36. // Open the serial port
  37. int serialOpen(char *port) {
  38. struct termios options;
  39. if ((strcmp(ttyname(fileno(stdin)), port) == 0) || (strcmp(ttyname(fileno(stdout)), port) == 0)) {
  40. return -1;
  41. }
  42. if (fd != -1) {
  43. close(fd);
  44. }
  45. fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
  46. if (fd == -1) {
  47. return -1;
  48. }
  49. if (fcntl(fd, F_SETFL, FNDELAY) == -1) { // read() isn't blocking'
  50. close(fd);
  51. return -1;
  52. }
  53. if (tcgetattr(fd, &options) == -1) {
  54. close(fd);
  55. return -1;
  56. }
  57. if (cfsetispeed(&options, BAUD) == -1) { // Set speed
  58. close(fd);
  59. return -1;
  60. }
  61. if (cfsetospeed(&options, BAUD) == -1) {
  62. close(fd);
  63. return -1;
  64. }
  65. options.c_cflag |= (CLOCAL | CREAD);
  66. options.c_cflag &= ~PARENB; // 8N1
  67. options.c_cflag &= ~CSTOPB;
  68. options.c_cflag &= ~CSIZE;
  69. options.c_cflag |= CS8;
  70. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  71. options.c_oflag &= ~OPOST; // Raw output
  72. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  73. if (tcsetattr(fd, TCSANOW, &options) == -1) {
  74. close(fd);
  75. return -1;
  76. }
  77. return 0;
  78. }
  79. // Write to port. Returns number of characters sent, -1 on error
  80. ssize_t serialWrite(char *data, size_t length) {
  81. return write(fd, data, length);
  82. }
  83. // Read from port. Return number of characters read, 0 if none available, -1 on error
  84. ssize_t serialRead(char *data, size_t length) {
  85. return read(fd, data, length);
  86. }
  87. // Close the serial Port
  88. void serialClose(void) {
  89. close(fd);
  90. }
  91. char** namesInDev(int *siz) {
  92. DIR *dir;
  93. struct dirent *ent;
  94. int i = 0, size = 0;
  95. char **files = NULL;
  96. dir = opendir("/dev/");
  97. while ((ent = readdir(dir)) != NULL) {
  98. size++;
  99. }
  100. files = (char **)malloc((size + 1) * sizeof(char *));
  101. files[size] = NULL;
  102. closedir(dir);
  103. dir = opendir("/dev/");
  104. while ((ent = readdir(dir)) != NULL) {
  105. files[i] = (char *)malloc((strlen(ent->d_name) + 1) * sizeof(char));
  106. files[i] = strcpy(files[i], ent->d_name);
  107. i++;
  108. }
  109. closedir(dir);
  110. char *tmp = NULL;
  111. // Fix every string, addin /dev/ in front of it...
  112. for (i = 0; i < size; i++) {
  113. tmp = (char *)malloc((strlen(files[i]) + 6) * sizeof(char));
  114. tmp[0] = '/';
  115. tmp[1] = 'd';
  116. tmp[2] = 'e';
  117. tmp[3] = 'v';
  118. tmp[4] = '/';
  119. files[i] = strncat(tmp, files[i], strlen(files[i]));
  120. }
  121. *siz = size;
  122. return files;
  123. }
  124. char** getSerialPorts(void) {
  125. int size;
  126. char** files = namesInDev(&size);
  127. char** fin = NULL;
  128. int i = 0, j = 0, f, g;
  129. fin = (char **)malloc((size + 1) * sizeof(char *));
  130. fin[size] = NULL;
  131. while (files[i] != NULL) {
  132. if (strstr(files[i], SEARCH) != NULL) {
  133. f = serialOpen(files[i]);
  134. if (fd != -1) {
  135. fin[j++] = files[i];
  136. serialClose();
  137. }
  138. }
  139. i++;
  140. }
  141. free(files);
  142. char** finish = (char **)malloc((j + 1) * sizeof(char *));
  143. for (i = 0; i < (j + 1); i++) {
  144. finish[i] = fin[i];
  145. }
  146. free(fin);
  147. return finish;
  148. }