--- mstools/samples/comm/tty.c 2018/08/09 18:22:16 1.1.1.2 +++ 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 ) ) ; @@ -1501,7 +1519,9 @@ int NEAR ReadCommBlock( HWND hWnd, LPSTR wsprintf( szError, "", dwErrorFlags ) ; WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ; } + dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ; + if (dwLength > 0) { fReadStat = ReadFile( COMDEV( npTTYInfo ), lpszBlock, @@ -1528,6 +1548,7 @@ int NEAR ReadCommBlock( HWND hWnd, LPSTR dwLength = 0 ; } } + return ( dwLength ) ; #else nLength = ReadComm( COMDEV( npTTYInfo ), lpszBlock, nMaxLength ) ; @@ -1918,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 @@ -1929,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() @@ -1967,7 +1988,7 @@ BOOL NEAR SettingsDlgInit( HWND hDlg ) return ( FALSE ) ; #ifdef WIN32 - wMaxCOM = 4 ; + wMaxCOM = MAXPORTS ; #else wMaxCOM = LOWORD( EscapeCommFunction( NULL, GETMAXCOM ) ) + 1 ; #endif @@ -1981,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) @@ -2005,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 @@ -2097,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 ) ; @@ -2144,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 @@ -2165,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 @@ -2334,7 +2360,7 @@ DWORD FAR PASCAL CommWatchProc( LPSTR lp while ( CONNECTED( npTTYInfo ) ) { - dwEvtMask = NULL ; + dwEvtMask = 0 ; if (!WaitCommEvent( COMDEV( npTTYInfo ), &dwEvtMask, &os )) { @@ -2369,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 ) ;