-
Notifications
You must be signed in to change notification settings - Fork 0
/
xfer.c
72 lines (64 loc) · 1.41 KB
/
xfer.c
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
69
70
71
72
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
static int set_interface_attribs(int fd, int speed, int parity)
{
struct termios tty;
if(tcgetattr(fd, &tty) != 0)
{
fprintf(stderr, "(tcgetattr) Error %d\n", errno);
return -1;
}
cfsetospeed(&tty, speed);
cfsetispeed(&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag &= ~IGNBRK;
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 5;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if(tcsetattr(fd, TCSANOW, &tty) != 0)
{
fprintf(stderr, "(tcsetattr) Error %d\n", errno);
return -1;
}
return 0;
}
static int set_blocking(int fd, int should_block)
{
struct termios tty;
memset(&tty, 0, sizeof tty);
if(tcgetattr(fd, &tty) != 0)
{
fprintf(stderr, "(tcgetattr) Error %d\n", errno);
return -1;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5;
if(tcsetattr(fd, TCSANOW, &tty) != 0)
{
fprintf(stderr, "(tcsetattr) Error %d\n", errno);
return -1;
}
return 0;
}
static int init_fd(char *port)
{
int fd = open(port, O_RDWR | O_NOCTTY | O_SYNC);
if(fd < 0)
{
printf("(open) Error %d: %s\n", errno, strerror(errno));
return -1;
}
set_interface_attribs(fd, B1200, 0);
set_blocking(fd, 1);
return fd;
}