Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

serial.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * POSIX compatible serial port library
  3. * Uses 8 databits, no parity, 1 stop bit, no handshaking
  4. * By: Thomas Buck <taucher.bodensee@gmail.com>
  5. * Visit: www.xythobuz.org
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <termios.h>
  13. #include <dirent.h>
  14. #include <errno.h>
  15. #include "serial.h"
  16. int fd = -1;
  17. // Open the serial port
  18. int serialOpen(char *port) {
  19. struct termios options;
  20. if (fd != -1) {
  21. close(fd);
  22. }
  23. fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
  24. if (fd == -1) {
  25. return -1;
  26. }
  27. fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
  28. tcgetattr(fd, &options);
  29. cfsetispeed(&options, BAUD); // Set speed
  30. cfsetospeed(&options, BAUD);
  31. options.c_cflag |= (CLOCAL | CREAD);
  32. options.c_cflag &= ~PARENB; // 8N1
  33. options.c_cflag &= ~CSTOPB;
  34. options.c_cflag &= ~CSIZE;
  35. options.c_cflag |= CS8;
  36. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  37. options.c_oflag &= ~OPOST; // Raw output
  38. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  39. tcsetattr(fd, TCSANOW, &options);
  40. return 0;
  41. }
  42. // Write to port. Returns number of characters sent, -1 on error
  43. ssize_t serialWrite(char *data, size_t length) {
  44. return write(fd, data, length);
  45. }
  46. // Read from port. Return number of characters read, 0 if none available, -1 on error
  47. ssize_t serialRead(char *data, size_t length) {
  48. ssize_t temp = read(fd, data, length);
  49. if ((temp == -1) && (errno == EAGAIN)) {
  50. return 0;
  51. } else {
  52. return temp;
  53. }
  54. }
  55. // Close the serial Port
  56. void serialClose(void) {
  57. close(fd);
  58. }
  59. char** namesInDev(int *siz) {
  60. DIR *dir;
  61. struct dirent *ent;
  62. int i = 0, size = 0;
  63. char **files = NULL;
  64. dir = opendir("/dev/");
  65. while ((ent = readdir(dir)) != NULL) {
  66. size++;
  67. }
  68. files = (char **)malloc((size + 1) * sizeof(char *));
  69. files[size++] = NULL;
  70. closedir(dir);
  71. dir = opendir("/dev/");
  72. while ((ent = readdir(dir)) != NULL) {
  73. files[i] = (char *)malloc((strlen(ent->d_name) + 1) * sizeof(char));
  74. files[i] = strcpy(files[i], ent->d_name);
  75. i++;
  76. }
  77. closedir(dir);
  78. char *tmp = NULL;
  79. // Fix every string, addin /dev/ in front of it...
  80. for (i = 0; i < (size - 1); i++) {
  81. tmp = (char *)malloc((strlen(files[i]) + 6) * sizeof(char));
  82. tmp[0] = '/';
  83. tmp[1] = 'd';
  84. tmp[2] = 'e';
  85. tmp[3] = 'v';
  86. tmp[4] = '/';
  87. files[i] = strncat(tmp, files[i], strlen(files[i]));
  88. }
  89. *siz = size;
  90. return files;
  91. }
  92. char** getSerialPorts() {
  93. int size;
  94. char** files = namesInDev(&size);
  95. char **fin = NULL, **finish = NULL;
  96. int i = 0, j = 0, f, g;
  97. fin = (char **)malloc(size * sizeof(char *));
  98. // Has space for all files in dev!
  99. while (files[i] != NULL) {
  100. // Filter for SEARCH and if it is a serial port
  101. if (strstr(files[i], SEARCH) != NULL) {
  102. fin[j++] = files[i];
  103. } else {
  104. free(files[i]);
  105. }
  106. i++;
  107. }
  108. free(files);
  109. // Copy in memory with matching size, NULL at end
  110. finish = (char **)malloc((j + 1) * sizeof(char *));
  111. finish[j] = NULL;
  112. for (i = 0; i < j; i++) {
  113. finish[i] = fin[i];
  114. }
  115. free(fin);
  116. return finish;
  117. }