-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_port.c
More file actions
68 lines (53 loc) · 1.79 KB
/
Copy pathserial_port.c
File metadata and controls
68 lines (53 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Created by rleroux on 4/21/24.
//
#include "serial_port.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
int open_port_blocking_io(const char *file) {
return open(file, O_RDWR | O_NOCTTY);
}
int close_port(const int fd) {
return close(fd);
}
int set_port_access_exlusive(const int fd) {
return ioctl(fd, TIOCEXCL);
}
int set_port_access_nonexclusive(const int fd) {
return ioctl(fd, TIOCNXCL);
}
int configure_port(const int fd, const cc_t vtime, const cc_t vmin, const speed_t speed) {
struct termios tty;
if (tcgetattr(fd, &tty) != 0) {
return -1;
}
/*
* Disable any special handling of received bytes
* termios_p->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
*
* Prevent special interpretation of output bytes (e.g. newline chars)
* termios_p->c_oflag &= ~OPOST;
*
* Disable echo, use non-canonical mode, disable interpretation of INTR, QUIT and SUSP
* termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
*
* Clear parity bit, disabling parity, 8 bits per byte
* termios_p->c_cflag &= ~(CSIZE | PARENB);
* termios_p->c_cflag |= CS8;
*/
cfmakeraw(&tty);
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
tty.c_cc[VTIME] = vtime;
tty.c_cc[VMIN] = vmin;
// Set in/out baud rate
cfsetspeed(&tty, speed);
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
return -1;
}
return 0;
}