Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

serial.c 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 pseudo terminal
  18. // return name of slave side or NULL on error.
  19. char *serialOpen() {
  20. struct termios options;
  21. if (fd != -1) {
  22. close(fd);
  23. }
  24. fd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY);
  25. if (fd == -1) {
  26. return NULL;
  27. }
  28. // Set rigths
  29. if (grantpt(fd) != 0) {
  30. return NULL;
  31. }
  32. // Unlock slave
  33. if (unlockpt(fd) != 0) {
  34. return NULL;
  35. }
  36. fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
  37. tcgetattr(fd, &options);
  38. cfsetispeed(&options, BAUD); // Set speed
  39. cfsetospeed(&options, BAUD);
  40. options.c_cflag |= (CLOCAL | CREAD);
  41. options.c_cflag &= ~PARENB; // 8N1
  42. options.c_cflag &= ~CSTOPB;
  43. options.c_cflag &= ~CSIZE;
  44. options.c_cflag |= CS8;
  45. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  46. options.c_oflag &= ~OPOST; // Raw output
  47. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  48. tcsetattr(fd, TCSANOW, &options);
  49. return ptsname(fd);
  50. }
  51. // Write to port. Returns number of characters sent, -1 on error
  52. ssize_t serialWrite(char *data, size_t length) {
  53. return write(fd, data, length);
  54. }
  55. // Read from port. Return number of characters read, 0 if none available, -1 on error
  56. ssize_t serialRead(char *data, size_t length) {
  57. ssize_t temp = read(fd, data, length);
  58. if ((temp == -1) && (errno == EAGAIN)) {
  59. return 0;
  60. } else {
  61. return temp;
  62. }
  63. }
  64. // Close the serial Port
  65. void serialClose(void) {
  66. close(fd);
  67. }