Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <unistd.h>
- #define BAUDRATE B9600
- #define MODEMDEVICE "/dev/ttyUSB0"
- #define _POSIX_SOURCE 1 /* POSIX compliant source */
- int main()
- {
- int fd, res;
- struct termios oldtio, newtio;
- char buf[255];
- // Open the serial port
- fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
- if (fd < 0)
- {
- perror(MODEMDEVICE);
- exit(1);
- }
- // Save current port settings
- tcgetattr(fd, &oldtio);
- // Set new port settings
- memset(&newtio, 0, sizeof(newtio));
- newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
- newtio.c_iflag = IGNPAR;
- newtio.c_oflag = 0;
- // Set input mode to non-canonical
- newtio.c_lflag = 0;
- // Set timeouts to 0.1 seconds
- newtio.c_cc[VTIME] = 1;
- newtio.c_cc[VMIN] = 0;
- // Apply new settings
- tcflush(fd, TCIFLUSH);
- tcsetattr(fd, TCSANOW, &newtio);
- // Send data
- char data[] = "Hey, it's Eden\n";
- write(fd, data, strlen(data));
- // Read response
- while (1)
- {
- res = read(fd, buf, sizeof(buf));
- if (res > 0)
- {
- buf[res] = 0;
- printf("Received response: %s", buf);
- break;
- }
- }
- // Restore old port settings and close serial port
- tcsetattr(fd, TCSANOW, &oldtio);
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement