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

serial.c 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * POSIX compatible
  3. * Compatible API to libSerial (SerialHelper)
  4. * Opens a pseudoterminal, lets you write to it,
  5. * gives name of slave side.
  6. * By: Thomas Buck <taucher.bodensee@gmail.com>
  7. * Visit: www.xythobuz.org
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <termios.h>
  15. #include <dirent.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include "serial.h"
  19. int fd = -1;
  20. // Open pseudo terminal
  21. // return name of slave side or NULL on error.
  22. char *serialOpen() {
  23. struct termios options;
  24. if (fd != -1) {
  25. close(fd);
  26. }
  27. fd = posix_openpt(O_RDWR | O_NOCTTY);
  28. if (fd == -1) {
  29. return NULL;
  30. }
  31. // Set rigths
  32. if (grantpt(fd) == -1) {
  33. return NULL;
  34. }
  35. // Unlock slave
  36. if (unlockpt(fd) == -1) {
  37. return NULL;
  38. }
  39. // fcntl(fd, F_SETFL, FNDELAY); // read non blocking
  40. tcgetattr(fd, &options);
  41. cfsetispeed(&options, BAUD); // Set speed
  42. cfsetospeed(&options, BAUD);
  43. options.c_cflag |= (CLOCAL | CREAD);
  44. options.c_cflag &= ~PARENB; // 8N1
  45. options.c_cflag &= ~CSTOPB;
  46. options.c_cflag &= ~CSIZE;
  47. options.c_cflag |= CS8;
  48. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  49. options.c_oflag &= ~OPOST; // Raw output
  50. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  51. tcsetattr(fd, TCSANOW, &options);
  52. return ptsname(fd);
  53. }
  54. // Write to port. Returns number of characters sent, -1 on error
  55. ssize_t serialWrite(char *data, size_t length) {
  56. return write(fd, data, length);
  57. }
  58. // Read from port. Return number of characters read, 0 if none available, -1 on error
  59. ssize_t serialRead(char *data, size_t length) {
  60. ssize_t temp = read(fd, data, length);
  61. if ((temp == -1) && (errno == EAGAIN)) {
  62. return 0;
  63. } else {
  64. return temp;
  65. }
  66. }
  67. // Close the serial Port
  68. void serialClose(void) {
  69. close(fd);
  70. }