Commit 639bea37c5c015f961f77d2683a83882a8ad6f6d

Authored by xiemeng
1 parent 725f6c31

更新代码注释

@@ -23,7 +23,9 @@ @@ -23,7 +23,9 @@
23 SetupCommPort - 第一次打开港口 23 SetupCommPort - 第一次打开港口
24 WaitForThreads - 设置线程退出事件并等待工作线程退出 24 WaitForThreads - 设置线程退出事件并等待工作线程退出
25 BreakDownCommPort - 关闭与comm端口的连接 25 BreakDownCommPort - 关闭与comm端口的连接
26 - DisconnectOK - 询问用户是否可以断开连接 26 + DisconnectOK - 询问用户是否可以断开连接
  27 + QuerySingleSerialPortEx(int nPort) - 查询指定端口状态
  28 + QuerySerialPortStatusEx - 查询指定范围端口状态
27 29
28 -----------------------------------------------------------------------------*/ 30 -----------------------------------------------------------------------------*/
29 31
@@ -182,6 +184,7 @@ void InitNewFont(LOGFONT LogFont, COLORREF rgbColor) @@ -182,6 +184,7 @@ void InitNewFont(LOGFONT LogFont, COLORREF rgbColor)
182 TTYInfo.lfTTYFont = LogFont; 184 TTYInfo.lfTTYFont = LogFont;
183 TTYInfo.hTTYFont = CreateFontIndirect(&(TTYInfo.lfTTYFont)); 185 TTYInfo.hTTYFont = CreateFontIndirect(&(TTYInfo.lfTTYFont));
184 TTYInfo.rgbFGColor = rgbColor; 186 TTYInfo.rgbFGColor = rgbColor;
  187 + TTYInfo.rgbBGColor = RGB(255,255,255);
185 188
186 hDC = GetDC( ghwndMain ) ; 189 hDC = GetDC( ghwndMain ) ;
187 SelectObject( hDC, TTYInfo.hTTYFont ) ; 190 SelectObject( hDC, TTYInfo.hTTYFont ) ;
@@ -213,9 +216,7 @@ HISTORY: Date: Author: Comment: @@ -213,9 +216,7 @@ HISTORY: Date: Author: Comment:
213 -----------------------------------------------------------------------------*/ 216 -----------------------------------------------------------------------------*/
214 BOOL InitTTYInfo() 217 BOOL InitTTYInfo()
215 { 218 {
216 - //  
217 - // initialize generial TTY info  
218 - // 219 +
219 TTYInfo.hCommPort = NULL ; 220 TTYInfo.hCommPort = NULL ;
220 TTYInfo.fConnected = FALSE ; 221 TTYInfo.fConnected = FALSE ;
221 TTYInfo.fLocalEcho = FALSE ; 222 TTYInfo.fLocalEcho = FALSE ;
@@ -236,7 +237,7 @@ BOOL InitTTYInfo() @@ -236,7 +237,7 @@ BOOL InitTTYInfo()
236 TTYInfo.fDisplayErrors = TRUE ; 237 TTYInfo.fDisplayErrors = TRUE ;
237 238
238 // 239 //
239 - // timeouts 240 + // 超时
240 // 241 //
241 TTYInfo.timeoutsnew = gTimeoutsDefault; 242 TTYInfo.timeoutsnew = gTimeoutsDefault;
242 243
@@ -380,29 +381,27 @@ HANDLE SetupCommPort() @@ -380,29 +381,27 @@ HANDLE SetupCommPort()
380 ErrorReporter("GetCommTimeouts"); 381 ErrorReporter("GetCommTimeouts");
381 382
382 // 383 //
383 - // Set port state 384 + // 重连COM口连接
384 // 385 //
385 UpdateConnection(); 386 UpdateConnection();
386 387
387 // 388 //
388 - // set comm buffer sizes 389 + // 设置通信缓冲区大小
389 // 390 //
390 SetupComm(TTYInfo.hCommPort, MAX_READ_BUFFER, MAX_WRITE_BUFFER); 391 SetupComm(TTYInfo.hCommPort, MAX_READ_BUFFER, MAX_WRITE_BUFFER);
391 392
392 // 393 //
393 - // raise DTR  
394 - // 394 + // 将DT&线路升成高电位。
  395 + // 参考后面注释
395 if (!EscapeCommFunction(TTYInfo.hCommPort, SETDTR)) 396 if (!EscapeCommFunction(TTYInfo.hCommPort, SETDTR))
396 ErrorReporter("EscapeCommFunction (SETDTR)"); 397 ErrorReporter("EscapeCommFunction (SETDTR)");
397 398
398 // 399 //
399 - // start threads and set initial thread state to not done 400 + // 启动线程并将初始线程状态设置为not done
400 // 401 //
401 StartThreads(); 402 StartThreads();
402 403
403 - //  
404 - // set overall connect flag  
405 - // 404 + //设置连接标志
406 TTYInfo.fConnected = TRUE ; 405 TTYInfo.fConnected = TRUE ;
407 406
408 return TTYInfo.hCommPort; 407 return TTYInfo.hCommPort;
@@ -412,15 +411,15 @@ HANDLE SetupCommPort() @@ -412,15 +411,15 @@ HANDLE SetupCommPort()
412 411
413 FUNCTION: WaitForThreads(DWORD) 412 FUNCTION: WaitForThreads(DWORD)
414 413
415 -PURPOSE: Waits a specified time for the worker threads to exit 414 +PURPOSE: 等待指定时间让工作线程退出
416 415
417 PARAMETERS: 416 PARAMETERS:
418 - dwTimeout - milliseconds to wait until timeout 417 + dwTimeout - 等待超时的毫秒数
419 418
420 RETURN: 419 RETURN:
421 - WAIT_OBJECT_0 - successful wait, threads are not running  
422 - WAIT_TIMEOUT - at least one thread is still running  
423 - WAIT_FAILED - failure in WaitForMultipleObjects 420 + WAIT_OBJECT_0 - 成功等待,线程没有运行
  421 + WAIT_TIMEOUT - 至少有一个线程仍在运行
  422 + WAIT_FAILED - failure in WaitForMultipleObjects 参考文档末尾注释
424 423
425 HISTORY: Date: Author: Comment: 424 HISTORY: Date: Author: Comment:
426 10/27/95 AllenD Wrote it 425 10/27/95 AllenD Wrote it
@@ -435,10 +434,9 @@ DWORD WaitForThreads(DWORD dwTimeout) @@ -435,10 +434,9 @@ DWORD WaitForThreads(DWORD dwTimeout)
435 hThreads[1] = TTYInfo.hWriter; 434 hThreads[1] = TTYInfo.hWriter;
436 435
437 // 436 //
438 - // set thread exit event here  
439 - // 437 + // 设置线程终止句柄
  438 + // 两个线程 读线程和写线程
440 SetEvent(ghThreadExitEvent); 439 SetEvent(ghThreadExitEvent);
441 -  
442 dwRes = WaitForMultipleObjects(2, hThreads, TRUE, dwTimeout); 440 dwRes = WaitForMultipleObjects(2, hThreads, TRUE, dwTimeout);
443 switch(dwRes) 441 switch(dwRes)
444 { 442 {
@@ -448,13 +446,12 @@ DWORD WaitForThreads(DWORD dwTimeout) @@ -448,13 +446,12 @@ DWORD WaitForThreads(DWORD dwTimeout)
448 break; 446 break;
449 447
450 case WAIT_TIMEOUT: 448 case WAIT_TIMEOUT:
451 - 449 + //读线程
452 if (WaitForSingleObject(TTYInfo.hReaderStatus, 0) == WAIT_TIMEOUT) 450 if (WaitForSingleObject(TTYInfo.hReaderStatus, 0) == WAIT_TIMEOUT)
453 OutputDebugString("Reader/Status Thread didn't exit.\n\r"); 451 OutputDebugString("Reader/Status Thread didn't exit.\n\r");
454 - 452 + //写线程
455 if (WaitForSingleObject(TTYInfo.hWriter, 0) == WAIT_TIMEOUT) 453 if (WaitForSingleObject(TTYInfo.hWriter, 0) == WAIT_TIMEOUT)
456 OutputDebugString("Writer Thread didn't exit.\n\r"); 454 OutputDebugString("Writer Thread didn't exit.\n\r");
457 -  
458 break; 455 break;
459 456
460 default: 457 default:
@@ -474,15 +471,13 @@ DWORD WaitForThreads(DWORD dwTimeout) @@ -474,15 +471,13 @@ DWORD WaitForThreads(DWORD dwTimeout)
474 471
475 FUNCTION: BreakDownCommPort 472 FUNCTION: BreakDownCommPort
476 473
477 -PURPOSE: Closes a connection to a comm port 474 +PURPOSE: 关闭对comm端口的连接
478 475
479 RETURN: 476 RETURN:
480 - TRUE - successful breakdown of port 477 + TRUE - 成功关闭港口
481 FALSE - port isn't connected 478 FALSE - port isn't connected
482 479
483 -COMMENTS: Waits for threads to exit,  
484 - clears DTR, restores comm port timeouts, purges any i/o  
485 - and closes all pertinent handles 480 +COMMENTS: 等待线程退出,清除DTR,恢复通信端口超时,清除任何i/o并关闭所有相关句柄
486 481
487 HISTORY: Date: Author: Comment: 482 HISTORY: Date: Author: Comment:
488 10/27/95 AllenD Wrote it 483 10/27/95 AllenD Wrote it
@@ -496,34 +491,20 @@ BOOL BreakDownCommPort() @@ -496,34 +491,20 @@ BOOL BreakDownCommPort()
496 TTYInfo.fConnected = FALSE; 491 TTYInfo.fConnected = FALSE;
497 492
498 // 493 //
499 - // wait for the threads for a small period 494 + // 等待线程一小段时间
500 // 495 //
501 if (WaitForThreads(20000) != WAIT_OBJECT_0) 496 if (WaitForThreads(20000) != WAIT_OBJECT_0)
502 - /*  
503 - if threads haven't exited, then they will  
504 - interfere with a new connection. I must abort  
505 - the entire program.  
506 - */ 497 + //如果线程还没有退出,那么它们将退出 干扰一个新的连接。 必须中止 整个程序。
507 ErrorHandler("Error closing port."); 498 ErrorHandler("Error closing port.");
508 -  
509 - //  
510 - // lower DTR  
511 - // 499 + // 将DTR线路降成低电位。
512 if (!EscapeCommFunction(TTYInfo.hCommPort, CLRDTR)) 500 if (!EscapeCommFunction(TTYInfo.hCommPort, CLRDTR))
513 ErrorReporter("EscapeCommFunction(CLRDTR)"); 501 ErrorReporter("EscapeCommFunction(CLRDTR)");
514 -  
515 - //  
516 // restore original comm timeouts 502 // restore original comm timeouts
517 - //  
518 if (!SetCommTimeouts(TTYInfo.hCommPort, &(TTYInfo.timeoutsorig))) 503 if (!SetCommTimeouts(TTYInfo.hCommPort, &(TTYInfo.timeoutsorig)))
519 ErrorReporter("SetCommTimeouts (Restoration to original)"); 504 ErrorReporter("SetCommTimeouts (Restoration to original)");
520 -  
521 - //  
522 - // Purge reads/writes, input buffer and output buffer  
523 - // 505 + // 清除读取/写入,输入缓冲区和输出缓冲区
524 if (!PurgeComm(TTYInfo.hCommPort, PURGE_FLAGS)) 506 if (!PurgeComm(TTYInfo.hCommPort, PURGE_FLAGS))
525 ErrorReporter("PurgeComm"); 507 ErrorReporter("PurgeComm");
526 -  
527 CloseHandle(TTYInfo.hCommPort); 508 CloseHandle(TTYInfo.hCommPort);
528 CloseHandle(TTYInfo.hReaderStatus); 509 CloseHandle(TTYInfo.hReaderStatus);
529 CloseHandle(TTYInfo.hWriter); 510 CloseHandle(TTYInfo.hWriter);
@@ -535,7 +516,7 @@ BOOL BreakDownCommPort() @@ -535,7 +516,7 @@ BOOL BreakDownCommPort()
535 516
536 FUNCTION: DisconnectOK 517 FUNCTION: DisconnectOK
537 518
538 -PURPOSE: Asks user if it is OK to disconnect 519 +PURPOSE: 询问用户是否可以断开连接
539 520
540 RETURN: 521 RETURN:
541 TRUE - OK to disconnect 522 TRUE - OK to disconnect
@@ -547,12 +528,15 @@ HISTORY: Date: Author: Comment: @@ -547,12 +528,15 @@ HISTORY: Date: Author: Comment:
547 -----------------------------------------------------------------------------*/ 528 -----------------------------------------------------------------------------*/
548 BOOL DisconnectOK() 529 BOOL DisconnectOK()
549 { 530 {
550 - if (!TTYInfo.fConnected)  
551 - return TRUE;  
552 - 531 + if (!TTYInfo.fConnected) return TRUE;
553 return ((MessageBox(ghwndMain, "OK to Disconnect?", gszPort, MB_YESNO)) == IDYES); 532 return ((MessageBox(ghwndMain, "OK to Disconnect?", gszPort, MB_YESNO)) == IDYES);
554 } 533 }
555 534
  535 +/*-----------------------------------------------------------------------------
  536 +FUNCTION: QuerySingleSerialPortEx
  537 +
  538 +PURPOSE: 查询指定端口状态
  539 +-----------------------------------------------------------------------------*/
556 540
557 541
558 BOOL QuerySingleSerialPortEx(int nPort) 542 BOOL QuerySingleSerialPortEx(int nPort)
@@ -586,8 +570,15 @@ BOOL QuerySingleSerialPortEx(int nPort) @@ -586,8 +570,15 @@ BOOL QuerySingleSerialPortEx(int nPort)
586 return bValid; 570 return bValid;
587 } 571 }
588 572
589 -//利用API查询指定范围内的串口状态信息,并分别返回指定范围内系统存在的串口,  
590 -//当前未被占用的可用串口,已被占用的串口和系统不存在的串口 573 +
  574 +/*-----------------------------------------------------------------------------
  575 +FUNCTION: QuerySingleSerialPortEx
  576 +
  577 +PURPOSE: 查询指定范围端口状态
  578 +利用API查询指定范围内的串口状态信息,并分别返回指定范围内系统存在的串口,
  579 +当前未被占用的可用串口,已被占用的串口和系统不存在的串口
  580 +*/
  581 +
591 void QuerySerialPortStatusEx(int *AryNoneOpenedPort,int nStartPort,int nLastPort) 582 void QuerySerialPortStatusEx(int *AryNoneOpenedPort,int nStartPort,int nLastPort)
592 { 583 {
593 char strTmp[50],strComm[50],strFree[50]; 584 char strTmp[50],strComm[50],strFree[50];
@@ -626,3 +617,196 @@ void QuerySerialPortStatusEx(int *AryNoneOpenedPort,int nStartPort,int nLastPor @@ -626,3 +617,196 @@ void QuerySerialPortStatusEx(int *AryNoneOpenedPort,int nStartPort,int nLastPor
626 } 617 }
627 618
628 } 619 }
  620 +
  621 +
  622 +/*
  623 +代码说明
  624 +EscapeCommFunction()函数
  625 +指示指定的通信设备执行扩展功能。
  626 +BOOL EscapeCommFunction(
  627 + HANDLE hFile, //通信设备的句柄
  628 + DWORD dwFunc //指定执行的功能
  629 +);
  630 +hFile:串行端口的Handle值,此值即为使用createFile函数后所返回的值。
  631 +dwFunc:指定串行端口必须完成的工作,由以下的常数数值组成:
  632 +CLRDTR:将DTR线路降成低电位。
  633 +CLRRTS:将RTS线路降成低电位
  634 +SETDTR:将DT&线路升成高电位。
  635 +SETRTS:RTS线路升成高电位
  636 +SETXOFF:当接收到Xoff字符时启动传输操作。
  637 +SETXON:当接收到Xon字符时启动传蝓作。
  638 +SETBREAK:没置道信状态为中断(送出Break信号)。
  639 +CLRBREAK:清除 Break信号,使传输动作继续。
  640 +请特别注意到上述的参数,包有DTR、RTS的线路控制指令,如果要个别地控制这两条线路的高低电位状态就需要硬用这个函数。
  641 +
  642 +
  643 +
  644 +GetCommState()函数功能,使用指定通信设备的当前控制设置填充设备控制块(DCB结构)
  645 +BOOL GetCommState( HANDLE hFile, LPDCB lpDCB );
  646 +参数
  647 +hFile [in]由CreateFile函数返回的通信设备句柄。
  648 +lpDCB [out]指向返回控制设置数据的DCB结构的长指针。
  649 +typedef struct _DCB { //DCB结构声明
  650 + DWORD DCBlength; DCB结构的长度
  651 + DWORD BaudRate; 当前波特率
  652 + DWORD fBinary: 1; 二进制模式,无EOF检查
  653 + DWORD fParity: 1; 激活奇偶校验检查
  654 + DWORD fOutxCtsFlow:1; CTS输出流量控制
  655 + DWORD fOutxDsrFlow:1; DSR输出流量控制
  656 + DWORD fDtrControl:2; DTR流量控制类型
  657 + DWORD fDsrSensitivity:1; DSR安全设置
  658 + DWORD fTXContinueOnXoff: 1; XOFF持续Tx
  659 + DWORD fOutX: 1; XON/XOFF输出流量控制
  660 + DWORD fInX: 1; XON/XOFF输出流量控制
  661 + DWORD fErrorChar: 1; 激活错误替换机制
  662 + DWORD fNull: 1; 允许舍空格
  663 + DWORD fRtsControl:2; Rts流量控制
  664 + DWORD fAbortOnError:1; 有错误时放弃读/写
  665 + DWORD fDummy2:17; 保留
  666 + WORD wReserved; 现在不使用
  667 + WORD XonLim; 传送XON阀值
  668 + WORD XoffLim; 传送XOFF阀值
  669 + BYTE ByteSize; 每个字节的位数 4-8
  670 + BYTE Parity; 校验 0-4=None,Odd,Even,Mark,Space
  671 + BYTE StopBits; 0,1,2 = 1, 1.5, 2
  672 + char XonChar; 设置Tx和Rx的XON的字符
  673 + char XoffChar; 设置Tx和Rx的XOFF字符
  674 + char ErrorChar; 有错误时的替换字符
  675 + char EofChar; 表示输入结束的字符
  676 + char EvtChar; 接收事件字符
  677 + WORD wReserved1; 保留未使用
  678 +} DCB, *LPDCB;
  679 +
  680 +DCB结构中重要参数说明如下:
  681 +
  682 +Baudrate:串行端口的传输速度
  683 +fBinary:是否采用二进制方式发送数据,若设为1,表示采用二进制方式,占 一个位。由于win32只支持二进制的方式,故此值必须设置为1.
  684 +fParity:是否启动奇偶校验位检查,若设为1,表示采用奇偶校验位检查。此标志占一个位。
  685 +fOutxCtsFlow:是否采用CTS硬件流量控制。当此值设为1,而CTS引脚为底电位时,数据发送将暂停,直到CTS引脚升至高电位。占一个位。
  686 +fOutDsrFlow:是否采用DSR硬件流量控制。当此值设为1,而DSR引脚为低电位时,数据发送将暂停,直到DSR引脚升至高电位,占一个位。
  687 +fDtrControl:是否采用DTR硬件流量控制。可设为DTR_CONTROL_DISABLE(串行端口打开后设为低电位)、DTR_CONTROL_ENABLE( 串行端口打开后设为高电位)DTR_CONTROL_HANDSHAKE(启动硬件流量控制)三者之一,其常数定义数值分别是0、1、2。此标志占两个位。
  688 +fDsrSensitivity:当成True时,当DSR引脚被提升至高电位之前,串行端口将忽略任何被收到的数据。此标志占一个位的位置。
  689 +ftOutX:发送过程是否启用软件握手(XON_XOFF)。当设成1时,发送过程收到XOFF字符时停上发送,而收到XON字符时恢复发送。此标志占一个位的位置。
  690 +fInX:发送过程是否后用软件握手(XON-XOFF),当设成1时,接收过程若超过设置值(XoffLim参数决定),则送出XOFF字符:而低于设置值时(Xonlim参数决定)送出XON字符。此标志占一个位的位置。
  691 +fNuil:接收过程是否舍弃空格符。若设成1则于接收过程将自动去掉空格符。此标志占一个位的位置。
  692 +fRtsControl:是否启动RTS硬件流控制。可设为RTS_CONTROL_DISABLE(串行端凵打丌后设为低电位)、RTS_CONTROL_ENABLE(串行端口打开后设为高电位)、RTS_CONTROL_HANDSHAKE(启动硬件流量控制)、
  693 +RTSCONTROLTOGGLE(指示发过程的进行状态)四者之一,其常数是0、1、2、3。此标志占两个位的位置。
  694 +XonLim:设置发送XON字符时,输入缓冲区中的最小字节数
  695 +XoffLim:设置发送XOFF字符时,输入缓冲区中的最大字节数
  696 +ByteSize:设置数据位数。有5、6、7、8可以设置。
  697 +Parity:设置校验位检查的种类。有None、Even、Odd三种
  698 +StopBits:设置停止位数。有1、1.5、2三种
  699 +XonChar:设置XON字符
  700 +XoffChar:设置XOFF字符
  701 +
  702 +
  703 +SetCommState()函数
  704 +SetCommState()函数设置串行端口状态。如设置串口设置(波特率,校验,停止位,数据位等).
  705 +SetCommState()函数原型:
  706 +BOOL SetCommState( HANDLE hFile, LPDCB lpDCB );
  707 +hFile:串行端口的Handle值,此值即为使用CreateFile函数后返回的值。
  708 +lpDCB:设备控制块(Device Control Block, DCB)结构,将指定后的DCB结构传入。
  709 +
  710 +
  711 +WriteFile()函数
  712 +WriteFile函数,可以将数据写入一个文件或者I/O设备。该函数比fwrite函数要灵活的多,也可将这个函数应用于对通信设备、管道、套接字以及邮槽的处理。
  713 +windows将串行端口当成文件来使用,因此写入串行端口数据的函数也是WriteFile。
  714 +WriteFile()函数原型:
  715 +BOOL WriteFile( HANDLE hFile,//文件句柄
  716 + LPCVOID lpBuffer,//数据缓存区指针
  717 + DWORD nNumberOfBytesToWrite,//要写的字节数
  718 + LPDWORD lpNumberOfBytesWritten,//用于保存实际写入字节数的存储区域的指针
  719 + LPOVERLAPPED lpOverlapped//OVERLAPPED结构体指针
  720 +);
  721 +hFile:串行端口的Handle值,句柄
  722 +lpBuffer:指向欲发送的数据
  723 +nNumberOfBytesToWrite:写入的字节数
  724 +lpNumberOfBytesWritten:指向被写入的字节数的变量地址
  725 +lpOverlapped:指向overlapped I/O的结构地址,通常用来作背景工作时同步检查用,在串行通信中若不使用异步传输,则可不使用,设成NULL即可。
  726 +
  727 +
  728 +ReadFile()函数
  729 +从文件中读出数据。与fread函数相比,这个函数要明显灵活的多。该函数能够操作通信设备、管道、套接字以及邮槽。
  730 +windows将串行端口的使用当成文件,因此读取串行端口数据的函数亦使用读文件函数ReadFile.
  731 +ReadFile()函数原型:
  732 +BOOL ReadFile(
  733 + HANDLE hFile, //文件的句柄
  734 + LPVOID lpBuffer, //接收数据的缓冲区
  735 + DWORD nNumberOfBytesToRead, //读取的字节数
  736 + LPDWORD lpNumberOfBytesRead, //指向实际读取字节数的指针
  737 + LPOVERLAPPED lpOverlapped
  738 + //如文件打开时指定了FILE_FLAG_OVERLAPPED,那么必须,用这个参数引用一个特殊的结构。
  739 + //该结构定义了一次异步读取操作。否则,应将这个参数设为NULL
  740 +);
  741 +ReadFile()函数参数说明:
  742 +hFile:串行端口的Handle值,
  743 +lpBuffer:指向存储被读取数据的地址
  744 +nNumberOfBytesToRead:准备读取的数据字节数
  745 +lpNumberOfBytesRead:准备读取字节数的指针
  746 +lpOverlapped:指向overlapped I/O的结构地址,在串行通信中若不同时进行后台工作,则可不使用,设成NULL即可。
  747 +
  748 +
  749 +ClearCommError()函数
  750 +清除串行端口错误或读取串行端口现在的状态时,可用函数ClearCommError。Windows系统利用此函数清除硬件的通讯错误以及获取通讯设备的当前状态
  751 +ClearCommError()函数原型
  752 +BOOL ClearCommError( HANDLE hFile, //通信设备的句柄
  753 + LPDWORD lpErrors,//接收错误代码变量的指针
  754 + LPCOMSTAT lpStat //通信状态缓冲区的指针
  755 +);
  756 +ClearCommError()函数参数说明:
  757 +hFile:串行端冂的Handle值,此值即为使用CreateFile函数后所返回的值。
  758 +lpError:返回错误数值,错误常数如下:
  759 +CE_BREAK:检测到中断信号。
  760 +CE_DNS:Windows95专用,未被选择的并行端口。
  761 +CE_FRAME:硬件检到框架错误
  762 +CE_IOE:通信设备发生输入/输出綹误,
  763 +CE_MODE:设置模式错误,或是hFile值错误。
  764 +CE_OOP:Wmdows95专用,并行端口发生缺纸错误。CE_OVERRUN:缓冲区容量不足,数据将遗失。
  765 +CE_PTO:Windows95专用,并行端口发生超时错误。
  766 +CE_RXOVER:接收区满溢或在文件结尾被接收到后仍有数据发送过来。
  767 +CE_RXPARITY:硬件检测到校验位检查错误。
  768 +CE_TXFULL:发送缓存区已满后,应用程序仍要发送数据。
  769 +lpStat:指向通信端口状态的结构变量。此结构的原始声明如下:
  770 +typedef struct _COMSTAT { //cst
  771 + DWORD fCtsHold : 1; //Tx正在等待CTS信号
  772 + DWORD fDsrHold : 1; //Tx正在等待DSR信号
  773 + DWORD fRlsdHold : 1; //Tx正在等待RLSD信号
  774 + DWORD fXoffHold : 1; //Tx由于接收XOFF字符而在等待
  775 + DWORD fXoffSent : 1; //Tx由于发送XOFF字符而在等待
  776 + DWORD fEof : 1; //发送EOF字符
  777 + DWORD fTxim : 1; //字符在等待Tx
  778 + DWORD fReserved : 25; //保留
  779 + DWORD cbInQue; //输入缓冲区中的字节数
  780 + DWORD cbOutQue; //输出缓冲区中的字节数
  781 +} COMSTAT, *LPCOMSTAT;
  782 +此结构屮有关参数说明如下:
  783 +fCtsHold:是否正在等待CTS信号。占一个位的位置。
  784 +fDsrHold:是否正在等待DSR信号。占一个位的位置。
  785 +fRlsdHoId:是否正在等待RLSD信号。占一个位的位置。
  786 +fXoftHoId:是否因收到xoff字符而在等待。占一个位的位置。
  787 +fXoffHold:是否因送出xoff字符而使得发送的动作在等待。占一个位置
  788 +cbInQue:在输入缓冲区尚未被ReadFile函数读取的数据字节数。这个参数经常被用来进行状态检查。
  789 +cbOutQue:在发送缓冲区而尚未被发送的据字节数
  790 +
  791 +
  792 +WaitForMultipleObjects 是Windows中的一个功能非常强大的函数,几乎可以等待Windows中的所有的内核对象
  793 +
  794 +PurgeComm()函数
  795 +PurgeComm()函数–清空缓冲区
  796 +BOOL PurgeComm( HANDLE hFile, DWORD dwFlags );
  797 +hFile
  798 +[in]处理通信资源。该的CreateFile函数返回该句柄。
  799 +dwFlags
  800 +[in]指定要采取的操作。下表显示了可能的值。
  801 +PURGE_TXABORT 终止所有正在进行的字符输出操作,完成一个正处于等待状态的重叠i/o操作,他将产生一个事件,指明完成了写操作
  802 +PURGE_RXCLEAR 终止所有正在进行的字符输入操作,完成一个正在进行中的重叠i/o操作,并带有已设置得适当事件
  803 +PURGE_TXABORT 这个命令指导设备驱动程序清除输出缓冲区,经常与PURGE_TXABORT 命令标志一起使用
  804 +PURGE_RXCLEAR 这个命令用于设备驱动程序清除输入缓冲区,经常与PURGE_RXABORT 命令标志一起使用
  805 +
  806 +
  807 +
  808 +
  809 +
  810 +
  811 +
  812 +*/
@@ -50,13 +50,13 @@ BOOL PaintTTY( HWND ); @@ -50,13 +50,13 @@ BOOL PaintTTY( HWND );
50 50
51 FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 51 FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
52 52
53 -PURPOSE: Start application and process all window messages 53 +PURPOSE: 启动应用程序并处理所有窗口消息
54 54
55 PARAMETERS: 55 PARAMETERS:
56 hInstance - this apps hinstance 56 hInstance - this apps hinstance
57 - hPrevInstance - previous instance of this app - always NULL 57 + hPrevInstance - 这个应用程序的前一个实例-总是NULL
58 lpCmdLine - command line parameters 58 lpCmdLine - command line parameters
59 - nCmdShow - code for showing window 59 + nCmdShow - 显示窗口的代码
60 60
61 RETURN: 61 RETURN:
62 1 for success 62 1 for success
@@ -95,7 +95,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine @@ -95,7 +95,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
95 95
96 FUNCTION: VersionCheck(void) 96 FUNCTION: VersionCheck(void)
97 97
98 -PURPOSE: Verifies that the correct version of Windows is running 98 +PURPOSE: 验证运行的Windows版本是否正确
99 99
100 RETURN: 100 RETURN:
101 TRUE - success version for running this app 101 TRUE - success version for running this app
@@ -122,8 +122,7 @@ BOOL VersionCheck() @@ -122,8 +122,7 @@ BOOL VersionCheck()
122 122
123 FUNCTION: InitializeApp(HINSTANCE, int) 123 FUNCTION: InitializeApp(HINSTANCE, int)
124 124
125 -PURPOSE: GlobalInitialize, Register window classes  
126 - and create main window 125 +PURPOSE: 全局初始化,注册窗口类,创建主窗口
127 126
128 PARAMETERS: 127 PARAMETERS:
129 hInst - HINSTANCE of this app 128 hInst - HINSTANCE of this app
@@ -140,12 +139,8 @@ HISTORY: Date: Author: Comment: @@ -140,12 +139,8 @@ HISTORY: Date: Author: Comment:
140 BOOL InitializeApp(HINSTANCE hInst, int nShowCmd) 139 BOOL InitializeApp(HINSTANCE hInst, int nShowCmd)
141 { 140 {
142 WNDCLASS wc = {0}; 141 WNDCLASS wc = {0};
143 -  
144 - GlobalInitialize(); // get all global variables initialized to defaults  
145 -  
146 - //  
147 - // setup program's main window class  
148 - // 142 + GlobalInitialize(); // 所有的全局变量初始化为defaultss app
  143 + // 安装程序的主窗口类
149 wc.lpfnWndProc = (WNDPROC) MTTTYWndProc; 144 wc.lpfnWndProc = (WNDPROC) MTTTYWndProc;
150 wc.hInstance = hInst; 145 wc.hInstance = hInst;
151 wc.hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_APPICON)); 146 wc.hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_APPICON));
@@ -153,18 +148,13 @@ BOOL InitializeApp(HINSTANCE hInst, int nShowCmd) @@ -153,18 +148,13 @@ BOOL InitializeApp(HINSTANCE hInst, int nShowCmd)
153 wc.lpszMenuName = MAKEINTRESOURCE(IDR_MTTTYMENU); 148 wc.lpszMenuName = MAKEINTRESOURCE(IDR_MTTTYMENU);
154 wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ; 149 wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
155 wc.lpszClassName = "MTTTYClass"; 150 wc.lpszClassName = "MTTTYClass";
156 -  
157 - if (!RegisterClass(&wc)) {  
158 - GlobalCleanup();  
159 - return FALSE;  
160 - }  
161 - 151 + if (!RegisterClass(&wc))
  152 + {
  153 + GlobalCleanup();
  154 + return FALSE;
  155 + }
162 QuerySerialPortStatusEx(TTYInfo.unOpenedPortAry,2,255); 156 QuerySerialPortStatusEx(TTYInfo.unOpenedPortAry,2,255);
163 - TTYInfo.rgbBGColor = RGB(255,255,255);  
164 -  
165 - //  
166 - // setup program's tty child window class  
167 - // 157 + //设置程序的tty子窗口类
168 wc.lpfnWndProc = (WNDPROC) TTYChildProc; 158 wc.lpfnWndProc = (WNDPROC) TTYChildProc;
169 wc.hInstance = hInst; 159 wc.hInstance = hInst;
170 wc.hCursor = LoadCursor(NULL, IDC_IBEAM); 160 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
@@ -172,32 +162,27 @@ BOOL InitializeApp(HINSTANCE hInst, int nShowCmd) @@ -172,32 +162,27 @@ BOOL InitializeApp(HINSTANCE hInst, int nShowCmd)
172 wc.lpszClassName = "MTTTYChildClass"; 162 wc.lpszClassName = "MTTTYChildClass";
173 wc.lpszMenuName = NULL; 163 wc.lpszMenuName = NULL;
174 wc.hIcon = NULL; 164 wc.hIcon = NULL;
175 -  
176 - if (!RegisterClass(&wc)) {  
177 - GlobalCleanup();  
178 - return FALSE;  
179 - }  
180 -  
181 - // 165 + if (!RegisterClass(&wc))
  166 + {
  167 + GlobalCleanup();
  168 + return FALSE;
  169 + }
182 // create main window 170 // create main window
183 - //  
184 ghwndMain = CreateWindow("MTTTYClass", "BlueFlashTool 鼎维尔科技", 171 ghwndMain = CreateWindow("MTTTYClass", "BlueFlashTool 鼎维尔科技",
185 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 172 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
186 STARTXWINDOW, STARTYWINDOW, 173 STARTXWINDOW, STARTYWINDOW,
187 MAXXWINDOW, MAXYWINDOW, 174 MAXXWINDOW, MAXYWINDOW,
188 NULL, NULL, hInst, NULL); 175 NULL, NULL, hInst, NULL);
189 -  
190 - if (ghwndMain == NULL) {  
191 - GlobalCleanup();  
192 - return FALSE;  
193 - }  
194 - 176 + if (ghwndMain == NULL)
  177 + {
  178 + GlobalCleanup();
  179 + return FALSE;
  180 + }
195 ShowWindow( ghwndMain, nShowCmd ) ; 181 ShowWindow( ghwndMain, nShowCmd ) ;
196 UpdateWindow( ghwndMain ) ; 182 UpdateWindow( ghwndMain ) ;
197 -  
198 ghInst = hInst; 183 ghInst = hInst;
  184 + //LoadAccelerators函数功能:调入加速键表。该函数调入指定的加速键表。
199 ghAccel = LoadAccelerators( hInst, MAKEINTRESOURCE( IDR_MTTTYACCELERATOR) ) ; 185 ghAccel = LoadAccelerators( hInst, MAKEINTRESOURCE( IDR_MTTTYACCELERATOR) ) ;
200 -  
201 return TRUE; 186 return TRUE;
202 } 187 }
203 188
@@ -229,7 +214,7 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) @@ -229,7 +214,7 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
229 { 214 {
230 case WM_CREATE: 215 case WM_CREATE:
231 // 216 //
232 - // since main window is created, I can now open all other windows 217 + // 由于创建了主窗口,我现在可以打开所有其他窗口
233 // 218 //
234 InitTTYInfo(); 219 InitTTYInfo();
235 OpenTTYChildWindow(hwnd); 220 OpenTTYChildWindow(hwnd);
@@ -240,7 +225,7 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) @@ -240,7 +225,7 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
240 225
241 case WM_DESTROY: 226 case WM_DESTROY:
242 // 227 //
243 - // since main windows is being destroyed, so same to other windows 228 + // 由于主窗口正在被销毁,所以其他窗口也一样
244 // 229 //
245 DestroyTTYInfo(); 230 DestroyTTYInfo();
246 DestroyWindow(ghWndToolbarDlg); 231 DestroyWindow(ghWndToolbarDlg);
@@ -253,58 +238,37 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) @@ -253,58 +238,37 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
253 238
254 case WM_GETMINMAXINFO: 239 case WM_GETMINMAXINFO:
255 { 240 {
256 - //  
257 - // make sure that main window doesn't get smaller than  
258 - // the minimum child windows.  
259 - // 241 + // 确保主窗口不会小于最小子窗口。
260 LPMINMAXINFO lpTemp; 242 LPMINMAXINFO lpTemp;
261 POINT ptTemp; 243 POINT ptTemp;
262 -  
263 lpTemp = (LPMINMAXINFO) lParam; 244 lpTemp = (LPMINMAXINFO) lParam;
264 -  
265 ptTemp.x = (long) lpTemp->ptMinTrackSize.x; 245 ptTemp.x = (long) lpTemp->ptMinTrackSize.x;
266 ptTemp.y = (long) gcyMinimumWindowHeight; 246 ptTemp.y = (long) gcyMinimumWindowHeight;
267 -  
268 lpTemp->ptMinTrackSize = ptTemp; 247 lpTemp->ptMinTrackSize = ptTemp;
269 } 248 }
270 -  
271 break; 249 break;
272 250
273 case WM_SIZE: 251 case WM_SIZE:
274 { 252 {
275 - //  
276 - // main window size has changed,  
277 - // so I need to change the positions of child windows  
278 - // 253 + //主窗口大小已经改变,,所以我需要改变子窗口的位置
279 WORD wTop; 254 WORD wTop;
280 WORD wHeight; 255 WORD wHeight;
281 WORD wWidth = LOWORD(lParam); 256 WORD wWidth = LOWORD(lParam);
282 -  
283 - //  
284 - // put Settings window at top  
285 - // 257 + // 把设置窗口放在顶部
286 wHeight = SETTINGSFACTOR*gwBaseY; 258 wHeight = SETTINGSFACTOR*gwBaseY;
287 wTop = 0; 259 wTop = 0;
288 MoveWindow(ghWndToolbarDlg, 0,wTop, wWidth, wHeight, TRUE); 260 MoveWindow(ghWndToolbarDlg, 0,wTop, wWidth, wHeight, TRUE);
289 -  
290 - //  
291 - // put Status window at bottom  
292 - // 261 + // 把状态窗口放在底部
293 wHeight = STATUSFACTOR*gwBaseY; 262 wHeight = STATUSFACTOR*gwBaseY;
294 wTop = HIWORD(lParam) - wHeight; 263 wTop = HIWORD(lParam) - wHeight;
295 MoveWindow(ghWndStatusDlg, 0, wTop, wWidth, wHeight, TRUE); 264 MoveWindow(ghWndStatusDlg, 0, wTop, wWidth, wHeight, TRUE);
296 -  
297 - //  
298 - // put TTTY window right in the middle 265 + // 把TTTY window放在中间
299 // height = whole window - height of two previous windows 266 // height = whole window - height of two previous windows
300 - //  
301 wHeight = HIWORD(lParam) - ((STATUSFACTOR + SETTINGSFACTOR)*gwBaseY); 267 wHeight = HIWORD(lParam) - ((STATUSFACTOR + SETTINGSFACTOR)*gwBaseY);
302 wTop = SETTINGSFACTOR*gwBaseY; 268 wTop = SETTINGSFACTOR*gwBaseY;
303 MoveWindow(ghWndTTY, 0, wTop, wWidth, wHeight, TRUE); 269 MoveWindow(ghWndTTY, 0, wTop, wWidth, wHeight, TRUE);
304 } 270 }
305 -  
306 break; 271 break;
307 -  
308 case WM_COMMAND: 272 case WM_COMMAND:
309 CmdDispatch(LOWORD(wParam), hwnd, lParam); 273 CmdDispatch(LOWORD(wParam), hwnd, lParam);
310 break; 274 break;
@@ -315,16 +279,17 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) @@ -315,16 +279,17 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
315 break; 279 break;
316 280
317 case WM_CLOSE: 281 case WM_CLOSE:
318 - if (DisconnectOK()) {  
319 - if (TTYInfo.fConnected) {  
320 - if (TTYInfo.fTransferring)  
321 - TransferFileTextEnd();  
322 - BreakDownCommPort();  
323 - }  
324 - DestroyWindow(hwnd);  
325 - } 282 + if (DisconnectOK())
  283 + {
  284 + if (TTYInfo.fConnected)
  285 + {
  286 + if (TTYInfo.fTransferring)
  287 + TransferFileTextEnd();
  288 + BreakDownCommPort();
  289 + }
  290 + DestroyWindow(hwnd);
  291 + }
326 break; 292 break;
327 -  
328 default: 293 default:
329 return DefWindowProc(hwnd, message, wParam, lParam); 294 return DefWindowProc(hwnd, message, wParam, lParam);
330 } 295 }
@@ -336,11 +301,11 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) @@ -336,11 +301,11 @@ int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
336 301
337 FUNCTION: CmdDispatch(int, HWND) 302 FUNCTION: CmdDispatch(int, HWND)
338 303
339 -PURPOSE: Responds to menu selections 304 +PURPOSE: 响应菜单选择
340 305
341 PARAMETERS: 306 PARAMETERS:
342 - iMenuChoice - ID of menu choice (from resource file)  
343 - hwnd - window handle of menu owner 307 + iMenuChoice - 菜单选择ID(来自资源文件)
  308 + hwnd - 菜单所有者的窗口句柄
344 309
345 HISTORY: Date: Author: Comment: 310 HISTORY: Date: Author: Comment:
346 10/27/95 AllenD Wrote it 311 10/27/95 AllenD Wrote it
@@ -349,40 +314,34 @@ HISTORY: Date: Author: Comment: @@ -349,40 +314,34 @@ HISTORY: Date: Author: Comment:
349 void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam) 314 void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam)
350 { 315 {
351 static char szFileName[MAX_PATH] = {0}; 316 static char szFileName[MAX_PATH] = {0};
352 -  
353 switch (iMenuChoice) 317 switch (iMenuChoice)
354 { 318 {
355 case ID_HELP_ABOUTMTTTY: 319 case ID_HELP_ABOUTMTTTY:
356 CmdAbout(hwnd); 320 CmdAbout(hwnd);
357 break; 321 break;
358 -  
359 case ID_TRANSFER_SENDFILETEXT: 322 case ID_TRANSFER_SENDFILETEXT:
360 - { 323 + {// 发送文件
361 if (strlen(TTYInfo.szFileName) == 0) 324 if (strlen(TTYInfo.szFileName) == 0)
362 { 325 {
363 - char * szFilter = "Text Files\0*.*\0";  
364 - OPENFILENAME ofn = {0};  
365 -  
366 - ofn.lStructSize = sizeof(OPENFILENAME);  
367 - ofn.hwndOwner = hwnd;  
368 - ofn.lpstrFilter = szFilter;  
369 - ofn.lpstrFile = szFileName;  
370 - ofn.nMaxFile = MAX_PATH;  
371 - ofn.lpstrTitle = "Send File";  
372 - ofn.Flags = OFN_FILEMUSTEXIST;  
373 - 326 + char * szFilter = "Text Files\0*.*\0";
  327 + OPENFILENAME ofn = { 0 };
  328 + ofn.lStructSize = sizeof(OPENFILENAME);
  329 + ofn.hwndOwner = hwnd;
  330 + ofn.lpstrFilter = szFilter;
  331 + ofn.lpstrFile = szFileName;
  332 + ofn.nMaxFile = MAX_PATH;
  333 + ofn.lpstrTitle = "Send File";
  334 + ofn.Flags = OFN_FILEMUSTEXIST;
374 if (!GetOpenFileName(&ofn)) 335 if (!GetOpenFileName(&ofn))
375 break; 336 break;
376 - strcpy(TTYInfo.szFileName,szFileName); }  
377 -  
378 -  
379 - if (TRUE)  
380 - TransferFileTextStart(TTYInfo.szFileName);  
381 - } 337 + strcpy(TTYInfo.szFileName, szFileName);
  338 + }
  339 + if (TRUE)
  340 + TransferFileTextStart(TTYInfo.szFileName);
  341 + }
382 break; 342 break;
383 -  
384 case ID_TRANSFER_RECEIVEFILETEXT: 343 case ID_TRANSFER_RECEIVEFILETEXT:
385 - { 344 + {// 接收文件
386 char * szFilter = "Text Files\0*.TXT\0"; 345 char * szFilter = "Text Files\0*.TXT\0";
387 OPENFILENAME ofn = {0}; 346 OPENFILENAME ofn = {0};
388 347
@@ -402,7 +361,7 @@ void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam) @@ -402,7 +361,7 @@ void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam)
402 break; 361 break;
403 362
404 case ID_TRANSFER_ABORTSENDING: 363 case ID_TRANSFER_ABORTSENDING:
405 - // was abort sent from the abort button? 364 + // 是否从中止按钮发出中止?
406 if (LOWORD(lParam) == IDC_ABORTBTN) { 365 if (LOWORD(lParam) == IDC_ABORTBTN) {
407 // am I in a transfer repeat? 366 // am I in a transfer repeat?
408 if (TTYInfo.fRepeating) 367 if (TTYInfo.fRepeating)
@@ -415,12 +374,12 @@ void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam) @@ -415,12 +374,12 @@ void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam)
415 gfAbortTransfer = TRUE; 374 gfAbortTransfer = TRUE;
416 } 375 }
417 else 376 else
418 - // transfer abort was sent by transfer thread 377 + //传输中止
419 TransferFileTextEnd(); 378 TransferFileTextEnd();
420 break; 379 break;
421 380
422 case ID_TRANSFER_SENDREPEATEDLY: 381 case ID_TRANSFER_SENDREPEATEDLY:
423 - { 382 + {// 重复发送
424 DWORD dwFreq; 383 DWORD dwFreq;
425 char * szFilter = "Text Files\0*.TXT\0"; 384 char * szFilter = "Text Files\0*.TXT\0";
426 OPENFILENAME ofn = {0}; 385 OPENFILENAME ofn = {0};
@@ -481,12 +440,12 @@ void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam) @@ -481,12 +440,12 @@ void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam)
481 440
482 FUNCTION: OpenTTYChildWindow(HWND) 441 FUNCTION: OpenTTYChildWindow(HWND)
483 442
484 -PURPOSE: Creates the TTY Child Window 443 +PURPOSE: 创建TTY子窗口
485 444
486 PARAMETERS: 445 PARAMETERS:
487 - hWnd - parent window handle of TTY child window 446 + hWnd -TTY子窗口的父窗口句柄
488 447
489 -COMMENTS: This window is actually the TTY Screen 448 +COMMENTS: 这个窗口实际上是TTY屏幕
490 449
491 HISTORY: Date: Author: Comment: 450 HISTORY: Date: Author: Comment:
492 10/27/95 AllenD Wrote it 451 10/27/95 AllenD Wrote it
@@ -506,27 +465,21 @@ void OpenTTYChildWindow(HWND hWnd) @@ -506,27 +465,21 @@ void OpenTTYChildWindow(HWND hWnd)
506 } 465 }
507 466
508 467
509 -//---------------------------------------------------------------------------  
510 -// BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )  
511 -//  
512 -// Description:  
513 -// Scrolls TTY window vertically.  
514 -//  
515 -// Parameters:  
516 -// HWND hWnd  
517 -// handle to TTY window  
518 -//  
519 -// WORD wScrollCmd  
520 -// type of scrolling we're doing  
521 -//  
522 -// WORD wScrollPos  
523 -// scroll position  
524 -//  
525 -// History: Date Author Comment  
526 -// 5/ 8/91 BryanW Wrote it.  
527 -// 10/27/95 AllenD Included it for MTTTY Sample.  
528 -//  
529 -//--------------------------------------------------------------------------- 468 +/*
  469 + BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
  470 + Description:
  471 + 垂直滚动TTY窗口。
  472 + Parameters:
  473 + HWND hWnd
  474 + handle to TTY window
  475 + WORD wScrollCmd
  476 + type of scrolling we're doing
  477 + WORD wScrollPos
  478 + scroll position
  479 + History: Date Author Comment
  480 + 5/ 8/91 BryanW Wrote it.
  481 + 10/27/95 AllenD Included it for MTTTY Sample.
  482 +*/
530 BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos ) 483 BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
531 { 484 {
532 int nScrollAmt ; 485 int nScrollAmt ;
@@ -925,10 +878,7 @@ int WINAPI TTYChildProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam) @@ -925,10 +878,7 @@ int WINAPI TTYChildProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
925 break ; 878 break ;
926 879
927 case WM_MOUSEACTIVATE: 880 case WM_MOUSEACTIVATE:
928 - /*  
929 - If mouse is clicked in me (the tty child window)  
930 - then I need to get the focus.  
931 - */ 881 + // 如果鼠标在(tty子窗口)中被点击 然后设置焦点。
932 SetFocus(hWnd); 882 SetFocus(hWnd);
933 return MA_ACTIVATE; 883 return MA_ACTIVATE;
934 break; 884 break;
@@ -23,20 +23,24 @@ @@ -23,20 +23,24 @@
23 23
24 // 24 //
25 // GLOBAL DEFINES 25 // GLOBAL DEFINES
26 -// 26 +//输出buff size
27 #define TTY_BUFFER_SIZE MAXROWS * MAXCOLS 27 #define TTY_BUFFER_SIZE MAXROWS * MAXCOLS
28 #define MAX_STATUS_BUFFER 20000 28 #define MAX_STATUS_BUFFER 20000
  29 +//读写buff size
29 #define MAX_WRITE_BUFFER 1024 30 #define MAX_WRITE_BUFFER 1024
30 #define MAX_READ_BUFFER 2048 31 #define MAX_READ_BUFFER 2048
  32 +//超时设定
31 #define READ_TIMEOUT 500 33 #define READ_TIMEOUT 500
32 #define STATUS_CHECK_TIMEOUT 500 34 #define STATUS_CHECK_TIMEOUT 500
33 #define WRITE_CHECK_TIMEOUT 500 35 #define WRITE_CHECK_TIMEOUT 500
  36 +//清空缓冲区
34 #define PURGE_FLAGS PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR 37 #define PURGE_FLAGS PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR
  38 +
35 #define EVENTFLAGS_DEFAULT EV_BREAK | EV_CTS | EV_DSR | EV_ERR | EV_RING | EV_RLSD 39 #define EVENTFLAGS_DEFAULT EV_BREAK | EV_CTS | EV_DSR | EV_ERR | EV_RING | EV_RLSD
36 #define FLAGCHAR_DEFAULT '\n' 40 #define FLAGCHAR_DEFAULT '\n'
37 41
38 // 42 //
39 -// Write request types 43 +//写请求类型
40 // 44 //
41 #define WRITE_CHAR 0x01 45 #define WRITE_CHAR 0x01
42 #define WRITE_FILE 0x02 46 #define WRITE_FILE 0x02
@@ -54,8 +58,8 @@ @@ -54,8 +58,8 @@
54 // 58 //
55 // window coords 59 // window coords
56 // 60 //
57 -#define MAXXWINDOW 750  
58 -#define MAXYWINDOW 530 61 +#define MAXXWINDOW 350
  62 +#define MAXYWINDOW 230
59 #define STARTXWINDOW 80 63 #define STARTXWINDOW 80
60 #define STARTYWINDOW 70 64 #define STARTYWINDOW 70
61 65
@@ -91,7 +95,7 @@ WORD gwBaseY; @@ -91,7 +95,7 @@ WORD gwBaseY;
91 LONG gcyMinimumWindowHeight; 95 LONG gcyMinimumWindowHeight;
92 96
93 // 97 //
94 -// Flags controlling thread actions 98 +// 控制线程操作的标志
95 // 99 //
96 HANDLE ghThreadExitEvent; 100 HANDLE ghThreadExitEvent;
97 BOOL gfAbortTransfer; 101 BOOL gfAbortTransfer;
@@ -125,7 +129,7 @@ typedef struct STATUS_MESSAGE @@ -125,7 +129,7 @@ typedef struct STATUS_MESSAGE
125 129
126 130
127 // 131 //
128 -// Port name 132 +// 端口名称
129 // 133 //
130 char gszPort[10]; 134 char gszPort[10];
131 135
@@ -139,7 +143,7 @@ HANDLE ghWriterEvent; @@ -139,7 +143,7 @@ HANDLE ghWriterEvent;
139 HANDLE ghTransferCompleteEvent; 143 HANDLE ghTransferCompleteEvent;
140 144
141 // 145 //
142 -// Write request data structure; look in Writer.c for more info 146 +// 写请求数据结构;在Writer.c中查找更多信息
143 // 147 //
144 typedef struct WRITEREQUEST; 148 typedef struct WRITEREQUEST;
145 149
@@ -13,9 +13,9 @@ @@ -13,9 +13,9 @@
13 PURPOSE: Read from comm port 13 PURPOSE: Read from comm port
14 14
15 FUNCTIONS: 15 FUNCTIONS:
16 - OutputABufferToWindow - process incoming data destined for tty window  
17 - OutputABufferToFile - process incoming data destined for a file  
18 - OutputABuffer - called when data is read from port 16 + OutputABufferToWindow - 传入的数据显示到窗口
  17 + OutputABufferToFile - 传入的数据保存到文件
  18 + OutputABuffer - 从端口读取数据时调用
19 19
20 -----------------------------------------------------------------------------*/ 20 -----------------------------------------------------------------------------*/
21 21
@@ -34,7 +34,7 @@ void OutputABufferToFile( HANDLE, char *, DWORD ); @@ -34,7 +34,7 @@ void OutputABufferToFile( HANDLE, char *, DWORD );
34 34
35 FUNCTION: OutputABufferToWindow(HWND, char *, DWORD) 35 FUNCTION: OutputABufferToWindow(HWND, char *, DWORD)
36 36
37 -PURPOSE: Updates TTY Buffer with characters just received. 37 +PURPOSE: 用刚刚收到的字符更新TTY缓冲区。
38 38
39 PARAMETERS: 39 PARAMETERS:
40 hTTY - handle to the TTY child window 40 hTTY - handle to the TTY child window
@@ -47,84 +47,72 @@ HISTORY: Date Author Comment @@ -47,84 +47,72 @@ HISTORY: Date Author Comment
47 47
48 -----------------------------------------------------------------------------*/ 48 -----------------------------------------------------------------------------*/
49 void OutputABufferToWindow(HWND hTTY, char * lpBuf, DWORD dwBufLen) 49 void OutputABufferToWindow(HWND hTTY, char * lpBuf, DWORD dwBufLen)
50 -{  
51 - RECT rect;  
52 -  
53 - /*  
54 - update screen buffer with new buffer  
55 - need to do a character by character check  
56 - for special characters  
57 - */  
58 - int i;  
59 -  
60 - for ( i = 0 ; i < (int) dwBufLen; i++) {  
61 - switch (lpBuf[ i ]) {  
62 - case ASCII_BEL: // BELL CHAR  
63 - MessageBeep( 0 ) ;  
64 - break ;  
65 -  
66 - case ASCII_BS: // Backspace CHAR  
67 - if (TTYInfo.nColumn > 0)  
68 - TTYInfo.nColumn -- ;  
69 - break ;  
70 -  
71 - case ASCII_CR: // Carriage Return  
72 - TTYInfo.nColumn = 0 ;  
73 - if (!TTYInfo.fNewLine)  
74 - break;  
75 -  
76 - //  
77 - // FALL THROUGH  
78 - //  
79 -  
80 - case ASCII_LF: // Line Feed  
81 - if (TTYInfo.nRow++ == MAXROWS - 1)  
82 - {  
83 - MoveMemory( (LPSTR) (TTYInfo.Screen),  
84 - (LPSTR) (TTYInfo.Screen + MAXCOLS),  
85 - (MAXROWS - 1) * MAXCOLS ) ;  
86 - FillMemory((LPSTR) (TTYInfo.Screen + (MAXROWS - 1) * MAXCOLS),  
87 - MAXCOLS, ' ' ) ;  
88 - InvalidateRect( hTTY, NULL, FALSE ) ;  
89 - TTYInfo.nRow-- ;  
90 - }  
91 - break ;  
92 -  
93 - default: // standard character  
94 - TTYInfo.Screen[TTYInfo.nRow * MAXCOLS + TTYInfo.nColumn]= lpBuf[ i ];  
95 -  
96 - rect.left = (TTYInfo.nColumn * TTYInfo.xChar) -  
97 - TTYInfo.xOffset ;  
98 - rect.right = rect.left + TTYInfo.xChar ;  
99 - rect.top = (TTYInfo.nRow * TTYInfo.yChar) -  
100 - TTYInfo.yOffset ;  
101 - rect.bottom = rect.top + TTYInfo.yChar ;  
102 - InvalidateRect( hTTY, &rect, FALSE ) ;  
103 -  
104 - //  
105 - // Line wrap  
106 - //  
107 - if (TTYInfo.nColumn < MAXCOLS-1 )  
108 - TTYInfo.nColumn++ ;  
109 - else if (TTYInfo.fAutowrap)  
110 - OutputABufferToWindow(hTTY, "\r\n", 2 ) ;  
111 -  
112 - break;  
113 - }  
114 - }  
115 50
116 - MoveTTYCursor(hTTY);  
117 - return; 51 +{
  52 + RECT rect;
  53 +//更新屏幕缓冲区与新的缓冲区 需要一个字符一个字符地检查 为特殊字符
  54 + int i;
  55 +
  56 + for (i = 0; i < dwBufLen; i++)
  57 + {
  58 + switch (lpBuf[i])
  59 + {
  60 + case ASCII_BEL: // BELL CHAR
  61 + MessageBeep(0);
  62 + break;
  63 + case ASCII_BS: // Backspace CHAR
  64 + if (TTYInfo.nColumn > 0)
  65 + TTYInfo.nColumn--;
  66 + break;
  67 + case ASCII_CR: // Carriage Return
  68 + TTYInfo.nColumn = 0;
  69 + if (!TTYInfo.fNewLine)
  70 + break;
  71 +
  72 + //
  73 + // FALL THROUGH
  74 + //
  75 + case ASCII_LF: // Line Feed
  76 + if (TTYInfo.nRow++ == MAXROWS - 1)
  77 + {
  78 + MoveMemory((LPSTR) (TTYInfo.Screen),
  79 + (LPSTR) (TTYInfo.Screen + MAXCOLS),
  80 + (MAXROWS - 1) *MAXCOLS);
  81 + FillMemory((LPSTR) (TTYInfo.Screen + (MAXROWS - 1) *MAXCOLS),
  82 + MAXCOLS, ' ');
  83 + InvalidateRect(hTTY, NULL, FALSE);
  84 + TTYInfo.nRow--;
  85 + }
  86 + break;
  87 + default: // standard character
  88 + TTYInfo.Screen[TTYInfo.nRow * MAXCOLS + TTYInfo.nColumn] = lpBuf[i];
  89 + rect.left = (TTYInfo.nColumn * TTYInfo.xChar) -TTYInfo.xOffset;
  90 + rect.right = rect.left + TTYInfo.xChar;
  91 + rect.top = (TTYInfo.nRow * TTYInfo.yChar) -TTYInfo.yOffset;
  92 + rect.bottom = rect.top + TTYInfo.yChar;
  93 + InvalidateRect(hTTY, &rect, FALSE);
  94 + // Line wrap
  95 + if (TTYInfo.nColumn < MAXCOLS - 1)
  96 + TTYInfo.nColumn++;
  97 + else if (TTYInfo.fAutowrap)
  98 + OutputABufferToWindow(hTTY, "\r\n", 2);
  99 + break;
  100 + }
  101 + }
  102 + MoveTTYCursor(hTTY);
  103 + return;
118 } 104 }
119 105
  106 +
  107 +
120 /*----------------------------------------------------------------------------- 108 /*-----------------------------------------------------------------------------
121 109
122 FUNCTION: OutputABufferToFile(HANDLE, char *, DWORD) 110 FUNCTION: OutputABufferToFile(HANDLE, char *, DWORD)
123 111
124 -PURPOSE: Output a rec'd buffer to a file 112 +PURPOSE: 将已读取的缓冲区输出到文件中
125 113
126 PARAMETERS: 114 PARAMETERS:
127 - hFile - handle of file save data into 115 + hFile - 文件的句柄保存数据到
128 lpBuf - address of data buffer 116 lpBuf - address of data buffer
129 dwBufLen - size of data buffer 117 dwBufLen - size of data buffer
130 118
@@ -9,9 +9,8 @@ @@ -9,9 +9,8 @@
9 9
10 MODULE: ReadStat.c 10 MODULE: ReadStat.c
11 11
12 - PURPOSE: Thread procedure responsible for reading comm port,  
13 - reporting status events, report status messages,  
14 - and periodically updating status controls 12 + PURPOSE:
  13 + 线程过程负责读取通信端口,报告状态事件,报告状态消息,并定期更新状态控制
15 14
16 FUNCTIONS: 15 FUNCTIONS:
17 ReaderAndStatusProc - Thread procedure does the work here 16 ReaderAndStatusProc - Thread procedure does the work here
@@ -28,8 +27,7 @@ @@ -28,8 +27,7 @@
28 27
29 FUNCTION: ReaderAndStatusProc(LPVOID) 28 FUNCTION: ReaderAndStatusProc(LPVOID)
30 29
31 -PURPOSE: Thread function controls comm port reading, comm port status  
32 - checking, and status messages. 30 +PURPOSE: 线程功能控制通信端口读取、通信端口状态检查和状态消息。
33 31
34 PARMATERS: 32 PARMATERS:
35 lpV - 4 byte value contains the tty child window handle 33 lpV - 4 byte value contains the tty child window handle
@@ -16,8 +16,8 @@ @@ -16,8 +16,8 @@
16 SetCommState and SetCommTimeouts. 16 SetCommState and SetCommTimeouts.
17 17
18 FUNCTIONS: 18 FUNCTIONS:
19 - OpenSettingsToolbar - Creates the Settings Dialog  
20 - ChangeConnection - Modifies menus & controls based on connection state 19 + OpenSettingsToolbar - 创建设置对话框
  20 + ChangeConnection - 根据连接状态修改菜单和控件
21 UpdateTTYInfo - Modifies TTY data from Settings Dialog and 21 UpdateTTYInfo - Modifies TTY data from Settings Dialog and
22 if connected, updates open comm port settings 22 if connected, updates open comm port settings
23 FillComboBox - Fills a combo box with strings 23 FillComboBox - Fills a combo box with strings
@@ -299,10 +299,9 @@ void UpdateTTYInfo() @@ -299,10 +299,9 @@ void UpdateTTYInfo()
299 299
300 FUNCTION: UpdateConnection( void ) 300 FUNCTION: UpdateConnection( void )
301 301
302 -PURPOSE: Sets port state based on settings from the user 302 +PURPOSE: 根据用户的设置设置端口状态
303 303
304 -COMMENTS: Sets up DCB structure and calls SetCommState.  
305 - Sets up new timeouts by calling SetCommTimeouts. 304 +COMMENTS: 设置DCB结构并调用SetCommState。通过调用SetCommTimeouts设置新的超时。
306 305
307 HISTORY: Date: Author: Comment: 306 HISTORY: Date: Author: Comment:
308 1/ 9/96 AllenD Wrote it 307 1/ 9/96 AllenD Wrote it
@@ -59,20 +59,15 @@ BOOL GetTransferSizes( HANDLE, DWORD *, DWORD *, DWORD *); @@ -59,20 +59,15 @@ BOOL GetTransferSizes( HANDLE, DWORD *, DWORD *, DWORD *);
59 59
60 FUNCTION: TransferRepeatCreate(LPCTSTR, DWORD) 60 FUNCTION: TransferRepeatCreate(LPCTSTR, DWORD)
61 61
62 -PURPOSE: Prepares program for a repeated text file transfer (send) 62 +PURPOSE: 重复的文件传输(发送)准备
63 63
64 PARAMETERS: 64 PARAMETERS:
65 lpstrFileName - name of file selected to send 65 lpstrFileName - name of file selected to send
66 dwFrequency - frequency of timer 66 dwFrequency - frequency of timer
67 67
68 -COMMENTS: This function sets up a window timer to fire off  
69 - every so often. When it fires, TransferRepeatDo is  
70 - called with the same name as above. This causes the file transfer  
71 - to actuall take place.  
72 - TransferRepeatDestroy is called to kill the timer.  
73 - This function disables certain menu items that should not be  
74 - available for the duration of a repeated send even if the actual  
75 - Tx is not taking place. 68 +COMMENTS:
  69 +This function sets up a window timer to fire off every so often. When it fires, TransferRepeatDo is called with the same name as above. This causes the file transfer to actuall take place.
  70 +TransferRepeatDestroy is called to kill the timer. This function disables certain menu items that should not be available for the duration of a repeated send even if the actual Tx is not taking place.
76 71
77 HISTORY: Date: Author: Comment: 72 HISTORY: Date: Author: Comment:
78 1/29/96 AllenD Wrote it 73 1/29/96 AllenD Wrote it
Please register or login to post a comment