You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

605 lines
14 KiB

7 years ago
​
7 years ago
6 years ago
​
​
​
​
​
6 years ago
6 years ago
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
7 years ago
​
7 years ago
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
  1. #include "serialPort.h"
  2. #define LOG(X) m_pLog->Log("UART", this, X)
  3. #ifdef DEBUG
  4. #define LOG_ERROR(X) \
  5. { \
  6. char _tBfrErrMSG2w34[256]; \
  7. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  8. m_pLog->Log("UART", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  9. }
  10. #else
  11. #define LOG_ERROR(X) m_pLog->Log("UART", this, _LOG_ERROR, X);
  12. #endif
  13. serialPort::serialPort() :
  14. transportSerial()
  15. {
  16. #ifdef __linux__
  17. memset(&m_sTTY, 0, sizeof(m_sTTY));
  18. m_iInFID = -1;
  19. m_iOutFID = -1;
  20. #else
  21. memset(&PortDCB, 0, sizeof(PortDCB));
  22. m_ioFID = INVALID_HANDLE_VALUE;
  23. #endif
  24. m_pLog = basicLogCollection::getInstance();
  25. // m_bConnected = false;
  26. m_bInitComplete = false;
  27. m_sDevicePath = NULL;
  28. m_iLineSpeed = 9600;
  29. }
  30. serialPort::serialPort(CSTR DevicePath, int Speed) :
  31. transportSerial()
  32. {
  33. #ifdef __linux__
  34. memset(&m_sTTY, 0, sizeof(m_sTTY));
  35. m_iInFID = -1;
  36. m_iOutFID = -1;
  37. #else
  38. memset(&PortDCB, 0, sizeof(PortDCB));
  39. m_ioFID = INVALID_HANDLE_VALUE;
  40. #endif
  41. m_pLog = basicLogCollection::getInstance();
  42. // m_bConnected = false;
  43. m_iLineSpeed = Speed;
  44. m_sDevicePath = (CSTR) malloc(strlen(DevicePath) + 1);
  45. strcpy(m_sDevicePath, DevicePath);
  46. m_bInitComplete = HW_init(m_sDevicePath, m_iLineSpeed);
  47. if (m_bInitComplete)
  48. {
  49. #ifdef DEBUG
  50. char MsgBuffer[CLOG];
  51. sprintf(MsgBuffer, "Init %s [%d] OK", m_sDevicePath, m_iLineSpeed);
  52. LOG(MsgBuffer);
  53. #endif
  54. m_bInitComplete = init();
  55. };
  56. }
  57. bool serialPort::create(CSTR DevicePath, int Speed)
  58. {
  59. if (m_eConnectionStatus == CS_CONNECTED)
  60. {
  61. LOG_ERROR("TRYING TO CONNECT ON OPENED PORT!");
  62. return false;
  63. };
  64. if (m_bInitComplete)
  65. {
  66. LOG_ERROR("TRYING TO CREATE CREATED PORT!");
  67. return false;
  68. };
  69. m_iLineSpeed = Speed;
  70. m_sDevicePath = (CSTR) malloc(strlen(DevicePath) + 1);
  71. strcpy(m_sDevicePath, DevicePath);
  72. m_bInitComplete = HW_init(m_sDevicePath, m_iLineSpeed);
  73. if (m_bInitComplete)
  74. {
  75. #ifdef DEBUG
  76. char MsgBuffer[CLOG];
  77. sprintf(MsgBuffer, "Init %s [%d] OK", m_sDevicePath, m_iLineSpeed);
  78. LOG(MsgBuffer);
  79. #endif
  80. return m_bInitComplete = init();
  81. };
  82. return false;
  83. }
  84. // Destructor implementation
  85. serialPort::~serialPort()
  86. {
  87. int result;
  88. #ifdef __linux__
  89. if (m_iInFID != -1)
  90. {
  91. do
  92. {
  93. result = close(m_iInFID);
  94. }
  95. while (result == -1 && errno == EINTR);
  96. if (result == -1)
  97. {
  98. #else
  99. if (m_ioFID != INVALID_HANDLE_VALUE)
  100. {
  101. result = CloseHandle(m_ioFID);
  102. if (result == 0)
  103. {
  104. #endif
  105. char MsgBuffer[CLOG];
  106. sprintf(MsgBuffer, "Error %d closing input [%s]: %s", errno, m_sDevicePath, strerror(errno));
  107. LOG_ERROR(MsgBuffer);
  108. };
  109. };
  110. #ifdef __linux__
  111. if (m_iOutFID != -1)
  112. {
  113. do
  114. {
  115. result = close(m_iOutFID);
  116. }
  117. while (result == -1 && errno == EINTR);
  118. if (result == -1)
  119. {
  120. char MsgBuffer[CLOG];
  121. sprintf(MsgBuffer, "Error %d closing output [%s]: %s", errno, m_sDevicePath, strerror(errno));
  122. LOG_ERROR(MsgBuffer);
  123. };
  124. };
  125. #endif
  126. waitThreadAEnd(true);
  127. waitThreadBEnd(true);
  128. }
  129. bool serialPort::HW_init(CSTR DevicePath, int Speed)
  130. {
  131. char MsgBuffer[CLOG];
  132. m_bInitComplete = false;
  133. sprintf(m_sDevicePath, "%s", DevicePath);
  134. #ifdef __linux__
  135. int result;
  136. /* Open File Descriptor */
  137. // m_iFID = open( DevicePath, O_RDWR| O_NONBLOCK | O_NDELAY );
  138. do
  139. {
  140. m_iInFID = open(m_sDevicePath, O_RDONLY | O_NONBLOCK | O_NDELAY);
  141. }
  142. while (m_iInFID == -1 && errno == EINTR);
  143. if (m_iInFID == -1)
  144. {
  145. return false;
  146. }
  147. do
  148. {
  149. result = fcntl(m_iInFID, F_SETFL, O_NONBLOCK);
  150. }
  151. while (result == -1 && errno == EINTR);
  152. if (result == -1)
  153. {
  154. const int error = errno;
  155. do
  156. {
  157. result = close(m_iInFID);
  158. }
  159. while (result == -1 && errno == EINTR);
  160. errno = error;
  161. sprintf(MsgBuffer, "Error %d openning port: %s", errno, strerror(errno));
  162. LOG_ERROR(MsgBuffer);
  163. return false;
  164. };
  165. do
  166. {
  167. m_iOutFID = open(DevicePath, O_WRONLY | O_NDELAY);
  168. }
  169. while (m_iOutFID == -1 && errno == EINTR);
  170. if (m_iOutFID == -1)
  171. {
  172. return false;
  173. }
  174. /* *** Configure Port *** */
  175. memset(&m_sTTY, 0, sizeof(m_sTTY));
  176. /* Error Handling */
  177. do
  178. {
  179. result = tcgetattr(m_iInFID, &m_sTTY);
  180. }
  181. while (result == -1 && errno == EINTR);
  182. if (result == -1)
  183. {
  184. const int error = errno;
  185. do
  186. {
  187. result = close(m_iInFID);
  188. }
  189. while (result == -1 && errno == EINTR);
  190. errno = error;
  191. sprintf(MsgBuffer, "Error %d from tcgetattr: %s", errno, strerror(errno));
  192. LOG_ERROR(MsgBuffer);
  193. return false;
  194. };
  195. /* Set Baud Rate */
  196. m_iLineSpeed = Speed;
  197. int RetVal = 0;
  198. switch (m_iLineSpeed)
  199. {
  200. case 4000000: cfsetspeed(&m_sTTY, B4000000); break;
  201. case 3500000: cfsetspeed(&m_sTTY, B3500000); break;
  202. case 3000000: cfsetspeed(&m_sTTY, B3000000); break;
  203. case 2500000: cfsetspeed(&m_sTTY, B2500000); break;
  204. case 2000000: cfsetspeed(&m_sTTY, B2000000); break;
  205. case 1500000: cfsetspeed(&m_sTTY, B1500000); break;
  206. case 1152000: cfsetspeed(&m_sTTY, B1152000); break;
  207. case 1000000: cfsetspeed(&m_sTTY, B1000000); break;
  208. case 921600 : cfsetspeed(&m_sTTY, B921600); break;
  209. case 460800 : cfsetspeed(&m_sTTY, B460800); break;
  210. case 230400 : cfsetspeed(&m_sTTY, B230400); break;
  211. case 115200 : cfsetspeed(&m_sTTY, B115200); break;
  212. case 19200 : cfsetspeed(&m_sTTY, B19200); break;
  213. case 9600 : cfsetspeed(&m_sTTY, B9600); break;
  214. default:
  215. {
  216. close(m_iInFID);
  217. close(m_iOutFID);
  218. sprintf(MsgBuffer, "Error cannot set speed as [%d] bod", m_iLineSpeed);
  219. LOG_ERROR(MsgBuffer);
  220. return false;
  221. };
  222. };
  223. if (RetVal == 0)
  224. {
  225. #ifdef DEBUG
  226. sprintf(MsgBuffer, "speed set to [%d] bod", m_iLineSpeed);
  227. LOG(MsgBuffer);
  228. #endif
  229. }
  230. else
  231. {
  232. RetVal = cfgetispeed(&m_sTTY);
  233. sprintf(MsgBuffer, "Error cannot set speed as [%d] bod. Current is: %d bod. ", m_iLineSpeed, RetVal);
  234. LOG_ERROR(MsgBuffer);
  235. return false;
  236. };
  237. /* Setting other Port Stuff */
  238. m_sTTY.c_cflag &= ~PARENB; // Make 8n1
  239. m_sTTY.c_cflag &= ~CSTOPB;
  240. m_sTTY.c_cflag &= ~CSIZE;
  241. m_sTTY.c_cflag |= CS8;
  242. m_sTTY.c_cflag &= ~CRTSCTS; // no flow control
  243. m_sTTY.c_lflag = 0; // no signaling chars, no echo, no canonical processing
  244. m_sTTY.c_oflag = 0; // no remapping, no delays
  245. m_sTTY.c_cc[VMIN] = 0; // read doesn't block
  246. m_sTTY.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  247. m_sTTY.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
  248. m_sTTY.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  249. m_sTTY.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
  250. m_sTTY.c_oflag &= ~OPOST; // make raw
  251. /* Flush Port, then applies attributes */
  252. tcflush(m_iOutFID, TCIFLUSH);
  253. do
  254. {
  255. result = tcsetattr(m_iOutFID, TCSANOW, &m_sTTY);
  256. }
  257. while (result == -1 && errno == EINTR);
  258. if (result == -1)
  259. {
  260. const int error = errno;
  261. do
  262. {
  263. result = close(m_iOutFID);
  264. }
  265. while (result == -1 && errno == EINTR);
  266. errno = error;
  267. sprintf(MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror(errno));
  268. LOG_ERROR(MsgBuffer);
  269. return false;
  270. };
  271. do
  272. {
  273. result = tcsetattr(m_iInFID, TCSANOW, &m_sTTY);
  274. }
  275. while (result == -1 && errno == EINTR);
  276. if (result == -1)
  277. {
  278. const int error = errno;
  279. do
  280. {
  281. result = close(m_iInFID);
  282. }
  283. while (result == -1 && errno == EINTR);
  284. errno = error;
  285. sprintf(MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror(errno));
  286. LOG_ERROR(MsgBuffer);
  287. return false;
  288. };
  289. #else
  290. m_ioFID = CreateFile((LPCSTR) m_sDevicePath, GENERIC_READ | GENERIC_WRITE, 0UL, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING,
  291. // FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,
  292. NULL);
  293. if (m_ioFID == INVALID_HANDLE_VALUE)
  294. {
  295. setConnectionStatus(CS_DISCONNECTED);
  296. sprintf(MsgBuffer, "Port [%s] Open Failed Err: %d", m_sDevicePath, GetLastError());
  297. LOG_ERROR(MsgBuffer);
  298. return false;
  299. };
  300. // Initialize the DCBlength member.
  301. PortDCB.DCBlength = sizeof(DCB);
  302. // Get the default port setting information.
  303. GetCommState(m_ioFID, &PortDCB);
  304. // Change the DCB structure settings
  305. /*
  306. PortDCB.fBinary = TRUE; // Binary mode; no EOF check
  307. PortDCB.fParity = NOPARITY; // Enable parity checking
  308. PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
  309. PortDCB.fErrorChar = FALSE; // Disable error replacement
  310. PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
  311. PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error
  312. PortDCB.fNull = FALSE; // Disable null stripping
  313. PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
  314. PortDCB.BaudRate = Speed;
  315. PortDCB.StopBits = ONESTOPBIT;
  316. */
  317. PortDCB.BaudRate = Speed;
  318. PortDCB.ByteSize = 8;
  319. PortDCB.Parity = NOPARITY;
  320. PortDCB.StopBits = ONESTOPBIT;
  321. PortDCB.EvtChar = 0;
  322. PortDCB.fParity = (PortDCB.Parity != NOPARITY);
  323. PortDCB.fOutxCtsFlow = FALSE;
  324. PortDCB.fOutxDsrFlow = FALSE;
  325. PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
  326. PortDCB.fDsrSensitivity = FALSE;
  327. PortDCB.fOutX = FALSE;
  328. PortDCB.fInX = FALSE;
  329. PortDCB.fErrorChar = FALSE;
  330. PortDCB.fNull = FALSE;
  331. // dcb.fRtsControl = RTS_CONTROL_DISABLE;
  332. PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
  333. // dcb.fRtsControl = RTS_CONTROL_TOGGLE; // specially for PROMAG USB->RS485 converter
  334. PortDCB.fAbortOnError = FALSE;
  335. PortDCB.fBinary = TRUE;
  336. PortDCB.fTXContinueOnXoff = TRUE;
  337. // Re-configure the port with the new DCB structure.
  338. if (! SetCommState(m_ioFID, &PortDCB))
  339. {
  340. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommState Failed)", m_sDevicePath);
  341. LOG_ERROR(MsgBuffer);
  342. CloseHandle(m_ioFID);
  343. return false;
  344. };
  345. COMMTIMEOUTS CommTimeouts;
  346. // Retrieve the time-out parameters for all read and write operations
  347. GetCommTimeouts(m_ioFID, &CommTimeouts);
  348. /*
  349. CommTimeouts.ReadIntervalTimeout = 20;
  350. CommTimeouts.ReadTotalTimeoutMultiplier = 10;
  351. CommTimeouts.ReadTotalTimeoutConstant = 100;
  352. CommTimeouts.WriteTotalTimeoutMultiplier = 10;
  353. CommTimeouts.WriteTotalTimeoutConstant = 100;
  354. */
  355. CommTimeouts.ReadIntervalTimeout = 20UL;
  356. CommTimeouts.ReadTotalTimeoutMultiplier = 1UL;
  357. CommTimeouts.ReadTotalTimeoutConstant = 5UL;
  358. CommTimeouts.WriteTotalTimeoutMultiplier = 1UL;
  359. CommTimeouts.WriteTotalTimeoutConstant = 1UL;
  360. // Set the time-out parameters for all read and write operations on the port.
  361. if (! SetCommTimeouts(m_ioFID, &CommTimeouts))
  362. {
  363. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommTimeouts Failed)", m_sDevicePath);
  364. LOG_ERROR(MsgBuffer);
  365. CloseHandle(m_ioFID);
  366. return false;
  367. };
  368. // Clear the port of any existing data.
  369. if (PurgeComm(m_ioFID, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT) == 0)
  370. {
  371. sprintf(MsgBuffer, "Port [%s] Clearing The Port Failed", m_sDevicePath);
  372. LOG_ERROR(MsgBuffer);
  373. CloseHandle(m_ioFID);
  374. return false;
  375. };
  376. EscapeCommFunction(m_ioFID, SETRTS);
  377. EscapeCommFunction(m_ioFID, SETDTR);
  378. // Set DataTerminalReady 1
  379. #endif
  380. setConnectionStatus(CS_CONNECTED);
  381. return true;
  382. }
  383. int serialPort::sendBlock(unsigned char * Data, int DataSize)
  384. {
  385. if (! m_bInitComplete)
  386. {
  387. #ifdef DEBUG
  388. LOG("Port is not Ready to Send");
  389. #endif
  390. return -1;
  391. };
  392. #ifdef __linux__
  393. int RetVal;
  394. do
  395. {
  396. RetVal = write(m_iOutFID, Data, DataSize);
  397. usleep(100);
  398. }
  399. while (RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK));
  400. #else
  401. DWORD RetVal;
  402. // OVERLAPPED ov;
  403. // if (!
  404. WriteFile(m_ioFID, Data, DataSize, &RetVal, NULL); //){
  405. // RetVal = 0;
  406. // };
  407. #endif
  408. #ifdef DEBUG
  409. char MsgBuffer[CLOG];
  410. sprintf(MsgBuffer, "Sent %d byte(s) of %d", RetVal, DataSize);
  411. LOG(MsgBuffer);
  412. #ifdef DEBUG_SERIAL_COMMUNICATION
  413. for (int i = 0; i < DataSize; i++)
  414. {
  415. sprintf(MsgBuffer, "0x%02X", Data[i]);
  416. LOG(MsgBuffer);
  417. };
  418. #endif
  419. #endif
  420. return RetVal;
  421. //*/
  422. /*
  423. char const *p = (char const *)Data;
  424. char const *const q = (char const *)Data + DataSize;
  425. ssize_t n;
  426. char MsgBuffer[CLOG];
  427. sprintf( MsgBuffer, "Prepare to Send %d Bytes", DataSize );
  428. Log( MsgBuffer );
  429. while (p < q) {
  430. do {
  431. n = write( m_iOutFID, p, (size_t)(q - p));
  432. } while (n == (ssize_t)-1 && errno == EINTR);
  433. if (n == (ssize_t)-1 && errno == EWOULDBLOCK) {
  434. // Sleep for a millisecond, then retry.
  435. usleep(1000);
  436. continue;
  437. }
  438. if (n == (ssize_t)-1)
  439. return errno;
  440. else
  441. if (n < (ssize_t)1)
  442. return EIO;
  443. p += (size_t)n;
  444. }
  445. sprintf( MsgBuffer, "Sent %d Bytes", DataSize );
  446. Log( MsgBuffer );
  447. if (p != q)
  448. return EIO;
  449. return DataSize;
  450. */
  451. }
  452. int serialPort::recptBlock(unsigned char * Buffer, int * BufferLength)
  453. {
  454. if (! m_bInitComplete)
  455. {
  456. #ifdef DEBUG
  457. LOG("Port is Not Ready to Read");
  458. #endif
  459. return -1;
  460. };
  461. /*
  462. int n_read = read( m_iInFID, Buffer, sizeof(Buffer) );
  463. (*BufferLength) = n_read;
  464. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  465. char MsgBuffer[CLOG];
  466. sprintf( MsgBuffer, "Received %d bytes", n_read );
  467. Log( MsgBuffer );
  468. #endif
  469. // Error Handling
  470. if( n_read < 0 ){
  471. #ifndef _DEBUG_SERIAL_COMMUNICATION_
  472. char MsgBuffer[CLOG];
  473. #endif
  474. sprintf( MsgBuffer, "Error %d reading: %s", errno, strerror (errno) );
  475. Log( _ERROR, MsgBuffer );
  476. };
  477. return n_read;
  478. */
  479. #ifdef __linux__
  480. int RetVal;
  481. do
  482. {
  483. RetVal = read(m_iInFID, Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH);
  484. if (RetVal == -1 && errno == EINTR)
  485. {
  486. SLEEP(1);
  487. }
  488. }
  489. while (RetVal == -1 && errno == EINTR);
  490. #else
  491. DWORD RetVal = 0;
  492. OVERLAPPED osReader = {0};
  493. if (! ReadFile(m_ioFID, Buffer, MAXIMAL_SERIAL_IO_BUFFER_LENGTH, &RetVal, &osReader))
  494. {
  495. if (GetLastError() != ERROR_IO_PENDING)
  496. { // read not delayed?
  497. char tBuffer[256];
  498. sprintf(tBuffer, "Communication Error %d!", GetLastError());
  499. LOG_ERROR(tBuffer);
  500. // Error in communications; report it.
  501. return -1; // RetVal;
  502. }
  503. else
  504. {
  505. // fWaitingOnRead = TRUE;
  506. #ifdef DEBUG_EXTRA
  507. LOG("WREAD!");
  508. #endif
  509. };
  510. }
  511. else
  512. {
  513. // read completed immediately
  514. #ifdef DEBUG_EXTRA
  515. LOG("IMREAD!");
  516. #endif
  517. };
  518. #endif // Linux
  519. #ifdef DEBUG
  520. if (RetVal > 0)
  521. {
  522. char MsgBuffer[CLOG];
  523. if (RetVal > 1 || RetVal == 0)
  524. {
  525. sprintf(MsgBuffer, "Received %d bytes", RetVal);
  526. }
  527. else
  528. {
  529. sprintf(MsgBuffer, "Received 1 byte");
  530. };
  531. LOG(MsgBuffer);
  532. #ifdef DEBUG_SERIAL_COMMUNICATION
  533. for (int i = 0; i < RetVal; i++)
  534. {
  535. sprintf(MsgBuffer, "0x%02X", Buffer[i]);
  536. LOG(MsgBuffer);
  537. };
  538. #endif
  539. };
  540. #endif
  541. if (RetVal > 0)
  542. {
  543. *BufferLength = RetVal;
  544. return RetVal;
  545. };
  546. *BufferLength = 0;
  547. if (RetVal == -1)
  548. {
  549. return 0;
  550. }
  551. if (RetVal == 0)
  552. {
  553. errno = 0;
  554. return 0;
  555. };
  556. /* r < -1, should never happen. */
  557. errno = EIO;
  558. return 0;
  559. }
  560. #undef LOG
  561. #undef LOG_ERROR