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.

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