Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unixSerial.c 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 <unistd.h>
  9. #include <fcntl.h>
  10. #include <termios.h>
  11. #define BAUD B19200
  12. int fd = -1;
  13. // Open the serial port
  14. int serialOpen(char *port) {
  15. struct termios options;
  16. if (fd != -1) {
  17. close(fd);
  18. }
  19. fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
  20. if (fd == -1) {
  21. return -1;
  22. }
  23. fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
  24. tcgetattr(fd, &options);
  25. cfsetispeed(&options, BAUD); // Set speed
  26. cfsetospeed(&options, BAUD);
  27. options.c_cflag |= (CLOCAL | CREAD);
  28. options.c_cflag &= ~PARENB; // 8N1
  29. options.c_cflag &= ~CSTOPB;
  30. options.c_cflag &= ~CSIZE;
  31. options.c_cflag |= CS8;
  32. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  33. options.c_oflag &= ~OPOST; // Raw output
  34. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  35. tcsetattr(fd, TCSANOW, &options);
  36. return 0;
  37. }
  38. // Write to port. Returns number of characters sent, -1 on error
  39. ssize_t serialWrite(char *data, size_t length) {
  40. return write(fd, data, length);
  41. }
  42. // Read from port. Return number of characters read, 0 if none available, -1 on error
  43. ssize_t serialRead(char *data, size_t length) {
  44. return read(fd, data, length);
  45. }
  46. // Close the serial Port
  47. void serialClose(void) {
  48. close(fd);
  49. }