-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
69 lines (59 loc) · 1.82 KB
/
serial.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
/* -*- linux-c -*-
*
* Functions for communicating with a serial port on linux.
*
* This file is part of oblsc.
*
* Copyright (C) 2010-2011 Frej Drejhammar <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version
* 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "serial.h"
/*
* Open the tty and set it up.
* 9600 8N1
*
* Return -1 on error or the FD for the tty.
*/
int open_serial(char *name, speed_t baudrate)
{
int serial;
struct termios serial_struct;
if((serial = open(name, O_RDWR | O_NONBLOCK)) < 0) {
perror(name);
return -1;
}
tcgetattr(serial, &serial_struct);
/* Clear the flags we don't want */
serial_struct.c_iflag &= ~(INPCK | ISTRIP | IGNCR | ICRNL |
INLCR | IXOFF | IXON);
serial_struct.c_oflag &= ~(OPOST);
serial_struct.c_lflag &= ~(ISIG | ICANON | ECHO);
serial_struct.c_cflag &= ~(CSTOPB | PARENB | CSIZE);
/* Set the flags we want */
serial_struct.c_iflag |= IGNBRK;
serial_struct.c_cflag |= (CS8 | CREAD) ;
cfsetospeed(&serial_struct, B115200);
cfsetispeed(&serial_struct, B115200);
if(tcsetattr(serial, TCSANOW, &serial_struct) < 0)
perror ("tcsetattr");
return serial;
}