⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 settings.c

📁 用于串口的测试调试
💻 C
📖 第 1 页 / 共 3 页
字号:
/*-----------------------------------------------------------------------------
    This is a part of the Microsoft Source Code Samples. 
    Copyright (C) 1995 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: Settings.c

    PURPOSE: Controls all dialog controls in the Settings Dialog as well
	     as the comm events dialog, flow control dialog, and timeouts
	     dialog.  The module also controls the tty settings based on
	     these dialogs and also control comm port settings using 
	     SetCommState and SetCommTimeouts.

    FUNCTIONS:
	OpenSettingsToolbar - Creates the Settings Dialog
	ChangeConnection    - Modifies menus & controls based on connection state
	UpdateTTYInfo       - Modifies TTY data from Settings Dialog and
			      if connected, updates open comm port settings
	FillComboBox        - Fills a combo box with strings
	SetComboBox         - Selects an entry from a combo box
	SettingDlgInit      - Initializes settings dialog
	GetdwTTYItem        - returns a DWORD value from a dialog control
	GetbTTYItem         - returns a BYTE value from a dialog control
	ToolbarProc         - Dialog procedure for Settings Dialog
	InitHexControl      - Places a byte value into edit controls of a dialog
	GetHexControl       - returns hex data from a control and converts to a char
	InitCommEventsDlg   - Initializes Comm Events Dialog
	SaveCommEventsDlg   - Saves comm events flag if changed
	CommEventsProc      - Dialog procedure for Comm Events Dialog
	SaveFlowControlDlg  - Saves flow control settings if changed
	InitFlowControlDlg  - Inititlizes Flow Control Dialog
	FlowDefault         - sets "hardware" or "software" flow control
	FlowControlProc     - Dialog procedure for Flow Control Dialog
	InitTimeoutsDlg     - Initializes Timeouts Dialog
	SaveTimeoutsDlg     - Saves comm timeouts from Timeouts Dialog
	TimeoutsProc        - Dialog procedure for Timeouts Dialog

-----------------------------------------------------------------------------*/

#include <windows.h>
#include <stdio.h>
#include "mttty.h"

#define MAXLEN_TEMPSTR  20

/*
    Prototypes for functions called only within this file
*/
void FillComboBox( HWND, char ** szString, DWORD *, WORD, DWORD );
BOOL SettingsDlgInit( HWND );
DWORD GetdwTTYItem( HWND, int, char **, DWORD *, int );
BYTE GetbTTYItem( HWND, int, char **, DWORD *, int);
BOOL CALLBACK CommEventsProc( HWND, UINT, WPARAM, LPARAM );
BOOL CALLBACK ToolbarProc( HWND, UINT, WPARAM, LPARAM );
void InitHexControl(HWND, WORD, WORD, char);
char GetHexControl(HWND, WORD, WORD);
void SaveCommEventsDlg( HWND );
void InitCommEventsDlg( HWND, DWORD );
BOOL CALLBACK FlowControlProc( HWND, UINT, WPARAM, LPARAM );
void InitFlowControlDlg( HWND );
void SaveFlowControlDlg( HWND );
void FlowDefault(HWND hdlg, WORD wId);
BOOL CALLBACK TimeoutsProc( HWND, UINT, WPARAM, LPARAM );
void InitTimeoutsDlg( HWND, COMMTIMEOUTS );
void SaveTimeoutsDlg( HWND );
BOOL CALLBACK GetADWORDProc( HWND, UINT, WPARAM, LPARAM );

/*
    GLOBALS for this file
    The string arrays are the items in the dialog list controls.
*/

DCB dcbTemp;

char * szBaud[] = {
	    "110", "300", "600", "1200", "2400", 
	    "4800", "9600", "14400", "19200",
	    "38400", "56000", "57600", "115200", 
	    "128000", "256000"
	};

DWORD   BaudTable[] =  {
	    CBR_110, CBR_300, CBR_600, CBR_1200, CBR_2400,
	    CBR_4800, CBR_9600, CBR_14400, CBR_19200, CBR_38400,
	    CBR_56000, CBR_57600, CBR_115200, CBR_128000, CBR_256000
	} ;

char * szParity[] =   {   "None", "Even", "Odd", "Mark", "Space" };

DWORD   ParityTable[] = {  NOPARITY, EVENPARITY, ODDPARITY, MARKPARITY, SPACEPARITY  } ;

char * szStopBits[] =  {  "1", "1.5", "2"  };

DWORD   StopBitsTable[] =  { ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS } ;

char * szDTRControlStrings[] = { "Enable", "Disable", "Handshake" };

DWORD   DTRControlTable[] = { DTR_CONTROL_ENABLE, DTR_CONTROL_DISABLE, DTR_CONTROL_HANDSHAKE };

char * szRTSControlStrings[] = { "Enable", "Disable", "Handshake", "Toggle" };

DWORD   RTSControlTable[] = {   RTS_CONTROL_ENABLE, RTS_CONTROL_DISABLE, 
				RTS_CONTROL_HANDSHAKE, RTS_CONTROL_TOGGLE };

DWORD   EventFlagsTable[] = {
	    EV_BREAK, EV_CTS, EV_DSR, EV_ERR, EV_RING,
	    EV_RLSD, EV_RXCHAR, EV_RXFLAG, EV_TXEMPTY
	};

/*-----------------------------------------------------------------------------

FUNCTION: OpenSettingsToolbar(HWND)

PURPOSE: Open Settings Dialog

PARAMETERS:
    hWnd - dialog owner window handle

HISTORY:   Date:      Author:     Comment:
	   10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void OpenSettingsToolbar(HWND hWnd)
{
    ghWndToolbarDlg = CreateDialog(ghInst, MAKEINTRESOURCE(IDD_TOOLBARSETTINGS), hWnd, ToolbarProc);

    if (ghWndToolbarDlg == NULL)
	ErrorReporter("CreateDialog (Toolbar Dialog)");

    return;
}

/*-----------------------------------------------------------------------------

FUNCTION: ChangeConnection(HWND, BOOL)

PURPOSE: Modifies connection appearance

PARAMETERS:
    hwnd       - menu owner windows
    fConnected - TRUE sets connection appearance to connected
		 FALSE sets appearance to not connected

HISTORY:   Date:      Author:     Comment:
	   10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void ChangeConnection( HWND hwnd, BOOL fConnected )
{
    HMENU hMenu;
    int i;

    if (fConnected) {
	/*
	    The port is connected.  Need to :
	    Disable connect menu and enable disconnect menu.
	    Enable file transfer menu
	    Disable comm port selection box
	    Disable no writing, no reading, no events, no status check boxes
	    Enable status check boxes
	    Set focus to the child tty window
	*/
	hMenu = GetMenu( hwnd ) ;
	EnableMenuItem( hMenu, ID_FILE_CONNECT,
		       MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;
	EnableMenuItem( hMenu, ID_FILE_DISCONNECT,
		       MF_ENABLED | MF_BYCOMMAND ) ;

	EnableMenuItem( hMenu, ID_TRANSFER_SENDFILETEXT,
		       MF_ENABLED | MF_BYCOMMAND ) ;
	EnableMenuItem( hMenu, ID_TRANSFER_RECEIVEFILETEXT,
		       MF_ENABLED | MF_BYCOMMAND ) ;
	EnableMenuItem( hMenu, ID_TRANSFER_SENDREPEATEDLY,
		       MF_ENABLED | MF_BYCOMMAND ) ;
	EnableMenuItem( hMenu, ID_TRANSFER_ABORTSENDING,
		       MF_DISABLED | MF_GRAYED | MF_BYCOMMAND );
	EnableMenuItem( hMenu, ID_TRANSFER_ABORTREPEATEDSENDING,
		       MF_DISABLED | MF_GRAYED | MF_BYCOMMAND );

	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_PORTCOMBO), FALSE);
	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_NOWRITINGCHK), FALSE);
	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_NOREADINGCHK), FALSE);
	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_NOEVENTSCHK),  FALSE);
	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_NOSTATUSCHK),  FALSE);

	for (i = IDC_STATCTS; i <= IDC_STATRLSD; i++)
	    EnableWindow( GetDlgItem(ghWndStatusDlg, i), TRUE );

	for (i = IDC_CTSHOLDCHK; i <= IDC_RXCHAREDIT; i++)
	    EnableWindow( GetDlgItem(ghWndStatusDlg, i), TRUE);
    
	SetFocus(ghWndTTY);
    }
    else {
	//
	// Not connected, do opposite of above.
	//
	hMenu = GetMenu( hwnd ) ;
	EnableMenuItem( hMenu, ID_FILE_CONNECT,
		       MF_ENABLED | MF_BYCOMMAND ) ;
	EnableMenuItem( hMenu, ID_FILE_DISCONNECT,
		       MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;

	EnableMenuItem( hMenu, ID_TRANSFER_SENDFILETEXT,
		       MF_DISABLED | MF_GRAYED | MF_BYCOMMAND ) ;
	EnableMenuItem( hMenu, ID_TRANSFER_RECEIVEFILETEXT,
		       MF_DISABLED | MF_GRAYED | MF_BYCOMMAND ) ;
	EnableMenuItem( hMenu, ID_TRANSFER_SENDREPEATEDLY,
		       MF_DISABLED | MF_GRAYED | MF_BYCOMMAND ) ;
	EnableMenuItem( hMenu, ID_TRANSFER_ABORTSENDING,
		       MF_DISABLED | MF_GRAYED | MF_BYCOMMAND ) ;
	EnableMenuItem( hMenu, ID_TRANSFER_ABORTREPEATEDSENDING,
		       MF_DISABLED | MF_GRAYED | MF_BYCOMMAND );

	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_PORTCOMBO), TRUE);
	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_NOWRITINGCHK), TRUE);
	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_NOREADINGCHK), TRUE);
	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_NOEVENTSCHK),  TRUE);
	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_NOSTATUSCHK),  TRUE);

	for (i = IDC_STATCTS; i <= IDC_STATRLSD; i++) {
	    CheckDlgButton(ghWndStatusDlg, i, 0);
	    EnableWindow( GetDlgItem(ghWndStatusDlg, i), FALSE );
	}

	for (i = IDC_CTSHOLDCHK; i <= IDC_RXCHAREDIT; i++) {
	    if (i != IDC_TXCHAREDIT && i != IDC_RXCHAREDIT)
		CheckDlgButton(ghWndStatusDlg, i, 0);
	    else
		SetDlgItemInt(ghWndStatusDlg, i, 0, FALSE);

	    EnableWindow( GetDlgItem(ghWndStatusDlg, i), FALSE);
	}

	SetFocus(ghwndMain);
    }

    return;
}


/*-----------------------------------------------------------------------------

FUNCTION: UpdateTTYInfo(void)

PURPOSE: Modifies TTY data based on the settings and calls UpdateConnection

COMMENTS: Modifies the data based on the dialog. If connected, 
	  calls UpdateConnection.

HISTORY:   Date:      Author:     Comment:
	   10/27/95   AllenD      Wrote it
	    1/ 9/96   AllenD      Split DCB settings to new function

-----------------------------------------------------------------------------*/
void UpdateTTYInfo()
{
    //
    // update globals from dialog settings
    //
    GetDlgItemText(ghWndToolbarDlg, IDC_PORTCOMBO, gszPort, sizeof(gszPort));

    BAUDRATE(TTYInfo) = GetdwTTYItem( ghWndToolbarDlg, 
					IDC_BAUDCOMBO, 
					szBaud, 
					BaudTable, 
					sizeof(BaudTable)/sizeof(BaudTable[0]));

    PARITY(TTYInfo) = GetbTTYItem( ghWndToolbarDlg, 
				     IDC_PARITYCOMBO, 
				     szParity, 
				     ParityTable, 
				     sizeof(ParityTable)/sizeof(ParityTable[0]));

    STOPBITS(TTYInfo) = GetbTTYItem( ghWndToolbarDlg, 
				       IDC_STOPBITSCOMBO, 
				       szStopBits, 
				       StopBitsTable, 
				       sizeof(StopBitsTable)/sizeof(StopBitsTable[0]));

    LOCALECHO(TTYInfo) = IsDlgButtonChecked(ghWndToolbarDlg, IDC_LOCALECHOCHK);
    BYTESIZE(TTYInfo) = GetDlgItemInt(ghWndToolbarDlg, IDC_DATABITSCOMBO, NULL, FALSE);
    NEWLINE(TTYInfo) = IsDlgButtonChecked(ghWndToolbarDlg, IDC_LFBTN);
    AUTOWRAP(TTYInfo) = IsDlgButtonChecked(ghWndToolbarDlg, IDC_AUTOWRAPCHK);
    DISPLAYERRORS(TTYInfo) = IsDlgButtonChecked(ghWndToolbarDlg, IDC_DISPLAYERRORSCHK);

    NOREADING(TTYInfo) = IsDlgButtonChecked(ghWndToolbarDlg, IDC_NOREADINGCHK);
    NOWRITING(TTYInfo) = IsDlgButtonChecked(ghWndToolbarDlg, IDC_NOWRITINGCHK);
    NOEVENTS(TTYInfo)  = IsDlgButtonChecked(ghWndToolbarDlg, IDC_NOEVENTSCHK);
    NOSTATUS(TTYInfo)  = IsDlgButtonChecked(ghWndToolbarDlg, IDC_NOSTATUSCHK);

    if (CONNECTED(TTYInfo))      // if connected, then update port state
	UpdateConnection();

    return;
}

/*-----------------------------------------------------------------------------

FUNCTION: UpdateConnection( void )

PURPOSE: Sets port state based on settings from the user

COMMENTS: Sets up DCB structure and calls SetCommState.
	  Sets up new timeouts by calling SetCommTimeouts.

HISTORY:   Date:      Author:     Comment:
	    1/ 9/96   AllenD      Wrote it

-----------------------------------------------------------------------------*/
BOOL UpdateConnection()
{
    DCB dcb = {0};
    
    dcb.DCBlength = sizeof(dcb);

    //
    // get current DCB settings
    //
    if (!GetCommState(COMDEV(TTYInfo), &dcb))
	ErrorReporter("GetCommState");

    //
    // update DCB rate, byte size, parity, and stop bits size
    //
    dcb.BaudRate = BAUDRATE(TTYInfo);
    dcb.ByteSize = BYTESIZE(TTYInfo);
    dcb.Parity   = PARITY(TTYInfo);
    dcb.StopBits = STOPBITS(TTYInfo);

    //
    // update event flags
    //
    if (EVENTFLAGS(TTYInfo) & EV_RXFLAG)
	dcb.EvtChar = FLAGCHAR(TTYInfo);      
    else
	dcb.EvtChar = '\0';

    //
    // update flow control settings
    //
    dcb.fDtrControl     = DTRCONTROL(TTYInfo);
    dcb.fRtsControl     = RTSCONTROL(TTYInfo);

    dcb.fOutxCtsFlow    = CTSOUTFLOW(TTYInfo);
    dcb.fOutxDsrFlow    = DSROUTFLOW(TTYInfo);
    dcb.fDsrSensitivity = DSRINFLOW(TTYInfo);
    dcb.fOutX           = XONXOFFOUTFLOW(TTYInfo);
    dcb.fInX            = XONXOFFINFLOW(TTYInfo);
    dcb.fTXContinueOnXoff = TXAFTERXOFFSENT(TTYInfo);
    dcb.XonChar         = XONCHAR(TTYInfo);
    dcb.XoffChar        = XOFFCHAR(TTYInfo);
    dcb.XonLim          = XONLIMIT(TTYInfo);
    dcb.XoffLim         = XOFFLIMIT(TTYInfo);

    //
    // DCB settings not in the user's control
    //
    dcb.fParity = TRUE;

    //
    // set new state
    //
    if (!SetCommState(COMDEV(TTYInfo), &dcb))
	ErrorReporter("SetCommState");

    //
    // set new timeouts
    //
    if (!SetCommTimeouts(COMDEV(TTYInfo), &(TIMEOUTSNEW(TTYInfo))))
	ErrorReporter("SetCommTimeouts");

    return TRUE;
}


/*-----------------------------------------------------------------------------

FUNCTION: FillComboBox(HWND, char **, DWORD *, WORD, DWORD)

PURPOSE: Populates dialog controls with proper strings

PARAMETERS:
    hCtrlWnd         - window handle of control being filled
    szString         - string table contains strings to fill control with
    npTable          - table of values corresponding to strings
    wTableLen        - length of the string table
    dwCurrentSetting - initialz combo box selection

COMMENTS: This function originally found in the Win32 COMM sample
	  Written by BryanW.  Modified for Win32 MTTTY Sample.

HISTORY:   Date:      Author:     Comment:
	   10/27/95   AllenD      Modified for MTTTY

-----------------------------------------------------------------------------*/
void FillComboBox( HWND hCtrlWnd, char ** szString,
			DWORD * npTable, WORD wTableLen, DWORD dwCurrentSetting )
{
    WORD wCount, wPosition ;

    for (wCount = 0; wCount < wTableLen; wCount++) {
	wPosition = LOWORD( SendMessage( hCtrlWnd, CB_ADDSTRING, 0,
					(LPARAM) (LPSTR) szString[wCount] ) ) ;

	//
	// use item data to store the actual table value
	//
	SendMessage( hCtrlWnd, CB_SETITEMDATA, (WPARAM) wPosition,
		     (LPARAM) *(npTable + wCount) ) ;

	//     
	// if this is our current setting, select it
	//
	if (*(npTable + wCount) == dwCurrentSetting)
	    SendMessage( hCtrlWnd, CB_SETCURSEL, (WPARAM) wPosition, 0L ) ;
    }
    return ;
}

/*-----------------------------------------------------------------------------

FUNCTION: SetComboBox(HWND, WORD, DWORD)

PURPOSE: Selects an entry from a dialog combobox

PARAMETERS:
    hCtrlWnd     - windows handle of control
    wTableLen    - length of value table for this control
    dwNewSetting - new item to base selection on

HISTORY:   Date:      Author:     Comment:
	   11/20/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void SetComboBox( HWND hCtrlWnd, WORD wTableLen, DWORD dwNewSetting )
{
    WORD wCount, wItemData ;

    for (wCount = 0; wCount < wTableLen; wCount++) {
	wItemData = LOWORD( SendMessage( hCtrlWnd, CB_GETITEMDATA, (WPARAM) wCount, 0L ) );

	if (wItemData == dwNewSetting) {
	    SendMessage( hCtrlWnd, CB_SETCURSEL, (WPARAM) wCount, 0L ) ;
	break;
	}
    }
    return ;
}

/*-----------------------------------------------------------------------------

FUNCTION: SettingsDlgInit(HWND)

PURPOSE: Initializes Settings Dialog

PARAMETERS:
    hDlg - Dialog window handle

RETURN: always TRUE

COMMENTS: This function originally found in the Win32 COMM sample
	  Written by BryanW.  Modified for Win32 MTTTY Sample.

HISTORY:   Date:      Author:     Comment:
	   10/27/95   AllenD      Modified for MTTTY

-----------------------------------------------------------------------------*/
BOOL SettingsDlgInit( HWND hDlg )
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -