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.

385 lines
11 KiB

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 [%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. FILE_GENERIC_READ | GENERIC_WRITE, // 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] Out 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 = 0;
  210. CommTimeouts.ReadTotalTimeoutMultiplier = 0;
  211. CommTimeouts.ReadTotalTimeoutConstant = 10;
  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) return -1;
  240. #ifdef __linux__
  241. int RetVal;
  242. do{
  243. RetVal = write( m_iOutFID, Data, DataSize );
  244. usleep(100);
  245. } while (RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK));
  246. #else
  247. DWORD RetVal;
  248. //OVERLAPPED ov;
  249. //if (!
  250. WriteFile(m_ioFID, Data, DataSize, &RetVal, NULL);//){
  251. // RetVal = 0;
  252. // };
  253. #endif
  254. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  255. char MsgBuffer[CLOG];
  256. sprintf( MsgBuffer, "Sent %d byte(s) of %d", RetVal, DataSize );
  257. LOG( MsgBuffer );
  258. for( int i = 0; i < DataSize; i++ ){
  259. sprintf( MsgBuffer, "0x%02X", Data[i] );
  260. LOG( MsgBuffer );
  261. };
  262. #endif
  263. return RetVal;
  264. //*/
  265. /*
  266. char const *p = (char const *)Data;
  267. char const *const q = (char const *)Data + DataSize;
  268. ssize_t n;
  269. char MsgBuffer[CLOG];
  270. sprintf( MsgBuffer, "Prepare to Send %d Bytes", DataSize );
  271. Log( MsgBuffer );
  272. while (p < q) {
  273. do {
  274. n = write( m_iOutFID, p, (size_t)(q - p));
  275. } while (n == (ssize_t)-1 && errno == EINTR);
  276. if (n == (ssize_t)-1 && errno == EWOULDBLOCK) {
  277. // Sleep for a millisecond, then retry.
  278. usleep(1000);
  279. continue;
  280. }
  281. if (n == (ssize_t)-1)
  282. return errno;
  283. else
  284. if (n < (ssize_t)1)
  285. return EIO;
  286. p += (size_t)n;
  287. }
  288. sprintf( MsgBuffer, "Sent %d Bytes", DataSize );
  289. Log( MsgBuffer );
  290. if (p != q)
  291. return EIO;
  292. return DataSize;
  293. */
  294. };
  295. int serialPort::recptBlock( unsigned char *Buffer, int *BufferLength ){
  296. if( !m_bInitComplete ) return 0;
  297. /*
  298. int n_read = read( m_iInFID, Buffer, sizeof(Buffer) );
  299. (*BufferLength) = n_read;
  300. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  301. char MsgBuffer[CLOG];
  302. sprintf( MsgBuffer, "Received %d bytes", n_read );
  303. Log( MsgBuffer );
  304. #endif
  305. // Error Handling
  306. if( n_read < 0 ){
  307. #ifndef _DEBUG_SERIAL_COMMUNICATION_
  308. char MsgBuffer[CLOG];
  309. #endif
  310. sprintf( MsgBuffer, "Error %d reading: %s", errno, strerror (errno) );
  311. Log( _ERROR, MsgBuffer );
  312. };
  313. return n_read;
  314. */
  315. #ifdef __linux__
  316. int RetVal;
  317. do {
  318. RetVal = read( m_iInFID, Buffer, MAXIMAL_SERIAL_PACKET_LENGTH );
  319. usleep(100);
  320. } while (RetVal == -1 && errno == EINTR);
  321. #else
  322. DWORD RetVal;
  323. //OVERLAPPED ov;
  324. if (!ReadFile(m_ioFID, Buffer, MAXIMAL_SERIAL_PACKET_LENGTH, &RetVal, NULL)){
  325. *BufferLength = 0;
  326. return -1;
  327. };
  328. #endif
  329. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  330. if (RetVal > 0){
  331. char MsgBuffer[256];
  332. if( RetVal > 1 ) {
  333. sprintf(MsgBuffer, "Received %d bytes", RetVal);
  334. } else {
  335. sprintf(MsgBuffer, "Received 1 byte");
  336. };
  337. LOG(MsgBuffer);
  338. };
  339. #endif
  340. *BufferLength = RetVal;
  341. return (int) RetVal;
  342. };
  343. #undef LOG
  344. #undef LOG_ERROR