-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusbserial_linux.c
More file actions
119 lines (100 loc) · 4 KB
/
Copy pathusbserial_linux.c
File metadata and controls
119 lines (100 loc) · 4 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* usbserial_linux.c - Arduino communications on a USB to serial converter.
*
* Copyright (C) 2015 Borislav Sapundzhiev <bsapundjiev_AT_gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>
*
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <termios.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "usbserial.h"
static void linux_serial_port_close(struct serial_opt *serial);
static int linux_serial_port_open(struct serial_opt *serial);
static int linux_serial_port_read(int fd, char *read_buffer, size_t max_chars_to_read);
static int linux_serial_port_write(int fd, const char *write_buffer);
static int linux_serial_port_bytes_available(struct serial_opt *serial);
usbserial_ops linux_opts = {
.serial_port_close = linux_serial_port_close,
.serial_port_open = linux_serial_port_open,
.serial_port_read = linux_serial_port_read,
.serial_port_write = linux_serial_port_write,
.serial_port_bytes_available = linux_serial_port_bytes_available,
};
usbserial_ops * serial_initialize(struct serial_opt * options)
{
return &linux_opts;
}
void linux_serial_port_close(struct serial_opt *serial)
{
tcsetattr(serial->handler,TCSANOW,&(serial)->options);
close(serial->handler);
}
int linux_serial_port_open(struct serial_opt *serial)
{
struct termios options;
serial->handler = open(serial->name, O_RDWR | O_NOCTTY | O_NONBLOCK );
if (serial->handler != -1) {
fcntl(serial->handler, F_SETFL, FNDELAY);
tcgetattr(serial->handler, &(serial)->options);
tcgetattr(serial->handler, &options);
cfsetispeed(&options, serial->baud);
cfsetospeed(&options, serial->baud);
//options.c_cflag |= (CLOCAL | CREAD);
//options.c_lflag |= ICANON;
//http://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c
options.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; /* 8-bit characters */
options.c_cflag &= ~PARENB; /* no parity bit */
options.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
options.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
/* setup for non-canonical mode */
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
options.c_oflag &= ~OPOST;
/* fetch bytes as they become available */
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
tcsetattr(serial->handler, TCSANOW, &options);
}
return (serial->handler);
}
int linux_serial_port_read(int fd, char *read_buffer, size_t max_chars_to_read)
{
int chars_read = read(fd, read_buffer, max_chars_to_read);
return chars_read;
}
int linux_serial_port_write(int fd, const char *write_buffer)
{
size_t len = strlen(write_buffer);
size_t bytes_written = write(fd, write_buffer, len);
tcdrain(fd);
return (bytes_written == len);
}
static int linux_serial_port_bytes_available(struct serial_opt *serial)
{
int n = -1;
if (ioctl(serial->handler, FIONREAD, &n) < 0) {
perror("ioctl failed");
return -1;
}
return n;
}