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.

467 lines
13 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
​
​
​
​
​
​
​
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( "serialPort", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "serialPort", 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. switch( m_iLineSpeed ){
  127. case 4000000: cfsetspeed(&m_sTTY, B4000000 ); break;
  128. case 3500000: cfsetspeed(&m_sTTY, B3500000 ); break;
  129. case 3000000: cfsetspeed(&m_sTTY, B3000000 ); break;
  130. case 2500000: cfsetspeed(&m_sTTY, B2500000 ); break;
  131. case 2000000: cfsetspeed(&m_sTTY, B2000000 ); break;
  132. case 1500000: cfsetspeed(&m_sTTY, B1500000 ); break;
  133. case 1152000: cfsetspeed(&m_sTTY, B1152000 ); break;
  134. case 1000000: cfsetspeed(&m_sTTY, B1000000 ); break;
  135. case 921600: cfsetspeed(&m_sTTY, B921600 ); break;
  136. case 460800: cfsetspeed(&m_sTTY, B460800 ); break;
  137. case 230400: cfsetspeed(&m_sTTY, B230400 ); break;
  138. case 115200: cfsetospeed (&m_sTTY, B115200); cfsetispeed (&m_sTTY, B115200); break;
  139. case 19200: cfsetospeed (&m_sTTY, B19200); cfsetispeed (&m_sTTY, B19200); break;
  140. case 9600: cfsetospeed (&m_sTTY, B9600); cfsetispeed (&m_sTTY, B9600); break;
  141. default:{
  142. close( m_iInFID );
  143. close( m_iOutFID );
  144. sprintf( MsgBuffer, "Error cannot set speed as [%d] bod", Speed );
  145. LOG_ERROR( MsgBuffer );
  146. return false;
  147. };
  148. };
  149. /* Setting other Port Stuff */
  150. m_sTTY.c_cflag &= ~PARENB; // Make 8n1
  151. m_sTTY.c_cflag &= ~CSTOPB;
  152. m_sTTY.c_cflag &= ~CSIZE;
  153. m_sTTY.c_cflag |= CS8;
  154. m_sTTY.c_cflag &= ~CRTSCTS; // no flow control
  155. m_sTTY.c_lflag = 0; // no signaling chars, no echo, no canonical processing
  156. m_sTTY.c_oflag = 0; // no remapping, no delays
  157. m_sTTY.c_cc[VMIN] = 0; // read doesn't block
  158. m_sTTY.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  159. m_sTTY.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
  160. m_sTTY.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  161. m_sTTY.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
  162. m_sTTY.c_oflag &= ~OPOST; // make raw
  163. /* Flush Port, then applies attributes */
  164. tcflush( m_iOutFID, TCIFLUSH );
  165. do{
  166. result = tcsetattr( m_iOutFID, TCSANOW, &m_sTTY );
  167. }while( result == -1 && errno == EINTR );
  168. if( result == -1 ){
  169. int const error = errno;
  170. do{
  171. result = close( m_iOutFID );
  172. }while( result == -1 && errno == EINTR );
  173. errno = error;
  174. sprintf( MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror (errno) );
  175. LOG_ERROR( MsgBuffer );
  176. return false;
  177. };
  178. do{
  179. result = tcsetattr( m_iInFID, TCSANOW, &m_sTTY );
  180. }while( result == -1 && errno == EINTR );
  181. if( result == -1 ){
  182. int const error = errno;
  183. do{
  184. result = close( m_iInFID );
  185. }while( result == -1 && errno == EINTR );
  186. errno = error;
  187. sprintf( MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror (errno) );
  188. LOG_ERROR( MsgBuffer );
  189. return false;
  190. };
  191. #else
  192. m_ioFID = CreateFile( (LPCSTR)m_sDevicePath,
  193. GENERIC_READ | GENERIC_WRITE,
  194. 0UL, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING,
  195. //FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,
  196. NULL );
  197. if (m_ioFID == INVALID_HANDLE_VALUE){
  198. setConnectionStatus(CS_DISCONNECTED);
  199. sprintf(MsgBuffer, "Port [%s] Open Failed Err: %d", m_sDevicePath, GetLastError());
  200. LOG_ERROR(MsgBuffer);
  201. return false;
  202. };
  203. // Initialize the DCBlength member.
  204. PortDCB.DCBlength = sizeof(DCB);
  205. // Get the default port setting information.
  206. GetCommState(m_ioFID, &PortDCB);
  207. // Change the DCB structure settings
  208. /*
  209. PortDCB.fBinary = TRUE; // Binary mode; no EOF check
  210. PortDCB.fParity = NOPARITY; // Enable parity checking
  211. PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
  212. PortDCB.fErrorChar = FALSE; // Disable error replacement
  213. PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
  214. PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error
  215. PortDCB.fNull = FALSE; // Disable null stripping
  216. PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
  217. PortDCB.BaudRate = Speed;
  218. PortDCB.StopBits = ONESTOPBIT;
  219. */
  220. PortDCB.BaudRate = Speed;
  221. PortDCB.ByteSize = 8;
  222. PortDCB.Parity = NOPARITY;
  223. PortDCB.StopBits = ONESTOPBIT;
  224. PortDCB.EvtChar = 0;
  225. PortDCB.fParity = (PortDCB.Parity != NOPARITY);
  226. PortDCB.fOutxCtsFlow = FALSE;
  227. PortDCB.fOutxDsrFlow = FALSE;
  228. PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
  229. PortDCB.fDsrSensitivity = FALSE;
  230. PortDCB.fOutX = FALSE;
  231. PortDCB.fInX = FALSE;
  232. PortDCB.fErrorChar = FALSE;
  233. PortDCB.fNull = FALSE;
  234. // dcb.fRtsControl = RTS_CONTROL_DISABLE;
  235. PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
  236. // dcb.fRtsControl = RTS_CONTROL_TOGGLE; // specially for PROMAG USB->RS485 converter
  237. PortDCB.fAbortOnError = FALSE;
  238. PortDCB.fBinary = TRUE;
  239. PortDCB.fTXContinueOnXoff = TRUE;
  240. //Re-configure the port with the new DCB structure.
  241. if (!SetCommState(m_ioFID, &PortDCB)) {
  242. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommState Failed)", m_sDevicePath);
  243. LOG_ERROR(MsgBuffer);
  244. CloseHandle(m_ioFID);
  245. return false;
  246. };
  247. COMMTIMEOUTS CommTimeouts;
  248. // Retrieve the time-out parameters for all read and write operations
  249. GetCommTimeouts(m_ioFID, &CommTimeouts);
  250. /*
  251. CommTimeouts.ReadIntervalTimeout = 20;
  252. CommTimeouts.ReadTotalTimeoutMultiplier = 10;
  253. CommTimeouts.ReadTotalTimeoutConstant = 100;
  254. CommTimeouts.WriteTotalTimeoutMultiplier = 10;
  255. CommTimeouts.WriteTotalTimeoutConstant = 100;
  256. */
  257. CommTimeouts.ReadIntervalTimeout = 0UL;
  258. CommTimeouts.ReadTotalTimeoutMultiplier = 0UL;
  259. CommTimeouts.ReadTotalTimeoutConstant = 0UL;
  260. CommTimeouts.WriteTotalTimeoutMultiplier = 0UL;
  261. CommTimeouts.WriteTotalTimeoutConstant = 0UL;
  262. // Set the time-out parameters for all read and write operations on the port.
  263. if (!SetCommTimeouts(m_ioFID, &CommTimeouts)){
  264. sprintf(MsgBuffer, "Port [%s] Could not create the read thread.(SetCommTimeouts Failed)", m_sDevicePath);
  265. LOG_ERROR(MsgBuffer);
  266. CloseHandle(m_ioFID);
  267. return false;
  268. };
  269. // Clear the port of any existing data.
  270. if (PurgeComm(m_ioFID, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT ) == 0){
  271. sprintf(MsgBuffer, "Port [%s] Clearing The Port Failed", m_sDevicePath);
  272. LOG_ERROR(MsgBuffer);
  273. CloseHandle(m_ioFID);
  274. return false;
  275. };
  276. EscapeCommFunction(m_ioFID, SETRTS);
  277. EscapeCommFunction(m_ioFID, SETDTR);
  278. //Set DataTerminalReady 1
  279. #endif
  280. setConnectionStatus(CS_CONNECTED);
  281. return true;
  282. };
  283. int serialPort::sendBlock( unsigned char *Data, int DataSize ){
  284. if(!m_bInitComplete) {
  285. #ifdef DEBUG
  286. LOG( "Port is not Ready to Send" );
  287. #endif
  288. return -1;
  289. };
  290. #ifdef __linux__
  291. int RetVal;
  292. do{
  293. RetVal = write( m_iOutFID, Data, DataSize );
  294. usleep(100);
  295. } while (RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK));
  296. #else
  297. DWORD RetVal;
  298. //OVERLAPPED ov;
  299. //if (!
  300. WriteFile(m_ioFID, Data, DataSize, &RetVal, NULL);//){
  301. // RetVal = 0;
  302. // };
  303. #endif
  304. #ifdef DEBUG
  305. char MsgBuffer[CLOG];
  306. sprintf( MsgBuffer, "Sent %d byte(s) of %d", RetVal, DataSize );
  307. LOG( MsgBuffer );
  308. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  309. for( int i = 0; i < DataSize; i++ ){
  310. sprintf( MsgBuffer, "0x%02X", Data[i] );
  311. LOG( MsgBuffer );
  312. };
  313. #endif
  314. #endif
  315. return RetVal;
  316. //*/
  317. /*
  318. char const *p = (char const *)Data;
  319. char const *const q = (char const *)Data + DataSize;
  320. ssize_t n;
  321. char MsgBuffer[CLOG];
  322. sprintf( MsgBuffer, "Prepare to Send %d Bytes", DataSize );
  323. Log( MsgBuffer );
  324. while (p < q) {
  325. do {
  326. n = write( m_iOutFID, p, (size_t)(q - p));
  327. } while (n == (ssize_t)-1 && errno == EINTR);
  328. if (n == (ssize_t)-1 && errno == EWOULDBLOCK) {
  329. // Sleep for a millisecond, then retry.
  330. usleep(1000);
  331. continue;
  332. }
  333. if (n == (ssize_t)-1)
  334. return errno;
  335. else
  336. if (n < (ssize_t)1)
  337. return EIO;
  338. p += (size_t)n;
  339. }
  340. sprintf( MsgBuffer, "Sent %d Bytes", DataSize );
  341. Log( MsgBuffer );
  342. if (p != q)
  343. return EIO;
  344. return DataSize;
  345. */
  346. };
  347. int serialPort::recptBlock( unsigned char *Buffer, int *BufferLength ){
  348. if (!m_bInitComplete) {
  349. #ifdef DEBUG
  350. LOG("Port is Not Ready to Read");
  351. #endif
  352. return -1;
  353. };
  354. /*
  355. int n_read = read( m_iInFID, Buffer, sizeof(Buffer) );
  356. (*BufferLength) = n_read;
  357. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  358. char MsgBuffer[CLOG];
  359. sprintf( MsgBuffer, "Received %d bytes", n_read );
  360. Log( MsgBuffer );
  361. #endif
  362. // Error Handling
  363. if( n_read < 0 ){
  364. #ifndef _DEBUG_SERIAL_COMMUNICATION_
  365. char MsgBuffer[CLOG];
  366. #endif
  367. sprintf( MsgBuffer, "Error %d reading: %s", errno, strerror (errno) );
  368. Log( _ERROR, MsgBuffer );
  369. };
  370. return n_read;
  371. */
  372. #ifdef __linux__
  373. int RetVal;
  374. do {
  375. RetVal = read( m_iInFID, Buffer, MAXIMAL_SERIAL_PACKET_LENGTH );
  376. usleep(100);
  377. } while (RetVal == -1 && errno == EINTR);
  378. #else
  379. DWORD RetVal = 0;
  380. OVERLAPPED osReader = {0};
  381. if (!ReadFile(m_ioFID, Buffer, MAXIMAL_SERIAL_PACKET_LENGTH, &RetVal, &osReader)) {
  382. if (GetLastError() != ERROR_IO_PENDING){ // read not delayed?
  383. char tBuffer[256];
  384. sprintf(tBuffer, "Communication Error %d!", GetLastError());
  385. LOG_ERROR(tBuffer);
  386. // Error in communications; report it.
  387. return -1;// RetVal;
  388. }else{
  389. //fWaitingOnRead = TRUE;
  390. };
  391. } else {
  392. // read completed immediately
  393. }
  394. #endif
  395. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  396. if( RetVal > -1 ){
  397. char MsgBuffer[CLOG];
  398. if( RetVal > 1 || RetVal == 0 ) {
  399. sprintf( MsgBuffer, "Received %d bytes", RetVal );
  400. } else {
  401. sprintf( MsgBuffer, "Received 1 byte");
  402. };
  403. LOG( MsgBuffer );
  404. };
  405. #endif
  406. if( RetVal > 0) {
  407. *BufferLength = RetVal;
  408. return RetVal;
  409. };
  410. *BufferLength = 0;
  411. if( RetVal == -1) return 0;
  412. if( RetVal == 0 ){
  413. errno = 0;
  414. return 0;
  415. };
  416. /* r < -1, should never happen. */
  417. errno = EIO;
  418. return 0;
  419. };
  420. #undef LOG
  421. #undef LOG_ERROR