comparison host/tools/Utils/common/eCosSerial.cpp @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents 6736c52df507
children 74dbf4c3f2e1
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
48 CeCosSerial::CeCosSerial(): 48 CeCosSerial::CeCosSerial():
49 m_nErr(0), 49 m_nErr(0),
50 m_pHandle(0), 50 m_pHandle(0),
51 m_nDataBits(8), 51 m_nDataBits(8),
52 m_nStopBits(ONE_STOP_BIT), 52 m_nStopBits(ONE_STOP_BIT),
53 m_bXONXOFFFlowControl(0),
54 m_bRTSCTSFlowControl(0),
55 m_bDSRDTRFlowControl(0),
53 m_bParity(false), 56 m_bParity(false),
54 m_nBaud(0), 57 m_nBaud(0),
55 m_nTotalReadTimeout(10*1000), 58 m_nTotalReadTimeout(10*1000),
56 m_nTotalWriteTimeout(10*1000), 59 m_nTotalWriteTimeout(10*1000),
57 m_nInterCharReadTimeout(500), 60 m_nInterCharReadTimeout(500),
68 CeCosSerial::CeCosSerial(LPCTSTR pszPort,int nBaud): 71 CeCosSerial::CeCosSerial(LPCTSTR pszPort,int nBaud):
69 m_nErr(0), 72 m_nErr(0),
70 m_pHandle(0), 73 m_pHandle(0),
71 m_nDataBits(8), 74 m_nDataBits(8),
72 m_nStopBits(ONE_STOP_BIT), 75 m_nStopBits(ONE_STOP_BIT),
76 m_bXONXOFFFlowControl(0),
77 m_bRTSCTSFlowControl(0),
78 m_bDSRDTRFlowControl(0),
73 m_bParity(false), 79 m_bParity(false),
74 m_nTotalReadTimeout(10*1000), 80 m_nTotalReadTimeout(10*1000),
75 m_nTotalWriteTimeout(10*1000), 81 m_nTotalWriteTimeout(10*1000),
76 m_nInterCharReadTimeout(500), 82 m_nInterCharReadTimeout(500),
77 m_nInterCharWriteTimeout(500), 83 m_nInterCharWriteTimeout(500),
105 } 111 }
106 112
107 bool CeCosSerial:: SetStopBits(StopBitsType n,bool bApplySettingsNow/*=true*/) 113 bool CeCosSerial:: SetStopBits(StopBitsType n,bool bApplySettingsNow/*=true*/)
108 { 114 {
109 m_nStopBits=n; 115 m_nStopBits=n;
116 return 0==m_pHandle || !bApplySettingsNow || ApplySettings();
117 }
118
119 bool CeCosSerial:: SetXONXOFFFlowControl(bool b,bool bApplySettingsNow/*=true*/)
120 {
121 m_bXONXOFFFlowControl=b;
122 return 0==m_pHandle || !bApplySettingsNow || ApplySettings();
123 }
124
125 bool CeCosSerial:: SetRTSCTSFlowControl(bool b,bool bApplySettingsNow/*=true*/)
126 {
127 m_bRTSCTSFlowControl=b;
128 return 0==m_pHandle || !bApplySettingsNow || ApplySettings();
129 }
130
131 bool CeCosSerial:: SetDSRDTRFlowControl(bool b,bool bApplySettingsNow/*=true*/)
132 {
133 m_bDSRDTRFlowControl=b;
110 return 0==m_pHandle || !bApplySettingsNow || ApplySettings(); 134 return 0==m_pHandle || !bApplySettingsNow || ApplySettings();
111 } 135 }
112 136
113 bool CeCosSerial:: SetReadTimeOuts(int nTotal,int nBetweenChars,bool bApplySettingsNow/*=true*/) // mSec 137 bool CeCosSerial:: SetReadTimeOuts(int nTotal,int nBetweenChars,bool bApplySettingsNow/*=true*/) // mSec
114 { 138 {
183 dcb.BaudRate, 207 dcb.BaudRate,
184 dcb.Parity, 208 dcb.Parity,
185 arpszStopbits[dcb.StopBits], 209 arpszStopbits[dcb.StopBits],
186 dcb.ByteSize); 210 dcb.ByteSize);
187 211
188 // No control over the following yet 212 if (m_bDSRDTRFlowControl) {
189 dcb.fDtrControl=DTR_CONTROL_ENABLE; 213 dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
214 dcb.fOutxDsrFlow = 1;
215 } else {
216 dcb.fDtrControl = DTR_CONTROL_ENABLE;
217 dcb.fOutxDsrFlow = 0;
218 }
219
220 if (m_bRTSCTSFlowControl) {
221 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
222 dcb.fOutxCtsFlow = 1;
223 } else {
224 dcb.fRtsControl = RTS_CONTROL_ENABLE;
225 dcb.fOutxCtsFlow = 0;
226 }
227
190 dcb.fTXContinueOnXoff=1; 228 dcb.fTXContinueOnXoff=1;
191 dcb.fRtsControl=RTS_CONTROL_ENABLE;
192 dcb.fAbortOnError=1;
193 dcb.XonLim=2048;
194 dcb.XoffLim=512;
195 dcb.XonChar=17; 229 dcb.XonChar=17;
196 dcb.XoffChar=19; 230 dcb.XoffChar=19;
231 if (m_bXONXOFFFlowControl) {
232 dcb.XonLim=512;
233 dcb.XoffLim=512;
234 dcb.fOutX=1;
235 dcb.fInX=1;
236 } else {
237 dcb.XonLim=2048;
238 dcb.XoffLim=512;
239 dcb.fOutX=0;
240 dcb.fInX=0;
241 }
242 dcb.fAbortOnError=1;
197 243
198 HANDLE hCom=(HANDLE)m_pHandle; 244 HANDLE hCom=(HANDLE)m_pHandle;
199 if (!SetCommState(hCom, &dcb)) { 245 if (!SetCommState(hCom, &dcb)) {
200 SaveError(); 246 SaveError();
201 ERROR(_T("Failed to set comm state - port %s handle=%d err=%d\n"),(LPCTSTR)m_strPort,hCom,GetLastError()); 247 ERROR(_T("Failed to set comm state - port %s handle=%d err=%d\n"),(LPCTSTR)m_strPort,hCom,GetLastError());
420 { 466 {
421 buf.c_cflag &= ~(PARENB | PARODD); // no parity. 467 buf.c_cflag &= ~(PARENB | PARODD); // no parity.
422 if (m_bParity) // even parity. 468 if (m_bParity) // even parity.
423 buf.c_cflag |= PARENB; 469 buf.c_cflag |= PARENB;
424 } 470 }
471
472 // Set flow control
473 {
474 buf.c_iflag &= ~(IXON|IXOFF);
475 #ifdef CRTSCTS
476 buf.c_cflag &= ~CRTSCTS;
477 #endif
478 if ( m_bXONXOFFFlowControl ) {
479 buf.c_iflag |= (IXON|IXOFF);
480 }
481 if ( m_bRTSCTSFlowControl ) {
482 #ifdef CRTSCTS
483 buf.c_cflag |= CRTSCTS;
484 #else
485 return false;
486 #endif
487 }
488 if ( m_bDSRDTRFlowControl ) {
489 #ifdef CDSRDTR
490 buf.c_cflag |= CDSRDTR;
491 #else
492 return false;
493 #endif
494 }
495
496
497 }
425 498
426 // Set the new settings 499 // Set the new settings
427 if (tcsetattr((int) m_pHandle, TCSADRAIN, &buf)) { 500 if (tcsetattr((int) m_pHandle, TCSADRAIN, &buf)) {
428 fprintf(stderr, _T("Error: tcsetattr\n")); 501 fprintf(stderr, _T("Error: tcsetattr\n"));
429 return false; 502 return false;
466 return true; 539 return true;
467 ERROR(_T("Read failed: %d\n"), errno); 540 ERROR(_T("Read failed: %d\n"), errno);
468 return false; 541 return false;
469 } 542 }
470 nRead = n; 543 nRead = n;
544 #if 0
545 if (n>0) {
546 unsigned int i;
547 fprintf(stderr, "%d:", nRead);
548 for (i = 0; i < nRead; i++)
549 fprintf(stderr, "%02x!", ((unsigned char *)pBuf)[i]);
550 fprintf(stderr, "\n");
551 }
552 #endif
471 return true; 553 return true;
472 } 554 }
473 555
474 // Blocking reads: emulate the Windows semantics: 556 // Blocking reads: emulate the Windows semantics:
475 // If m_nTotalReadTimeout elapses before we see the first TCHAR, 557 // If m_nTotalReadTimeout elapses before we see the first TCHAR,
495 int n = read((int)m_pHandle, pData, nSize); 577 int n = read((int)m_pHandle, pData, nSize);
496 if (-1 == n && EAGAIN != errno) { 578 if (-1 == n && EAGAIN != errno) {
497 ERROR(_T("Read failed: %d\n"), errno); 579 ERROR(_T("Read failed: %d\n"), errno);
498 return false; // FAILED 580 return false; // FAILED
499 } 581 }
500 nRead += n; 582 else if (n > 0) {
501 pData += n; 583 #if 0
502 nSize -= n; 584 unsigned int i;
585 fprintf(stderr, "%d:", nRead);
586 for (i = 0; i < nRead; i++)
587 fprintf(stderr, "%02x!", ((unsigned char *)pBuf)[i]);
588 fprintf(stderr, "\n");
589 #endif
590 nRead += n;
591 pData += n;
592 nSize -= n;
593 }
503 594
504 // Now use inter-char timeout. 595 // Now use inter-char timeout.
505 tv.tv_sec = m_nInterCharReadTimeout / 1000; 596 tv.tv_sec = m_nInterCharReadTimeout / 1000;
506 tv.tv_usec = (m_nInterCharReadTimeout % 1000) * 1000; 597 tv.tv_usec = (m_nInterCharReadTimeout % 1000) * 1000;
507 } 598 }