-
Notifications
You must be signed in to change notification settings - Fork 8
/
port_open.c
40 lines (30 loc) · 838 Bytes
/
port_open.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
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#define baudrate B500000
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Serial port wasn't provided!\n");
return 1;
}
int usbdev;
struct termios options;
usbdev = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
if (usbdev == -1)
perror("open_port : Unable to open:");
tcgetattr(usbdev, &options);
cfsetispeed(&options, baudrate);
cfsetospeed(&options, baudrate);
options.c_cflag |= CS8;
options.c_iflag |= IGNBRK;
options.c_iflag &= ~( BRKINT | ICRNL | IMAXBEL | IXON);
options.c_oflag &= ~( OPOST | ONLCR );
options.c_lflag &= ~( ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE);
options.c_lflag |= NOFLSH;
options.c_cflag &= ~CRTSCTS;
tcsetattr(usbdev, TCSANOW, &options);
return 0;
}