SETTINGS.C 22.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
/*-----------------------------------------------------------------------------
    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 - 创建设置对话框
	ChangeConnection    - 根据连接状态修改菜单和控件
	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 );

/*
    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","460800","921600"
	};

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,460800,921600
	} ;

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_TRANSFER_RECEIVEFILETEXT, MF_ENABLED | MF_BYCOMMAND ) ;

	SetFocus(ghWndTTY);
    }
    else {
	//
	// Not connected, do opposite of above.
	//
	hMenu = GetMenu( hwnd ) ;
	EnableMenuItem( hMenu, ID_TRANSFER_RECEIVEFILETEXT,MF_DISABLED | MF_GRAYED | MF_BYCOMMAND ) ;
	EnableWindow( GetDlgItem(ghWndToolbarDlg, IDC_PORTCOMBO), TRUE);
	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()
{
    GetDlgItemText(ghWndToolbarDlg, IDC_PORTCOMBO, gszPort, sizeof(gszPort));
    if (g_appdata.fConnected)      // if connected, then update port state
	UpdateConnection();
    return;
}

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

FUNCTION: UpdateConnection( void )

PURPOSE: 根据用户的设置设置端口状态

COMMENTS: 设置DCB结构并调用SetCommState。通过调用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(g_appdata.hCommPort, &dcb))
	ErrorReporter("GetCommState");

    //
    // update DCB rate, byte size, parity, and stop bits size
    //
    dcb.BaudRate = g_appdata.dwBaudRate;
    dcb.ByteSize = g_appdata.bByteSize;
    dcb.Parity   = g_appdata.bParity;
    dcb.StopBits = g_appdata.bStopBits;

    //
    // update event flags
    //
    if (g_appdata.dwEventFlags & EV_RXFLAG)
	dcb.EvtChar = g_appdata.chFlag;      
    else
	dcb.EvtChar = '\0';

    //
    // update flow control settings
    //
    dcb.fDtrControl     = g_appdata.fDtrControl;
    dcb.fRtsControl     = g_appdata.fRtsControl;

    dcb.fOutxCtsFlow    = g_appdata.fCTSOutFlow;
    dcb.fOutxDsrFlow    = g_appdata.fDSROutFlow;
    dcb.fDsrSensitivity = g_appdata.fDSRInFlow;
    dcb.fOutX           = g_appdata.fXonXoffOutFlow;
    dcb.fInX            = g_appdata.fXonXoffInFlow;
    dcb.fTXContinueOnXoff = g_appdata.fTXafterXoffSent;
    dcb.XonChar         = g_appdata.chXON;
    dcb.XoffChar        = g_appdata.chXOFF;
    dcb.XonLim          = g_appdata.wXONLimit;
    dcb.XoffLim         = g_appdata.wXOFFLimit;

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

    //
    // set new state
    //
    if (!SetCommState(g_appdata.hCommPort, &dcb))
	ErrorReporter("SetCommState");

    //
    // set new timeouts
    //
    if (!SetCommTimeouts(g_appdata.hCommPort, &(g_appdata.timeoutsnew)))
	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 )
{
    char szBuffer[ MAXLEN_TEMPSTR ], szTemp[ MAXLEN_TEMPSTR ] ;
    WORD wCount, wMaxCOM, wPosition ;

    wMaxCOM = MAXPORTS ;
    strcpy(szTemp, "COM");

    //
    // fill port combo box and make initial selection
    //
    for (wCount = 0; wCount < 255; wCount++) 
	{
		if (g_appdata.unOpenedPortAry[wCount] == 0)
			continue;;
		wsprintf( szBuffer, "%s%d", (LPSTR) szTemp, g_appdata.unOpenedPortAry[wCount] ) ;
	SendDlgItemMessage( hDlg, IDC_PORTCOMBO, CB_ADDSTRING, 0,
			    (LPARAM) (LPSTR) szBuffer ) ;
    }

    SendDlgItemMessage( hDlg, IDC_PORTCOMBO, CB_SETCURSEL,
		       (WPARAM) (g_appdata.bPort - 1), 0L ) ;
   
    GetDlgItemText(hDlg, IDC_PORTCOMBO, gszPort, sizeof(gszPort));


    //
    // fill data bits combo box and make initial selection
    //
    for (wCount = 5; wCount < 9; wCount++) 
	{
	//wsprintf( szBuffer, "%d", wCount ) ;
	//wPosition = LOWORD( SendDlgItemMessage( hDlg, IDC_DATABITSCOMBO,CB_ADDSTRING, 0,(LPARAM) (LPSTR) szBuffer ) ) ;
	// if wCount is current selection, tell the combo box
	//if (wCount == g_appdata.bByteSize)SendDlgItemMessage( hDlg, IDC_DATABITSCOMBO, CB_SETCURSEL,(WPARAM) wPosition, 0L ) ;
    }

    // fill parity combo box and make initial selection
    //FillComboBox(   GetDlgItem( hDlg, IDC_PARITYCOMBO ),szParity, ParityTable,sizeof( ParityTable ) / sizeof( ParityTable[ 0 ] ),g_appdata.bParity ) ;
    // fill stop bits combo box and make initial selection
    //FillComboBox(   GetDlgItem( hDlg, IDC_STOPBITSCOMBO ),szStopBits, StopBitsTable,sizeof( StopBitsTable ) / sizeof ( StopBitsTable[ 0 ] ),g_appdata.bStopBits ) ;
 
    return ( TRUE ) ;

} // end of SettingsDlgInit()


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

FUNCTION: GetdwTTYItem(HWND, int, char **, DWORD *, int)

PURPOSE: Returns a DWORD item from a dialog control

PARAMETERS:
    hDlg      - Dialog window handle
    idControl - id of control to get data from
    szString  - table of strings that the control displays
    pTable    - table of data associated with strings
    iNumItems - size of table

RETURN:  
    DWORD item corresponding to control selection
    0 if item not found correctly

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

-----------------------------------------------------------------------------*/
DWORD GetdwTTYItem(HWND hDlg, int idControl, char ** szString, DWORD * pTable, int iNumItems)
{
    int i;
    char szItem[MAXLEN_TEMPSTR];

    //
    // Get current selection (a string)
    //
    GetDlgItemText(hDlg, idControl, szItem, sizeof(szItem));
    
    /*
	Compare current selection with table to find index of item.
	If index is found, then return the DWORD item from table.
    */ 
    for (i = 0; i < iNumItems; i++) {
	if (strcmp(szString[i], szItem) == 0)
	    return pTable[i];
    }

    return 0;
}

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

FUNCTION: GetbTTYItem(HWND, int, char **, DWORD *, int)

PURPOSE: Returns a BYTE item from a dialog control

PARAMETERS:
    hDlg      - Dialog window handle
    idControl - id of control to get data from
    szString  - table of strings that the control displays
    pTable    - table of data associated with strings
    iNumItems - size of table

RETURN:
    BYTE item from corresponding to control selection 
    0 if item data not found

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

-----------------------------------------------------------------------------*/
BYTE GetbTTYItem(HWND hDlg, int idControl, char ** szString, DWORD * pTable, int iNumItems)
{
    int i;
    char szItem[MAXLEN_TEMPSTR];

    //
    // Get current selection (a string)
    //
    GetDlgItemText(hDlg, idControl, szItem, sizeof(szItem));
    
    /*
	Compare current selection with table to find index of item.
	If index is found, then return the BYTE item from table.
    */ 
    for (i = 0; i < iNumItems; i++) {
	if (strcmp(szString[i], szItem) == 0)
	    return (BYTE) pTable[i];
    }
    
    return 0;
}


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

FUNCTION: ToolbarProc(HWND, UINT, WPARAM, LPARAM)

PURPOSE: Dialog Procedure for Settings Dialog

PARAMETERS:
    hWndDlg - Dialog window handle
    uMsg    - Window message
    wParam  - message parameter (depends on message)
    lParam  - message parameter (depends on message)

RETURN:
    TRUE if message is handled
    FALSE if message is not handled
    Exception is WM_INITDIALOG: returns FALSE since focus is not set

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

-----------------------------------------------------------------------------*/
BOOL CALLBACK ToolbarProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    BOOL fRet = FALSE;
	static char szFileName[MAX_PATH] = {0};

    switch(uMsg)
    {
	case WM_INITDIALOG:     // setup dialog with defaults
	    SettingsDlgInit(hWndDlg);
	    break;

	case WM_COMMAND: 
	    {
		switch(LOWORD(wParam))
		{
		case IDC_BUTTON_CLEANINFO:
            ClearTTYContents();
            InvalidateRect(ghWndTTY, NULL, TRUE);
			break;
		case IDC_BUTTON2_SEC_BINFILE:
			{
				DWORD dwFileSize,dwRead;
				BOOL loadBinFile = FALSE;

				if (strlen(g_appdata.szFileName) == 0)
				{
					char * szFilter = "Text Files\0*.*\0";
					OPENFILENAME ofn = {0};
					ofn.lStructSize = sizeof(OPENFILENAME);
					ofn.hwndOwner = hWndDlg;
					ofn.lpstrFilter = szFilter;
					ofn.lpstrFile = szFileName;
					ofn.nMaxFile = MAX_PATH;
					ofn.lpstrTitle = "Send File";
					ofn.Flags = OFN_FILEMUSTEXIST;

					if (!GetOpenFileName(&ofn))
						break;
					strcpy(g_appdata.szFileName,szFileName);
					g_appdata.hFileOfEarphoneBin = OpenTheFile(g_appdata.szFileName);
					if (g_appdata.hFileOfEarphoneBin == INVALID_HANDLE_VALUE)
					{
						memset(g_appdata.szFileName,0,MAX_PATH);
						return;
					}
					// set up transfer metrics
					if (!GetTransferSizes(g_appdata.hFileOfEarphoneBin))
					{
						memset(g_appdata.szFileName,0,MAX_PATH);
						return;
					}
					memset(g_appdata.BufEarphoneBin, 0, 3*1024*1024);
					ReadFile(g_appdata.hFileOfEarphoneBin , g_appdata.BufEarphoneBin, g_appdata.BinSizeFileEarphone, &dwRead, NULL);
					if (dwRead != g_appdata.BinSizeFileEarphone)
					{
						memset(g_appdata.szFileName,0,MAX_PATH);
						CloseHandle(g_appdata.hFileOfEarphoneBin);
						ErrorReporter("file err!\r\n");
						return;
					}
					CloseHandle(g_appdata.hFileOfEarphoneBin);
					SetWindowText(GetDlgItem(hWndDlg, IDC_BUTTON2_SEC_BINFILE), "下载软件");
				}
				if (g_appdata.DownloadReday == FALSE)
				{
					MessageBox(ghwndMain, "请合上夹具", "提示!", MB_OK);
					return FALSE;
				}

			}
			break;
			case IDC_BUTTON_CONNECT_UART:       // font button pressed
				{
					if (!g_appdata.fConnected)
					{
						if (SetupCommPort() != NULL)
						{
							ChangeConnection(hWndDlg, g_appdata.fConnected);
							SetWindowText(GetDlgItem(hWndDlg, IDC_BUTTON_CONNECT_UART), "断开夹具");
						}
					}
					else
					{
						if (BreakDownCommPort())
						{
							ChangeConnection(hWndDlg, g_appdata.fConnected);
							SetWindowText(GetDlgItem(hWndDlg, IDC_BUTTON_CONNECT_UART), "连接夹具");
						}

					}
				}
				break;

		    case IDC_FONTBTN:       // font button pressed
			{
			    CHOOSEFONT cf = {0};
			    LOGFONT lf;

			    lf = g_appdata.lfTTYFont;
			    cf.lStructSize = sizeof(CHOOSEFONT);
			    cf.hwndOwner = hWndDlg;
			    cf.lpLogFont = &lf;
			    cf.rgbColors = g_appdata.rgbFGColor;
			    cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_FIXEDPITCHONLY | \
				       CF_EFFECTS;

			    if (!ChooseFont(&cf))
				break;

			    InitNewFont(lf, cf.rgbColors);

			    //
			    // fix scroll bar sizes since we may have more or less pixels per
			    // character now
			    //
			    SizeTTY(ghWndTTY, (WORD)g_appdata.xSize, (WORD)g_appdata.ySize);
			    
			    //
			    // repaint screen contents
			    //
			    InvalidateRect(ghWndTTY, NULL, TRUE);
			    
			    //
			    // kill old cursor
			    //
			    KillTTYFocus(ghWndTTY);
			    
			    //
			    // create new cursor
			    //
			    SetTTYFocus(ghWndTTY);
			}
			fRet = FALSE;
			break;

		    default:                    // some other control has been modified
			if (g_appdata.fConnected)
			    UpdateTTYInfo();
			break;
		}
	    }
	    break;

	default:
	    break;
    }    

    return fRet;
}

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

FUNCTION: InitHexControl(HWND, WORD, WORD, char)

PURPOSE: Places byte value into two edit boxes of the dialog

PARAMETERS:
    hdlg         - Dialog Handle
    wIdNumberBox - Edit control ID ; displays hex
    wIdCharBox   - Edit control ID ; displays char
    chData       - data to display

COMMENTS: Some dialogs may have an edit control designed to accept
	  hexidecimal input from the user.  This function initializes
	  such edit controls.  First, the byte (char) is placed into a
	  zero terminated string.  This is set as the item text of one
	  of the controls.  Next, the byte is converted to a hexidecimal
	  string.  This is set as the item text of the other control.

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

-----------------------------------------------------------------------------*/
void InitHexControl(HWND hdlg, WORD wIdNumberBox, WORD wIdCharBox, char chData)
{
    char szFlagText[3] = {0};
    char szFlagChar[2] = {0};
    
    //
    // put character into char edit display control
    //
    szFlagChar[0] = chData;
    SetDlgItemText(hdlg, wIdCharBox, szFlagChar);
    
    //
    // put flag character into hex numeric edit control
    //
    wsprintf(szFlagText, "%02x", 0x000000FF & chData);
    SetDlgItemText(hdlg, wIdNumberBox, szFlagText);

    return;
}

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

FUNCTION: GetHexControl(HWND, WORD, WORD)

PURPOSE: Get hex data from control and convert to character

PARAMETERS:
    hdlg         - Dialog Handle
    wIdNumberBox - Edit control ID ; contains hex string
    wIdCharBox   - Edit control ID ; displays the char

RETURN:
    0 if can't get hex string from edit control
    byte value of hex string otherwise

COMMENTS: Function does the following:
	  1) Gets first two characters from edit control
	  2) Converts hex string to numeric value
	  3) Displays ascii char of numeric value
	  4) Returns numeric value

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

-----------------------------------------------------------------------------*/
char GetHexControl(HWND hdlg, WORD wIdNumberBox, WORD wIdCharBox)
{
    UINT uFlagValue;
    char chFlagEntered[3] = {0};
    char chFlag[2] = {0};
    
    //
    // get numeric value from control
    //
    if (0 == GetDlgItemText(hdlg, wIdNumberBox, chFlagEntered, 3))
	return 0;

    sscanf(chFlagEntered, "%x", &uFlagValue);

    chFlag[0] = (char) uFlagValue;
    SetDlgItemText(hdlg, wIdCharBox, chFlag); // display character

    return chFlag[0];
}