| 1 |
/* |
| 2 |
Cross-platform serial / RS232 library |
| 3 |
Version 0.21, 11/10/2015 |
| 4 |
-> LINUX and MacOS implementation |
| 5 |
-> rs232-linux.c |
| 6 |
|
| 7 |
The MIT License (MIT) |
| 8 |
|
| 9 |
Copyright (c) 2013-2015 Frédéric Meslin, Florent Touchard |
| 10 |
Email: [email protected] |
| 11 |
Website: www.fredslab.net |
| 12 |
Twitter: @marzacdev |
| 13 |
|
| 14 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 |
of this software and associated documentation files (the "Software"), to deal |
| 16 |
in the Software without restriction, including without limitation the rights |
| 17 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 18 |
copies of the Software, and to permit persons to whom the Software is |
| 19 |
furnished to do so, subject to the following conditions: |
| 20 |
|
| 21 |
The above copyright notice and this permission notice shall be included in all |
| 22 |
copies or substantial portions of the Software. |
| 23 |
|
| 24 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 25 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 26 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 27 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 28 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 29 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 |
SOFTWARE. |
| 31 |
|
| 32 |
*/ |
| 33 |
|
| 34 |
#if defined(__unix__) || defined(__unix) || \ |
| 35 |
defined(__APPLE__) && defined(__MACH__) |
| 36 |
|
| 37 |
#define _DARWIN_C_SOURCE |
| 38 |
|
| 39 |
#include "rs232.h" |
| 40 |
|
| 41 |
#include <unistd.h> |
| 42 |
#define __USE_MISC // For CRTSCTS |
| 43 |
#include <termios.h> |
| 44 |
#include <fcntl.h> |
| 45 |
#include <dirent.h> |
| 46 |
|
| 47 |
#define __USE_SVID // For strdup |
| 48 |
#include <stdlib.h> |
| 49 |
#include <stdio.h> |
| 50 |
#include <string.h> |
| 51 |
|
| 52 |
/*****************************************************************************/ |
| 53 |
/** Base name for COM devices */ |
| 54 |
#if defined(__APPLE__) && defined(__MACH__) |
| 55 |
static const char * devBases[] = { |
| 56 |
"tty." |
| 57 |
}; |
| 58 |
static int noBases = 1; |
| 59 |
#else |
| 60 |
static const char * devBases[] = { |
| 61 |
"ttyACM", "ttyUSB", "rfcomm", "ttyS" |
| 62 |
}; |
| 63 |
static int noBases = 4; |
| 64 |
#endif |
| 65 |
|
| 66 |
/*****************************************************************************/ |
| 67 |
typedef struct { |
| 68 |
char * port; |
| 69 |
int handle; |
| 70 |
} COMDevice; |
| 71 |
|
| 72 |
#define COM_MAXDEVICES 64 |
| 73 |
static COMDevice comDevices[COM_MAXDEVICES]; |
| 74 |
static int noDevices = 0; |
| 75 |
|
| 76 |
/*****************************************************************************/ |
| 77 |
/** Private functions */ |
| 78 |
void _AppendDevices(const char * base); |
| 79 |
int _BaudFlag(int BaudRate); |
| 80 |
|
| 81 |
/*****************************************************************************/ |
| 82 |
int comEnumerate() |
| 83 |
{ |
| 84 |
for (int i = 0; i < noDevices; i++) { |
| 85 |
if (comDevices[i].port) free(comDevices[i].port); |
| 86 |
comDevices[i].port = NULL; |
| 87 |
} |
| 88 |
noDevices = 0; |
| 89 |
for (int i = 0; i < noBases; i++) |
| 90 |
_AppendDevices(devBases[i]); |
| 91 |
return noDevices; |
| 92 |
} |
| 93 |
|
| 94 |
void comTerminate() |
| 95 |
{ |
| 96 |
comCloseAll(); |
| 97 |
for (int i = 0; i < noDevices; i++) { |
| 98 |
if (comDevices[i].port) free(comDevices[i].port); |
| 99 |
comDevices[i].port = NULL; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
int comGetNoPorts() |
| 104 |
{ |
| 105 |
return noDevices; |
| 106 |
} |
| 107 |
|
| 108 |
/*****************************************************************************/ |
| 109 |
int comFindPort(const char * name) |
| 110 |
{ |
| 111 |
int p; |
| 112 |
for (p = 0; p < noDevices; p++) |
| 113 |
if (strcmp(name, comDevices[p].port) == 0) |
| 114 |
return p; |
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
const char * comGetInternalName(int index) |
| 119 |
{ |
| 120 |
#define COM_MAXNAME 128 |
| 121 |
static char name[COM_MAXNAME]; |
| 122 |
if (index >= noDevices || index < 0) |
| 123 |
return NULL; |
| 124 |
sprintf(name, "/dev/%s", comDevices[index].port); |
| 125 |
return name; |
| 126 |
} |
| 127 |
|
| 128 |
const char * comGetPortName(int index) { |
| 129 |
if (index >= noDevices || index < 0) |
| 130 |
return NULL; |
| 131 |
return comDevices[index].port; |
| 132 |
} |
| 133 |
|
| 134 |
/*****************************************************************************/ |
| 135 |
int comOpen(int index, int baudrate) |
| 136 |
{ |
| 137 |
if (index >= noDevices || index < 0) |
| 138 |
return 0; |
| 139 |
// Close if already open |
| 140 |
COMDevice * com = &comDevices[index]; |
| 141 |
if (com->handle >= 0) comClose(index); |
| 142 |
// Open port |
| 143 |
printf("Try %s \n", comGetInternalName(index)); |
| 144 |
int handle = open(comGetInternalName(index), O_RDWR | O_NOCTTY | O_NDELAY); |
| 145 |
if (handle < 0) |
| 146 |
return 0; |
| 147 |
printf("Open %s \n", comGetInternalName(index)); |
| 148 |
// General configuration |
| 149 |
struct termios config; |
| 150 |
memset(&config, 0, sizeof(config)); |
| 151 |
tcgetattr(handle, &config); |
| 152 |
config.c_iflag &= ~(INLCR | ICRNL); |
| 153 |
config.c_iflag |= IGNPAR | IGNBRK; |
| 154 |
config.c_oflag &= ~(OPOST | ONLCR | OCRNL); |
| 155 |
config.c_cflag &= ~(PARENB | PARODD | CSTOPB | CSIZE | CRTSCTS); |
| 156 |
config.c_cflag |= CLOCAL | CREAD | CS8; |
| 157 |
config.c_lflag &= ~(ICANON | ISIG | ECHO); |
| 158 |
int flag = _BaudFlag(baudrate); |
| 159 |
cfsetospeed(&config, flag); |
| 160 |
cfsetispeed(&config, flag); |
| 161 |
// Timeouts configuration |
| 162 |
config.c_cc[VTIME] = 1; |
| 163 |
config.c_cc[VMIN] = 0; |
| 164 |
//fcntl(handle, F_SETFL, FNDELAY); |
| 165 |
// Validate configuration |
| 166 |
if (tcsetattr(handle, TCSANOW, &config) < 0) { |
| 167 |
close(handle); |
| 168 |
return 0; |
| 169 |
} |
| 170 |
com->handle = handle; |
| 171 |
return 1; |
| 172 |
} |
| 173 |
|
| 174 |
void comClose(int index) |
| 175 |
{ |
| 176 |
if (index >= noDevices || index < 0) |
| 177 |
return; |
| 178 |
COMDevice * com = &comDevices[index]; |
| 179 |
if (com->handle < 0) |
| 180 |
return; |
| 181 |
tcdrain(com->handle); |
| 182 |
close(com->handle); |
| 183 |
com->handle = -1; |
| 184 |
} |
| 185 |
|
| 186 |
void comCloseAll() |
| 187 |
{ |
| 188 |
for (int i = 0; i < noDevices; i++) |
| 189 |
comClose(i); |
| 190 |
} |
| 191 |
|
| 192 |
/*****************************************************************************/ |
| 193 |
int comWrite(int index, const char * buffer, size_t len) |
| 194 |
{ |
| 195 |
if (index >= noDevices || index < 0) |
| 196 |
return 0; |
| 197 |
if (comDevices[index].handle <= 0) |
| 198 |
return 0; |
| 199 |
int res = write(comDevices[index].handle, buffer, len); |
| 200 |
if (res < 0) |
| 201 |
res = 0; |
| 202 |
return res; |
| 203 |
} |
| 204 |
|
| 205 |
int comRead(int index, char * buffer, size_t len) |
| 206 |
{ |
| 207 |
if (index >= noDevices || index < 0) |
| 208 |
return 0; |
| 209 |
if (comDevices[index].handle <= 0) |
| 210 |
return 0; |
| 211 |
int res = read(comDevices[index].handle, buffer, len); |
| 212 |
if (res < 0) |
| 213 |
res = 0; |
| 214 |
return res; |
| 215 |
} |
| 216 |
|
| 217 |
/*****************************************************************************/ |
| 218 |
int _BaudFlag(int BaudRate) |
| 219 |
{ |
| 220 |
switch(BaudRate) |
| 221 |
{ |
| 222 |
case 50: return B50; break; |
| 223 |
case 110: return B110; break; |
| 224 |
case 134: return B134; break; |
| 225 |
case 150: return B150; break; |
| 226 |
case 200: return B200; break; |
| 227 |
case 300: return B300; break; |
| 228 |
case 600: return B600; break; |
| 229 |
case 1200: return B1200; break; |
| 230 |
case 1800: return B1800; break; |
| 231 |
case 2400: return B2400; break; |
| 232 |
case 4800: return B4800; break; |
| 233 |
case 9600: return B9600; break; |
| 234 |
case 19200: return B19200; break; |
| 235 |
case 38400: return B38400; break; |
| 236 |
case 57600: return B57600; break; |
| 237 |
case 115200: return B115200; break; |
| 238 |
case 230400: return B230400; break; |
| 239 |
default : return B0; break; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
void _AppendDevices(const char * base) |
| 244 |
{ |
| 245 |
int baseLen = strlen(base); |
| 246 |
struct dirent * dp; |
| 247 |
// Enumerate devices |
| 248 |
DIR * dirp = opendir("/dev"); |
| 249 |
while ((dp = readdir(dirp)) && noDevices < COM_MAXDEVICES) { |
| 250 |
if (strlen(dp->d_name) >= baseLen) { |
| 251 |
if (memcmp(base, dp->d_name, baseLen) == 0) { |
| 252 |
COMDevice * com = &comDevices[noDevices ++]; |
| 253 |
com->port = (char *) strdup(dp->d_name); |
| 254 |
com->handle = -1; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
closedir(dirp); |
| 259 |
} |
| 260 |
|
| 261 |
#endif // unix |