--- mstools/samples/comm/tty.c 2018/08/09 18:20:59 1.1.1.1 +++ mstools/samples/comm/tty.c 2018/08/09 18:24:33 1.1.1.3 @@ -1,3 +1,14 @@ + +/******************************************************************************\ +* This is a part of the Microsoft Source Code Samples. +* Copyright (C) 1993 Microsoft Corporation. +* All rights reserved. +* This source code is only intended as a supplement to +* Microsoft Development Tools and/or WinHelp documentation. +* See these sources for detailed information regarding the +* Microsoft samples programs. +\******************************************************************************/ + //--------------------------------------------------------------------------- // // Module: tty.c @@ -12,44 +23,6 @@ // Description of functions: // Descriptions are contained in the function headers. // -// Development Team: -// Bryan A. Woodruff -// -// History: Date Author Comment -// -// 5/ 8/91 BryanW Wrote it. -// -// 10/18/91 BryanW Sanitized code & comments. -// -// 10/22/91 BryanW Minor bug fixes. -// -// 12/31/91 BryanW Ported to Win32 (NT). -// -// 2/10/92 BryanW Fixed a problem with ReadCommBlock() -// and error detection. ReadComm() is -// now performed... if the result is < 0 -// the error status is read using -// GetCommError() and displayed. The -// length is adjusted (*= -1). -// -// 2/13/92 BryanW Updated to provide the option to -// implement/handle CN_RECEIVE -// notifications. -// -// 2/13/92 BryanW Implemented forced DTR control. DTR -// is asserted on connect and dropped -// on disconnect. -// -// 2/21/92 BryanW Removed overhead of local memory -// functions - now uses LPTR for -// allocation from heap. -// -// 2/26/92 BryanW Fixed off by one problem in paint -// calculations. -// -// 6/15/92 BryanW Ported the "updated" version of TTY -// for Win 3.1 including bug fixes. -// //--------------------------------------------------------------------------- // // Written by Microsoft Product Support Services, Windows Developer Support. @@ -122,7 +95,7 @@ BOOL NEAR InitApplication( HANDLE hInsta // register tty window class - wndclass.style = NULL ; + wndclass.style = 0 ; wndclass.lpfnWndProc = TTYWndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = TTYEXTRABYTES ; @@ -232,7 +205,7 @@ LRESULT FAR PASCAL TTYWndProc( HWND hWnd return ( FALSE ) ; GoModalDialogBoxParam( GETHINST( hWnd ), MAKEINTRESOURCE( SETTINGSDLGBOX ), hWnd, - SettingsDlgProc, + (DLGPROC) SettingsDlgProc, (LPARAM) (LPSTR) npTTYInfo ) ; // if fConnected, set new COM parameters @@ -250,11 +223,11 @@ LRESULT FAR PASCAL TTYWndProc( HWND hWnd GoModalDialogBoxParam ( GETHINST( hWnd ), MAKEINTRESOURCE( ABOUTDLGBOX ), hWnd, - AboutDlgProc, NULL ) ; + (DLGPROC) AboutDlgProc, 0L ) ; break; case IDM_EXIT: - PostMessage( hWnd, WM_CLOSE, NULL, 0L ) ; + PostMessage( hWnd, WM_CLOSE, 0, 0L ) ; break ; } } @@ -429,7 +402,7 @@ LRESULT NEAR CreateTTYInfo( HWND hWnd ) // setup default font information - LFTTYFONT( npTTYInfo ).lfHeight = 12 ; + LFTTYFONT( npTTYInfo ).lfHeight = 9 ; LFTTYFONT( npTTYInfo ).lfWidth = 0 ; LFTTYFONT( npTTYInfo ).lfEscapement = 0 ; LFTTYFONT( npTTYInfo ).lfOrientation = 0 ; @@ -442,7 +415,7 @@ LRESULT NEAR CreateTTYInfo( HWND hWnd ) LFTTYFONT( npTTYInfo ).lfClipPrecision = CLIP_DEFAULT_PRECIS ; LFTTYFONT( npTTYInfo ).lfQuality = DEFAULT_QUALITY ; LFTTYFONT( npTTYInfo ).lfPitchAndFamily = FIXED_PITCH | FF_MODERN ; - LFTTYFONT( npTTYInfo ).lfFaceName[0] = NULL ; + lstrcpy( LFTTYFONT( npTTYInfo ).lfFaceName, "FixedSys" ) ; // set TTYInfo handle before any further message processing. @@ -972,6 +945,9 @@ BOOL NEAR ProcessCOMMNotification( HWND if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd ))) return ( FALSE ) ; + if (!CONNECTED( npTTYInfo )) + return ( FALSE ) ; + #ifdef WIN32 // verify that it is a COMM event sent by our thread @@ -1108,7 +1084,7 @@ BOOL NEAR ProcessTTYCharacter( HWND hWnd // handle to TTY window // // Win-32 Porting Issues: -// - OpenComm() is not supported under Win-32. Use OpenFile() +// - OpenComm() is not supported under Win-32. Use CreateFile() // and setup for OVERLAPPED_IO. // - Win-32 has specific communication timeout parameters. // - Created the secondary thread for event notification. @@ -1119,8 +1095,8 @@ BOOL NEAR ProcessTTYCharacter( HWND hWnd //--------------------------------------------------------------------------- BOOL NEAR OpenConnection( HWND hWnd ) -{ - char szPort[ 10 ], szTemp[ 10 ] ; +{ + char szPort[ 15 ], szTemp[ 10 ] ; BOOL fRetVal ; HCURSOR hOldCursor, hWaitCursor ; HMENU hMenu ; @@ -1139,10 +1115,26 @@ BOOL NEAR OpenConnection( HWND hWnd ) hWaitCursor = LoadCursor( NULL, IDC_WAIT ) ; hOldCursor = SetCursor( hWaitCursor ) ; +#ifdef WIN32 + // HACK! This checks for the PORT number defined by + // the combo box selection. If it is greater than the + // maximum number of ports, assume TELNET. + + if (PORT( npTTYInfo ) > MAXPORTS) + lstrcpy( szPort, "\\\\.\\TELNET" ) ; + else + { + // load the COM prefix string and append port number + + LoadString( GETHINST( hWnd ), IDS_COMPREFIX, szTemp, sizeof( szTemp ) ) ; + wsprintf( szPort, "%s%d", (LPSTR) szTemp, PORT( npTTYInfo ) ) ; + } +#else // load the COM prefix string and append port number LoadString( GETHINST( hWnd ), IDS_COMPREFIX, szTemp, sizeof( szTemp ) ) ; wsprintf( szPort, "%s%d", (LPSTR) szTemp, PORT( npTTYInfo ) ) ; +#endif // open COMM device @@ -1152,15 +1144,28 @@ BOOL NEAR OpenConnection( HWND hWnd ) 0, // exclusive access NULL, // no security attrs OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // overlapped I/O NULL )) == (HANDLE) -1 ) return ( FALSE ) ; else { + // get any early notifications + + SetCommMask( COMDEV( npTTYInfo ), EV_RXCHAR ) ; + + // setup device buffers + + SetupComm( COMDEV( npTTYInfo ), 4096, 4096 ) ; + // purge any information in the buffer - PurgeComm( COMDEV( npTTYInfo ), PURGE_TXABORT | PURGE_RXABORT | - PURGE_TXCLEAR | PURGE_RXCLEAR ) ; + // HACK! HACK! Not really needed... the buffers are allocated + // and clear. If TELNET is active, the buffer will contain + // immediate data. + +// PurgeComm( COMDEV( npTTYInfo ), PURGE_TXABORT | PURGE_RXABORT | +// PURGE_TXCLEAR | PURGE_RXCLEAR ) ; // set up for overlapped non-blocking I/O @@ -1168,7 +1173,7 @@ BOOL NEAR OpenConnection( HWND hWnd ) CommTimeOuts.ReadTotalTimeoutMultiplier = 0 ; CommTimeOuts.ReadTotalTimeoutConstant = 0 ; CommTimeOuts.WriteTotalTimeoutMultiplier = 0 ; - CommTimeOuts.WriteTotalTimeoutConstant = 0 ; + CommTimeOuts.WriteTotalTimeoutConstant = 5000 ; SetCommTimeouts( COMDEV( npTTYInfo ), &CommTimeOuts ) ; } #else @@ -1191,7 +1196,7 @@ BOOL NEAR OpenConnection( HWND hWnd ) 0, (LPTHREAD_START_ROUTINE) CommWatchProc, (LPVOID) npTTYInfo, - NULL, &dwThreadID ))) + 0, &dwThreadID ))) { CONNECTED( npTTYInfo ) = FALSE ; CloseHandle( COMDEV( npTTYInfo ) ) ; @@ -1202,11 +1207,15 @@ BOOL NEAR OpenConnection( HWND hWnd ) THREADID( npTTYInfo ) = dwThreadID ; HTHREAD( npTTYInfo ) = hCommWatchThread ; + // Adjust thread priority + + SetThreadPriority( hCommWatchThread, THREAD_PRIORITY_BELOW_NORMAL ) ; + ResumeThread( hCommWatchThread ) ; + // assert DTR EscapeCommFunction( COMDEV( npTTYInfo ), SETDTR ) ; - SetTTYFocus( hWnd ) ; hMenu = GetMenu( hWnd ) ; @@ -1214,6 +1223,12 @@ BOOL NEAR OpenConnection( HWND hWnd ) MF_ENABLED | MF_BYCOMMAND ) ; EnableMenuItem( hMenu, IDM_CONNECT, MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ; + + // kick TELNET into operation + + ProcessCOMMNotification( hWnd, (WPARAM) COMDEV( npTTYInfo ), + MAKELONG( CN_EVENT, 0 ) ) ; + } #else // Under Windows 3.1, we set up notifications from COMM.DRV @@ -1244,7 +1259,6 @@ BOOL NEAR OpenConnection( HWND hWnd ) EscapeCommFunction( COMDEV( npTTYInfo ), SETDTR ) ; - SetTTYFocus( hWnd ) ; hMenu = GetMenu( hWnd ) ; @@ -1408,11 +1422,11 @@ BOOL NEAR CloseConnection( HWND hWnd ) // disable event notification and wait for thread // to halt - SetCommMask( COMDEV( npTTYInfo ), NULL ) ; + SetCommMask( COMDEV( npTTYInfo ), 0 ) ; // block until thread has been halted - while (THREADID( npTTYInfo ) != NULL) ; + while (THREADID( npTTYInfo ) != 0) ; #else // Disable event notification. Using a NULL hWnd tells // the COMM.DRV to disable future notifications. @@ -1424,17 +1438,21 @@ BOOL NEAR CloseConnection( HWND hWnd ) KillTTYFocus( hWnd ) ; +#ifdef WIN32 // drop DTR EscapeCommFunction( COMDEV( npTTYInfo ), CLRDTR ) ; -#ifdef WIN32 // purge any outstanding reads/writes and close device handle PurgeComm( COMDEV( npTTYInfo ), PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ; CloseHandle( COMDEV( npTTYInfo ) ) ; #else + // drop DTR + + EscapeCommFunction( COMDEV( npTTYInfo ), CLRDTR ) ; + // close comm connection CloseComm( COMDEV( npTTYInfo ) ) ; @@ -1481,12 +1499,11 @@ BOOL NEAR CloseConnection( HWND hWnd ) int NEAR ReadCommBlock( HWND hWnd, LPSTR lpszBlock, int nMaxLength ) { #ifdef WIN32 - DWORD nLength ; BOOL fReadStat ; COMSTAT ComStat ; - DWORD dwErrorFlags ; + DWORD dwErrorFlags, dwLength ; #else - int nLength, nError ; + int nError, nLength ; #endif char szError[ 10 ] ; @@ -1502,35 +1519,37 @@ int NEAR ReadCommBlock( HWND hWnd, LPSTR wsprintf( szError, "", dwErrorFlags ) ; WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ; } - nLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ; - if (nLength > 0) + + dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ; + + if (dwLength > 0) { fReadStat = ReadFile( COMDEV( npTTYInfo ), lpszBlock, - nLength, &nLength, &READ_OS( npTTYInfo ) ) ; + dwLength, &dwLength, &READ_OS( npTTYInfo ) ) ; if (!fReadStat) { if (GetLastError() == ERROR_IO_PENDING) { - OutputDebugString( "ReadCommBlock: I/O Pending.\r\n" ) ; - // wait for a second for this transmission to complete if (WaitForSingleObject( READ_OS( npTTYInfo ).hEvent, 1000 )) - nLength = 0 ; + dwLength = 0 ; else { GetOverlappedResult( COMDEV( npTTYInfo ), &READ_OS( npTTYInfo ), - &nLength, FALSE ) ; - READ_OS( npTTYInfo ).Offset += nLength ; + &dwLength, FALSE ) ; + READ_OS( npTTYInfo ).Offset += dwLength ; } } else // some other error occurred - nLength = 0 ; + dwLength = 0 ; } } + + return ( dwLength ) ; #else nLength = ReadComm( COMDEV( npTTYInfo ), lpszBlock, nMaxLength ) ; @@ -1546,9 +1565,9 @@ int NEAR ReadCommBlock( HWND hWnd, LPSTR } } } + return ( nLength ) ; #endif - return ( nLength ) ; } // end of ReadCommBlock() @@ -1591,8 +1610,6 @@ BOOL NEAR WriteCommByte( HWND hWnd, BYTE &dwBytesWritten, &WRITE_OS( npTTYInfo ) ) ; if (!fWriteStat && (GetLastError() == ERROR_IO_PENDING)) { - OutputDebugString( "WriteCommByte: I/O Pending.\r\n" ) ; - // wait for a second for this transmission to complete if (WaitForSingleObject( WRITE_OS( npTTYInfo ).hEvent, 1000 )) @@ -1763,7 +1780,6 @@ BOOL FAR PASCAL AboutDlgProc( HWND hDlg, { #ifdef WIN32 char szBuffer[ MAXLEN_TEMPSTR ], szTemp[ MAXLEN_TEMPSTR ]; - DWORD dwFreeMemory ; WORD wRevision, wVersion ; #else int idModeString ; @@ -1819,6 +1835,7 @@ BOOL FAR PASCAL AboutDlgProc( HWND hDlg, SetDlgItemText( hDlg, IDD_WINDOWSMODE, "NT Mode" ) ; #endif +#ifndef WIN32 // get free memory information dwFreeMemory = GetFreeSpace( 0 ) / 1024L ; @@ -1826,7 +1843,6 @@ BOOL FAR PASCAL AboutDlgProc( HWND hDlg, wsprintf( szBuffer, szTemp, dwFreeMemory ) ; SetDlgItemText( hDlg, IDD_FREEMEM, (LPSTR) szBuffer ) ; -#ifndef WIN32 // get free resources information wFreeResources = GetFreeSystemResources( 0 ) ; @@ -1923,7 +1939,7 @@ VOID NEAR FillComboBox( HINSTANCE hInsta // add it to the combo box LoadString( hInstance, nIDString + wCount, szBuffer, sizeof( szBuffer ) ) ; - wPosition = LOWORD( SendMessage( hCtrlWnd, CB_ADDSTRING, NULL, + wPosition = LOWORD( SendMessage( hCtrlWnd, CB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuffer ) ) ; // use item data to store the actual table value @@ -1934,7 +1950,7 @@ VOID NEAR FillComboBox( HINSTANCE hInsta // if this is our current setting, select it if (*(npTable + wCount) == dwCurrentSetting) - SendMessage( hCtrlWnd, CB_SETCURSEL, (WPARAM) wPosition, NULL ) ; + SendMessage( hCtrlWnd, CB_SETCURSEL, (WPARAM) wPosition, 0L ) ; } } // end of FillComboBox() @@ -1972,7 +1988,7 @@ BOOL NEAR SettingsDlgInit( HWND hDlg ) return ( FALSE ) ; #ifdef WIN32 - wMaxCOM = 4 ; + wMaxCOM = MAXPORTS ; #else wMaxCOM = LOWORD( EscapeCommFunction( NULL, GETMAXCOM ) ) + 1 ; #endif @@ -1986,11 +2002,16 @@ BOOL NEAR SettingsDlgInit( HWND hDlg ) for (wCount = 0; wCount < wMaxCOM; wCount++) { wsprintf( szBuffer, "%s%d", (LPSTR) szTemp, wCount + 1 ) ; - SendDlgItemMessage( hDlg, IDD_PORTCB, CB_ADDSTRING, NULL, + SendDlgItemMessage( hDlg, IDD_PORTCB, CB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuffer ) ; } + +#ifdef WIN32 + SendDlgItemMessage( hDlg, IDD_PORTCB, CB_ADDSTRING, 0, + (LPARAM) (LPSTR) "TELNET" ) ; +#endif SendDlgItemMessage( hDlg, IDD_PORTCB, CB_SETCURSEL, - (WPARAM) (PORT( npTTYInfo ) - 1), NULL ) ; + (WPARAM) (PORT( npTTYInfo ) - 1), 0L ) ; // disable COM port combo box if connection has already been // established (e.g. OpenComm() already successful) @@ -2010,14 +2031,14 @@ BOOL NEAR SettingsDlgInit( HWND hDlg ) { wsprintf( szBuffer, "%d", wCount ) ; wPosition = LOWORD( SendDlgItemMessage( hDlg, IDD_DATABITSCB, - CB_ADDSTRING, NULL, + CB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuffer ) ) ; // if current selection, tell the combo box if (wCount == BYTESIZE( npTTYInfo )) SendDlgItemMessage( hDlg, IDD_DATABITSCB, CB_SETCURSEL, - (WPARAM) wPosition, NULL ) ; + (WPARAM) wPosition, 0L ) ; } // fill parity combo box and make initial selection @@ -2102,7 +2123,7 @@ BOOL NEAR SelectTTYFont( HWND hDlg ) cfTTYFont.lpLogFont = &LFTTYFONT( npTTYInfo ) ; cfTTYFont.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_EFFECTS | CF_INITTOLOGFONTSTRUCT ; - cfTTYFont.lCustData = NULL ; + cfTTYFont.lCustData = 0 ; cfTTYFont.lpfnHook = NULL ; cfTTYFont.lpTemplateName = NULL ; cfTTYFont.hInstance = GETHINST( hDlg ) ; @@ -2149,20 +2170,20 @@ BOOL NEAR SettingsDlgTerm( HWND hDlg ) PORT( npTTYInfo ) = LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_PORTCB, CB_GETCURSEL, - NULL, NULL ) ) + 1 ) ; + 0, 0L ) ) + 1 ) ; // get baud rate selection wSelection = LOWORD( SendDlgItemMessage( hDlg, IDD_BAUDCB, CB_GETCURSEL, - NULL, NULL ) ) ; + 0, 0L ) ) ; #ifdef WIN32 BAUDRATE( npTTYInfo ) = SendDlgItemMessage( hDlg, IDD_BAUDCB, CB_GETITEMDATA, - (WPARAM) wSelection, NULL ) ; + (WPARAM) wSelection, 0L ) ; #else BAUDRATE( npTTYInfo ) = LOWORD( SendDlgItemMessage( hDlg, IDD_BAUDCB, CB_GETITEMDATA, - (WPARAM) wSelection, NULL ) ) ; + (WPARAM) wSelection, 0L ) ) ; #endif // get data bits selection @@ -2170,28 +2191,28 @@ BOOL NEAR SettingsDlgTerm( HWND hDlg ) BYTESIZE( npTTYInfo ) = LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_DATABITSCB, CB_GETCURSEL, - NULL, NULL ) ) + 5 ) ; + 0, 0L ) ) + 5 ) ; // get parity selection wSelection = LOWORD( SendDlgItemMessage( hDlg, IDD_PARITYCB, CB_GETCURSEL, - NULL, NULL ) ) ; + 0, 0L ) ) ; PARITY( npTTYInfo ) = LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_PARITYCB, CB_GETITEMDATA, (WPARAM) wSelection, - NULL ) ) ) ; + 0L ) ) ) ; // get stop bits selection wSelection = LOWORD( SendDlgItemMessage( hDlg, IDD_STOPBITSCB, CB_GETCURSEL, - NULL, NULL ) ) ; + 0, 0L ) ) ; STOPBITS( npTTYInfo ) = LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_STOPBITSCB, CB_GETITEMDATA, - (WPARAM) wSelection, NULL ) ) ) ; + (WPARAM) wSelection, 0L ) ) ) ; // get flow control settings @@ -2315,8 +2336,7 @@ BOOL FAR PASCAL SettingsDlgProc( HWND hD DWORD FAR PASCAL CommWatchProc( LPSTR lpData ) { - DWORD cbTransfer ; - DWORD dwEvtMask ; + DWORD dwTransfer, dwEvtMask ; NPTTYINFO npTTYInfo = (NPTTYINFO) lpData ; OVERLAPPED os ; @@ -2340,14 +2360,14 @@ DWORD FAR PASCAL CommWatchProc( LPSTR lp while ( CONNECTED( npTTYInfo ) ) { - dwEvtMask = NULL ; + dwEvtMask = 0 ; if (!WaitCommEvent( COMDEV( npTTYInfo ), &dwEvtMask, &os )) { if (ERROR_IO_PENDING == GetLastError()) { - GetOverlappedResult( COMDEV( npTTYInfo ), &os, &cbTransfer, TRUE ) ; - os.Offset += cbTransfer ; + GetOverlappedResult( COMDEV( npTTYInfo ), &os, &dwTransfer, TRUE ) ; + os.Offset += dwTransfer ; } } @@ -2375,7 +2395,7 @@ DWORD FAR PASCAL CommWatchProc( LPSTR lp // clear information in structure (kind of a "we're done flag") - THREADID( npTTYInfo ) = NULL ; + THREADID( npTTYInfo ) = 0 ; HTHREAD( npTTYInfo ) = NULL ; return( TRUE ) ;