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.

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