houxiao
2017-08-17 2b43077d967c28fe99e1ff2b99f19e1433c710d9
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright 2012 - 2014 Thomas Buck <xythobuz@xythobuz.de>
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <dirent.h>
#include <errno.h>
#include <time.h>
#include <poll.h>
 
#include "serial.h"
 
#ifndef XON
#define XON 0x11
#endif
 
#ifndef XOFF
#define XOFF 0x13
#endif
 
#ifndef TIMEOUT
#define TIMEOUT 2
#endif
 
int serialOpen(const char *port, unsigned int baud) {
    struct termios options;
 
    int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        fprintf(stderr, "Couldn't open port \"%s\": %s\n", port, strerror(errno));
        return -1;
    }
 
    tcgetattr(fd, &options);
 
    options.c_lflag = 0;
    options.c_oflag = 0;
    options.c_iflag = 0;
 
    // Set Baudrate
    switch (baud) {
        case 2400:
            cfsetispeed(&options, B2400);
            cfsetospeed(&options, B2400);
            break;
        case 9600:
            cfsetispeed(&options, B9600);
            cfsetospeed(&options, B9600);
            break;
        case 19200:
            cfsetispeed(&options, B19200);
            cfsetospeed(&options, B19200);
            break;
        case 38400:
            cfsetispeed(&options, B38400);
            cfsetospeed(&options, B38400);
            break;
        //case 76800:
        //    cfsetispeed(&options, B76800);
        //    cfsetospeed(&options, B76800);
        //    break;
        case 115200:
            cfsetispeed(&options, B115200);
            cfsetospeed(&options, B115200);
            break;
        default:
            fprintf(stderr, "Warning: Baudrate not supported!\n");
            serialClose(fd);
            return -1;
    }
 
    // Input Modes
    options.c_iflag |= IGNCR; // Ignore CR
#ifdef XONXOFF
    options.c_iflag |= IXON; // XON-XOFF Flow Control
#endif
 
    // Output Modes
    options.c_oflag |= OPOST; // Post-process output
 
    // Control Modes
    options.c_cflag |= CS8; // 8 data bits
    options.c_cflag |= CREAD; // Enable Receiver
    options.c_cflag |= CLOCAL; // Ignore modem status lines
 
    // Local Modes
    options.c_lflag |= IEXTEN; // Extended input character processing
 
    // Special characters
    options.c_cc[VMIN] = 0; // Always return...
    options.c_cc[VTIME] = 0; // ..immediately from read()
#ifdef XONXOFF
    options.c_cc[VSTOP] = XOFF;
    options.c_cc[VSTART] = XON;
#endif
 
    tcsetattr(fd, TCSANOW, &options);
 
    tcflush(fd, TCIOFLUSH);
 
    return fd;
}
 
void serialClose(int fd) {
    tcflush(fd, TCIOFLUSH);
    close(fd);
}
 
int serialHasChar(int fd) {
    struct pollfd fds;
    fds.fd = fd;
    fds.events = (POLLIN | POLLPRI); // Data may be read
    if (poll(&fds, 1, 0) > 0) {
        return 1;
    } else {
        return 0;
    }
}
 
void serialWaitUntilSent(int fd) {
    while (tcdrain(fd) == -1) {
        fprintf(stderr, "Could not drain data: %s\n", strerror(errno));
    }
}
 
unsigned int serialWriteRaw(int fd, const char *d, int len) {
    unsigned int processed = 0;
    time_t start = time(NULL);
 
    while ((processed < len) && (difftime(time(NULL), start) < TIMEOUT)) {
        int t = write(fd, (d + processed), (len - processed));
        if (t == -1) {
            fprintf(stderr, "Error while writing: %s\n", strerror(errno));
            return processed;
        } else {
            processed += t;
        }
    }
 
    return processed;
}
 
unsigned int serialReadRaw(int fd, char *d, int len) {
    unsigned int processed = 0;
    time_t start = time(NULL);
 
    while ((processed < len) && (difftime(time(NULL), start) < TIMEOUT)) {
        int t = read(fd, (d + processed), (len - processed));
        if (t == -1) {
            fprintf(stderr, "Error while reading: %s\n", strerror(errno));
            return processed;
        } else {
            processed += t;
        }
    }
 
    return processed;
}
 
void serialWriteChar(int fd, char c) {
    while (serialWriteRaw(fd, &c, 1) != 1);
}
 
void serialReadChar(int fd, char *c) {
    while (serialReadRaw(fd, c, 1) != 1);
#ifdef XONXOFF
    if (*c == XON) {
        if (tcflow(fd, TCOON) == -1) {
            fprintf(stderr, "Could not restart flow: %s\n", strerror(errno));
        }
        serialReadChar(fd, c);
    } else if (*c == XOFF) {
        if (tcflow(fd, TCOOFF) == -1) {
            fprintf(stderr, "Could not stop flow: %s\n", strerror(errno));
        }
        serialReadChar(fd, c);
    }
#endif
}
 
void serialWriteString(int fd, const char *s) {
    while (*s)
        serialWriteChar(fd, *(s++));
}
 
char** getSerialPorts(void) {
    DIR *dir;
    struct dirent *ent;
 
    int size = 0;
    dir = opendir("/dev/");
    while ((ent = readdir(dir)) != NULL) {
#ifdef SEARCH
        if (strstr(ent->d_name, SEARCH) != NULL)
#endif
        size++;
    }
    closedir(dir);
 
    char **files = (char **)malloc((size + 1) * sizeof(char *));
 
    int i = 0;
    dir = opendir("/dev/");
    while (((ent = readdir(dir)) != NULL) && (i < size)) {
 
#ifdef SEARCH
        if (strstr(ent->d_name, SEARCH) != NULL) {
#endif
 
            int tmp = strlen(ent->d_name) + 6;
            files[i] = (char *)malloc(tmp * sizeof(char));
            strcpy(files[i], "/dev/");
            strcpy(files[i] + 5, ent->d_name);
            files[i][tmp - 1] = '\0';
 
#ifdef TRY_TO_OPEN_PORTS
            int fdtmp = serialOpen(files[i], 9600);
            if (fdtmp != -1) {
                serialClose(fdtmp);
#endif
 
                i++;
 
#ifdef TRY_TO_OPEN_PORTS
            } else {
                free(files[i]);
            }
#endif
 
#ifdef SEARCH
        }
#endif
 
    }
    closedir(dir);
    files[i] = NULL;
    return files;
}