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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016 Edouard Griffiths, F4EXB. //
// //
// 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 as version 3 of the License, or //
// //
// 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 V3 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 "serialdatacontroller.h"
#include <sys/types.h>
#include <stdio.h>
#if defined(__WINDOWS__)
#include <setupapi.h>
#include <winioctl.h>
#include <assert.h>
#include <stdio.h>
#else
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/serial.h>
#include <cerrno>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <cassert>
#endif
namespace SerialDV
{
#if defined(__WINDOWS__)
SerialDataController::SerialDataController() :
m_handle(INVALID_HANDLE_VALUE),
m_readOverlapped(),
m_writeOverlapped(),
m_readBuffer(NULL),
m_readLength(0U),
m_readPending(false)
{
m_readBuffer = new unsigned char[BUFFER_LENGTH];
}
SerialDataController::~SerialDataController()
{
delete[] m_readBuffer;
}
bool SerialDataController::open(const std::string& device, SERIAL_SPEED speed)
{
assert(m_handle == INVALID_HANDLE_VALUE);
assert(!device.empty());
m_device = device;
m_speed = speed;
DWORD errCode;
m_handle = ::CreateFile(m_device.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (m_handle == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Cannot open device - %s, err=%04lx\n", m_device.c_str(), ::GetLastError());
return false;
}
DCB dcb;
if (::GetCommState(m_handle, &dcb) == 0)
{
fprintf(stderr, "Cannot get the attributes for %s, err=%04lx\n", m_device.c_str(), ::GetLastError());
::ClearCommError(m_handle, &errCode, NULL);
::CloseHandle(m_handle);
return false;
}
dcb.BaudRate = DWORD(m_speed);
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.fParity = FALSE;
dcb.StopBits = ONESTOPBIT;
dcb.fInX = FALSE;
dcb.fOutX = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
if (::SetCommState(m_handle, &dcb) == 0)
{
fprintf(stderr, "Cannot set the attributes for %s, err=%04lx\n", m_device.c_str(), ::GetLastError());
::ClearCommError(m_handle, &errCode, NULL);
::CloseHandle(m_handle);
return false;
}
COMMTIMEOUTS timeouts;
if (!::GetCommTimeouts(m_handle, &timeouts))
{
fprintf(stderr, "Cannot get the timeouts for %s, err=%04lx\n", m_device.c_str(), ::GetLastError());
::ClearCommError(m_handle, &errCode, NULL);
::CloseHandle(m_handle);
return false;
}
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0UL;
timeouts.ReadTotalTimeoutConstant = 0UL;
if (!::SetCommTimeouts(m_handle, &timeouts))
{
fprintf(stderr, "Cannot set the timeouts for %s, err=%04lx\n", m_device.c_str(), ::GetLastError());
::ClearCommError(m_handle, &errCode, NULL);
::CloseHandle(m_handle);
return false;
}
if (::EscapeCommFunction(m_handle, CLRDTR) == 0)
{
fprintf(stderr, "Cannot clear DTR for %s, err=%04lx\n", m_device.c_str(), ::GetLastError());
::ClearCommError(m_handle, &errCode, NULL);
::CloseHandle(m_handle);
return false;
}
if (::EscapeCommFunction(m_handle, CLRRTS) == 0)
{
fprintf(stderr, "Cannot clear RTS for %s, err=%04lx\n", m_device.c_str(), ::GetLastError());
::ClearCommError(m_handle, &errCode, NULL);
::CloseHandle(m_handle);
return false;
}
::ClearCommError(m_handle, &errCode, NULL);
::memset(&m_readOverlapped, 0x00U, sizeof(OVERLAPPED));
::memset(&m_writeOverlapped, 0x00U, sizeof(OVERLAPPED));
m_readOverlapped.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
m_writeOverlapped.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
m_readLength = 0U;
m_readPending = false;
::memset(m_readBuffer, 0x00U, BUFFER_LENGTH);
return true;
}
int SerialDataController::read(unsigned char* buffer, unsigned int length)
{
assert(m_handle != INVALID_HANDLE_VALUE);
assert(buffer != NULL);
unsigned int ptr = 0U;
while (ptr < length)
{
int ret = readNonblock(buffer + ptr, length - ptr);
if (ret < 0)
{
return ret;
}
else if (ret == 0)
{
if (ptr == 0U)
return 0;
}
else
{
ptr += ret;
}
}
return int(length);
}
int SerialDataController::readNonblock(unsigned char* buffer, unsigned int length)
{
assert(m_handle != INVALID_HANDLE_VALUE);
assert(buffer != NULL);
if (length > BUFFER_LENGTH)
length = BUFFER_LENGTH;
if (m_readPending && length != m_readLength)
{
::CancelIo(m_handle);
m_readPending = false;
}
m_readLength = length;
if (length == 0U)
return 0;
if (!m_readPending)
{
DWORD bytes = 0UL;
BOOL res = ::ReadFile(m_handle, m_readBuffer, m_readLength, &bytes, &m_readOverlapped);
if (res)
{
::memcpy(buffer, m_readBuffer, bytes);
return int(bytes);
}
DWORD error = ::GetLastError();
if (error != ERROR_IO_PENDING)
{
fprintf(stderr, "Error from ReadFile: %04lx\n", error);
return -1;
}
m_readPending = true;
}
BOOL res = HasOverlappedIoCompleted(&m_readOverlapped);
if (!res)
return 0;
DWORD bytes = 0UL;
res = ::GetOverlappedResult(m_handle, &m_readOverlapped, &bytes, TRUE);
if (!res)
{
fprintf(stderr, "Error from GetOverlappedResult (ReadFile): %04lx\n", ::GetLastError());
return -1;
}
::memcpy(buffer, m_readBuffer, bytes);
m_readPending = false;
return int(bytes);
}
int SerialDataController::write(const unsigned char* buffer, unsigned int length)
{
assert(m_handle != INVALID_HANDLE_VALUE);
assert(buffer != NULL);
if (length == 0U)
return 0;
unsigned int ptr = 0U;
while (ptr < length)
{
DWORD bytes = 0UL;
BOOL res = ::WriteFile(m_handle, buffer + ptr, length - ptr, &bytes, &m_writeOverlapped);
if (!res)
{
DWORD error = ::GetLastError();
if (error != ERROR_IO_PENDING)
{
fprintf(stderr, "Error from WriteFile: %04lx\n", error);
return -1;
}
res = ::GetOverlappedResult(m_handle, &m_writeOverlapped, &bytes, TRUE);
if (!res)
{
fprintf(stderr, "Error from GetOverlappedResult (WriteFile): %04lx\n", ::GetLastError());
return -1;
}
}
ptr += bytes;
}
return int(length);
}
void SerialDataController::closeIt()
{
assert(m_handle != INVALID_HANDLE_VALUE);
::CloseHandle(m_handle);
m_handle = INVALID_HANDLE_VALUE;
::CloseHandle(m_readOverlapped.hEvent);
::CloseHandle(m_writeOverlapped.hEvent);
}
#else
SerialDataController::SerialDataController() :
m_speed(SERIAL_NONE),
m_fd(-1)
{
}
SerialDataController::~SerialDataController()
{
}
bool SerialDataController::open(const std::string& device, SERIAL_SPEED speed)
{
assert(m_fd == -1);
assert(!device.empty());
m_device = device;
m_speed = speed;
m_fd = ::open(m_device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY, 0);
if (m_fd < 0)
{
fprintf(stderr, "SerialDataController::open: Cannot open device - %s", m_device.c_str());
return false;
}
if (::isatty(m_fd) == 0)
{
fprintf(stderr, "SerialDataController::open: %s is not a TTY device", m_device.c_str());
::close(m_fd);
return false;
}
// This is an attempt to fix a bug introduced in the FTDI driver in kernel 4.4.52
// However this works only if you execute as root
// You will have to stick to an older kernel until a fix is found or run as root
// This bug introduces too much latency that prevents packets to flow on time
// A more persistent way to do this is to execute once per /dev/ttyUSBx lifetime:
// echo 1 | sudo tee /sys/bus/usb-serial/devices/ttyUSBx/latency_timer
// Of course replace "x" by your ttyUSB device number
struct serial_struct serial;
if (::ioctl(m_fd, TIOCGSERIAL, &serial) < 0) {
fprintf(stderr, "SerialDataController::open: ioctl: Cannot get serial_struct\n");
}
serial.flags |= ASYNC_LOW_LATENCY;
if (::ioctl(m_fd, TIOCSSERIAL, &serial) < 0) {
fprintf(stderr, "SerialDataController::open: ioctl: Cannot set ASYNC_LOW_LATENCY\n");
return false;
}
// Set "terminal" characteristics
termios termios;
if (::tcgetattr(m_fd, &termios) < 0)
{
fprintf(stderr, "SerialDataController::open: Cannot get the attributes for %s", m_device.c_str());
::close(m_fd);
return false;
}
termios.c_lflag &= ~(ECHO | ECHOE | ICANON | IEXTEN | ISIG);
termios.c_iflag &=
~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF | IXANY);
termios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CRTSCTS);
termios.c_cflag |= CS8;
termios.c_oflag &= ~(OPOST);
termios.c_cc[VMIN] = 0;
termios.c_cc[VTIME] = 10;
switch (m_speed)
{
case SERIAL_1200:
::cfsetospeed(&termios, B1200);
::cfsetispeed(&termios, B1200);
break;
case SERIAL_2400:
::cfsetospeed(&termios, B2400);
::cfsetispeed(&termios, B2400);
break;
case SERIAL_4800:
::cfsetospeed(&termios, B4800);
::cfsetispeed(&termios, B4800);
break;
case SERIAL_9600:
::cfsetospeed(&termios, B9600);
::cfsetispeed(&termios, B9600);
break;
case SERIAL_19200:
::cfsetospeed(&termios, B19200);
::cfsetispeed(&termios, B19200);
break;
case SERIAL_38400:
::cfsetospeed(&termios, B38400);
::cfsetispeed(&termios, B38400);
break;
case SERIAL_115200:
::cfsetospeed(&termios, B115200);
::cfsetispeed(&termios, B115200);
break;
case SERIAL_230400:
::cfsetospeed(&termios, B230400);
::cfsetispeed(&termios, B230400);
break;
case SERIAL_460800:
::cfsetospeed(&termios, B460800);
::cfsetispeed(&termios, B460800);
break;
default:
fprintf(stderr, "SerialDataController::open: Unsupported serial port speed - %d\n", int(m_speed));
::close(m_fd);
return false;
}
if (::tcsetattr(m_fd, TCSANOW, &termios) < 0)
{
fprintf(stderr, "SerialDataController::open: Cannot set the attributes for %s\n", m_device.c_str());
::close(m_fd);
return false;
}
fprintf(stderr, "SerialDataController::open: opened %s at speed %d\n", m_device.c_str(), int(m_speed));
return true;
}
int SerialDataController::read(unsigned char* buffer, unsigned int lengthInBytes)
{
assert(buffer != 0);
assert(m_fd != -1);
if (lengthInBytes == 0U)
return 0;
unsigned int offset = 0U;
while (offset < lengthInBytes)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(m_fd, &fds);
int n;
if (offset == 0U)
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
n = ::select(m_fd + 1, &fds, 0, 0, &tv);
if (n == 0) {
return 0;
}
}
else
{
n = ::select(m_fd + 1, &fds, 0, 0, 0);
}
if (n < 0)
{
fprintf(stderr, "SerialDataController::read: Error from select(), errno=%d", errno);
return -1;
}
if (n > 0)
{
ssize_t len = ::read(m_fd, buffer + offset, lengthInBytes - offset);
if (len < 0)
{
if (errno != EAGAIN)
{
fprintf(stderr, "SerialDataController::read: Error from read(), errno=%d", errno);
return -1;
}
}
if (len > 0) {
offset += len;
}
}
}
return lengthInBytes;
}
int SerialDataController::write(const unsigned char* buffer, unsigned int lengthInBytes)
{
assert(buffer != 0);
assert(m_fd != -1);
if (lengthInBytes == 0U)
return 0;
unsigned int ptr = 0U;
while (ptr < lengthInBytes)
{
ssize_t n = ::write(m_fd, buffer + ptr, lengthInBytes - ptr);
if (n < 0)
{
if (errno != EAGAIN)
{
fprintf(stderr, "SerialDataController::write: Error returned from write(), errno=%d", errno);
return -1;
}
}
if (n > 0) {
ptr += n;
}
}
return lengthInBytes;
}
void SerialDataController::closeIt()
{
assert(m_fd != -1);
::close (m_fd);
m_device.clear();
m_speed = SERIAL_NONE;
m_fd = -1;
}
#endif // WINDOWS
bool SerialDataController::initResponse()
{
return true; // Do nothing for serial
}
} // namespace SerialDV
|