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.

490 lines
14 KiB

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