1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <dirent.h>
- #include <errno.h>
-
- #include "serial.h"
-
- int fd = -1;
-
-
-
- char *serialOpen() {
- struct termios options;
-
- if (fd != -1) {
- close(fd);
- }
-
- fd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd == -1) {
- return NULL;
- }
-
-
- if (grantpt(fd) != 0) {
- return NULL;
- }
-
-
- if (unlockpt(fd) != 0) {
- return NULL;
- }
-
- fcntl(fd, F_SETFL, FNDELAY);
- tcgetattr(fd, &options);
- cfsetispeed(&options, BAUD);
- cfsetospeed(&options, BAUD);
- options.c_cflag |= (CLOCAL | CREAD);
-
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS8;
-
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- options.c_oflag &= ~OPOST;
- options.c_iflag &= ~(IXON | IXOFF | IXANY);
-
- tcsetattr(fd, TCSANOW, &options);
-
- return ptsname(fd);
- }
-
-
- ssize_t serialWrite(char *data, size_t length) {
- return write(fd, data, length);
- }
-
-
- ssize_t serialRead(char *data, size_t length) {
- ssize_t temp = read(fd, data, length);
- if ((temp == -1) && (errno == EAGAIN)) {
- return 0;
- } else {
- return temp;
- }
- }
-
-
- void serialClose(void) {
- close(fd);
- }
|