-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_serial.c
94 lines (68 loc) · 1.59 KB
/
usb_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
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
/*
* usb_serial.c
*/
#include "usb_serial.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
struct termios ttyUSB;
int ttyFD;
uchar *readUSBFrame(uchar *frame)
{
return NULL;
}
uchar *writeUSBFrame(uchar *frame)
{
return NULL;
}
uchar *initTTY(char *ttyPath)
{
if (ttyPath == NULL) {
fprintf(stderr, "Didn't pass path to initTTY!\n");
return NULL;
}
ttyFD = open(ttyPath, O_RDWR | O_NOCTTY | O_NDELAY);
if (ttyFD < 0) {
fprintf(stderr, "Couldn't open file at %s!\n", ttyPath);
return NULL;
}
if (!isatty(ttyFD)) {
fprintf(stderr, "The file specified isn't a tty!\n");
return NULL;
}
memset(&ttyUSB, 0, sizeof(struct termios));
if (tcgetattr(ttyFD, &ttyUSB) < 0) {
fprintf(stderr, "Failed to get attributes from ttyFD!\n");
return NULL;
}
ttyUSB.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR |
PARMRK | INPCK | ISTRIP | IXON);
ttyUSB.c_oflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
ttyUSB.c_lflag &= 0;
ttyUSB.c_cflag &= ~(CSIZE | PARENB);
ttyUSB.c_cflag |= CS8;
ttyUSB.c_cc[VMIN] = 1;
ttyUSB.c_cc[VTIME] = 0;
if ((cfsetispeed(&ttyUSB, B9600) < 0) || (cfsetospeed(&ttyUSB, B9600) < 0)) {
fprintf(stderr, "Failed to set baud rate!\n");
return NULL;
}
if (tcsetattr(ttyFD, TCSAFLUSH, &ttyUSB) < 0) {
fprintf(stderr, "Failed to set tty configuration!\n");
return NULL;
}
return (uchar *) 0;
};
uchar *closeTTY()
{
close(ttyFD);
return (uchar *) 0;
};
int getTTYFD()
{
return ttyFD;
}