12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
-
-
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
-
- #define BAUD B19200
-
- int fd = -1;
-
-
- int serialOpen(char *port) {
- struct termios options;
-
- if (fd != -1) {
- close(fd);
- }
- fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd == -1) {
- return -1;
- }
-
- 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 0;
- }
-
-
- ssize_t serialWrite(char *data, size_t length) {
- return write(fd, data, length);
- }
-
-
- ssize_t serialRead(char *data, size_t length) {
- return read(fd, data, length);
- }
-
-
- void serialClose(void) {
- close(fd);
- }
|