📄 chapt05.cpp
字号:
//
// CHAPT05.CPP
//
// Serial Communications: A C++ Developer's Guide, 2nd Edition
// by Mark Nelson, IDG Books, 1999
//
// Please see the book for information on usage.
//
// This test program is used to test the Intelligent Digiboard
// board driver. To build it using your compiler, see CHAPT05.MAK
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include "rs232.h"
#include "textwind.h"
#include "digi.h"
#include "pc8250.h"
//
// The following short section of definitions are used to customize
// the program. You can hard code 1 or 4 ports, and select various
// port setups. If you want to use a different number it's pretty
// easy to make other configurations work.
//
// In the one port configuration, I define the first port used to
// be COM2. If your system uses a PS/2 or other BUS mouse, you might
// want to change this to COM1.
//
#define PORT_COUNT 5
RS232PortName classic_port_names[ 4 ] = { COM5, COM6, COM7, COM8 };
TextWindow *Windows[ PORT_COUNT ] = { 0 };
RS232 *Ports[ PORT_COUNT ] = { 0 };
RS232 *Port = 0;
int window_parms[ PORT_COUNT ][ 4 ] = {
{ 1, 1, 38, 10 },
{ 1, 41, 38, 10 },
{ 13, 1, 38, 10 },
{ 13, 41, 38, 10 },
{ 25, 1, 78, 10 }
};
int Spewing[ PORT_COUNT ] = { 0 };
long SpewCount[ PORT_COUNT ] = { 0L };
int Reading[ PORT_COUNT ] = { 0 };
void HandleCommand( int key );
void DrawHelp( void );
void DrawDump( void );
TextWindow *UserWindow;
TextWindow *StatusLine;
int FocusWindow = -1;
int Done = 0;
int Helping = 1;
long NextStatusUpdate = 0;
int main()
{
int i;
int c;
char buffer[ 81 ];
Set43LineMode( 1 );
TextWindow *temp = new TextWindow( 0, 0, 80, 43 );
UserWindow = new TextWindow( 36, 0, 80, 12 );
StatusLine = new TextWindow( 48, 0, 80, 1 );
temp->Clear();
delete temp;
UserWindow->Clear();
StatusLine->Clear();
for ( i = 0 ; i < PORT_COUNT ; i++ ) {
//
// This switch allows you to mix and match different
// port types. In this program the first four ports
// are Digi ports, and the last is a standard
// PC COM1.
//
switch ( i ) {
case 0 :
case 1 :
case 2 :
case 3 :
Ports[ i ] = new DigiBoard( classic_port_names[ i ],
57600L, 'N', 8, 1 );
break;
case 4 :
Ports[ i ] = new PC8250( COM2,
57600L, 'N', 8, 1, 1, 1 );
break;
}
if ( Ports[ i ]->ErrorStatus() == RS232_SUCCESS ) {
Windows[ i ] = new TextWindow( window_parms[ i ][ 0 ],
window_parms[ i ][ 1 ],
window_parms[ i ][ 2 ],
window_parms[ i ][ 3 ] );
Windows[ i ]->Clear();
Windows[ i ]->AddBorder();
Windows[ i ]->SetPosition( 0, 0 );
if ( FocusWindow == -1 )
FocusWindow = i;
Reading[ i ] = 1;
} else {
*StatusLine << "\nError opening port "
<< itoa( i, buffer, 10 )
<< " status = "
<< Ports[ i ]->ErrorName(
Ports[ i ]->ErrorStatus() )
<< ". Hit any key to continue...";
while ( Windows[ FocusWindow ]->ReadKey() == 0 )
;
*StatusLine << '\n';
}
}
DrawHelp();
Port = Ports[ FocusWindow ];
while ( FocusWindow != -1 && !Done ) {
c = Windows[ FocusWindow ]->ReadKey();
if ( c > 255 ) {
HandleCommand( c );
Windows[ FocusWindow ]->Goto();
} else if ( c != 0 )
Port->Write( c );
for ( i = 0 ; i < PORT_COUNT ; i++ ) {
if ( Spewing[ i ] && Ports[ i ]->TXSpaceUsed() == 0 ) {
sprintf( buffer,
"Spewing packet %ld from port %d\r\n",
SpewCount[ i ]++,
i );
Ports[ i ]->Write( buffer );
}
if ( Reading[ i ] ) {
Ports[ i ]->Read( buffer, 80 );
if ( Ports[ i ]->ByteCount > 0 ) {
*Windows[ i ] << buffer;
if ( i != FocusWindow )
Windows[ FocusWindow ]->Goto();
}
}
}
if ( !Helping ) {
if ( ReadTime() > NextStatusUpdate ) {
DrawDump();
NextStatusUpdate = ReadTime() + 500;
}
}
}
for ( i = 0 ; i < PORT_COUNT ; i++ ) {
if ( Windows[ i ] )
delete Windows[ i ];
delete Ports[ i ];
}
Set43LineMode( 0 );
return 0;
}
void HandleCommand( int c )
{
char buffer[ 11 ];
RS232Error status;
int result;
*StatusLine << '\n';
switch ( c ) {
case F1 :
Helping = !Helping;
UserWindow->Clear();
if ( Helping )
DrawHelp();
return;
case F2 :
do
FocusWindow = ++FocusWindow % PORT_COUNT;
while ( Windows[ FocusWindow ] == 0 );
Port = Ports[ FocusWindow ];
if ( !Helping )
UserWindow->Clear();
Windows[ FocusWindow ]->Goto();
return;
case F3 :
Spewing[ FocusWindow ] = !Spewing[ FocusWindow ];
Windows[ FocusWindow ]->AddBorder();
if ( Spewing[ FocusWindow ] )
Windows[ FocusWindow ]->DisplayTitle( "Spewing" );
return;
case F4 :
Reading[ FocusWindow ] = !Reading[ FocusWindow ];
*StatusLine << "Window "
<< itoa( FocusWindow, buffer, 10 );
*StatusLine << " reading flag is "
<< itoa( Reading[ FocusWindow ], buffer, 10 );
return;
case F5 :
ReadLine( "New baud rate:", buffer, 10 );
status = Port->Set( atol( buffer ) );
*StatusLine << "Set baud rate to "
<< ltoa( atol( buffer ), buffer, 10 )
<< " returns status of: ";
break;
case F6 :
ReadLine( "New parity:", buffer, 10 );
status = Port->Set( UNCHANGED, buffer[ 0 ] );
*StatusLine << "Set parity to "
<< buffer[ 0 ]
<< " returns status of: ";
break;
case F7 :
ReadLine( "New word length:", buffer, 10 );
status = Port->Set( UNCHANGED, UNCHANGED, atoi( buffer ) );
*StatusLine << "Set word length to "
<< itoa( atoi( buffer ), buffer, 10 )
<< " returns status of: ";
break;
case F8 :
ReadLine( "New stop bits:", buffer, 10 );
status = Port->Set( UNCHANGED,
UNCHANGED,
UNCHANGED,
atoi( buffer ) );
*StatusLine << "Set stop bits to "
<< itoa( atoi( buffer ), buffer, 10 )
<< " returns status of: ";
break;
case F9 :
status = (RS232Error) Port->Break();
*StatusLine << "SendBreak returns: ";
break;
case F10 :
Done = 1;
return;
case ALT_F1 :
result = Port->XonXoffHandshaking();
status = (RS232Error) Port->XonXoffHandshaking( !result );
*StatusLine << "Toggle XON/XOFF handshaking returns: ";
if ( status >= 0 ) {
*StatusLine << itoa( status, buffer, 10 );
return;
}
break;
case ALT_F2 :
result = Port->RtsCtsHandshaking();
status = (RS232Error) Port->RtsCtsHandshaking( !result );
*StatusLine << "Toggle RTS/CTS handshaking returns: ";
if ( status >= 0 ) {
*StatusLine << itoa( status, buffer, 10 );
return;
}
break;
case ALT_F3 :
result = Port->DtrDsrHandshaking();
status = (RS232Error) Port->DtrDsrHandshaking( !result );
*StatusLine << "Toggle DTR/DSR handshaking returns: ";
if ( status >= 0 ) {
*StatusLine << itoa( status, buffer, 10 );
return;
}
break;
case ALT_F4 :
status = (RS232Error) Port->Rts( !Port->Rts() );
*StatusLine << "Toggle RTS returns: ";
if ( status >= 0 ) {
*StatusLine << itoa( status, buffer, 10 );
return;
}
break;
case ALT_F5 :
status = (RS232Error) Port->Dtr( !Port->Dtr() );
*StatusLine << "Toggle Dtr returns: ";
if ( status >= 0 ) {
*StatusLine << itoa( status, buffer, 10 );
return;
}
break;
case ALT_F6 :
status = (RS232Error) Port->FlushRXBuffer();
*StatusLine << "Flush RX Buffer returns: ";
break;
case ALT_F7 :
status = (RS232Error) Port->FlushTXBuffer();
*StatusLine << "Flush TX Buffer returns: ";
break;
case ALT_F8 :
status = (RS232Error) Port->Peek();
*StatusLine << "Peek count: "
<< itoa( Port->ByteCount, buffer, 10 )
<< " ";
if ( status >= 0 ) {
if ( Port->ByteCount > 0 ) {
*StatusLine << "Char: ";
if ( isprint( status ) )
*StatusLine << (char) status;
else
*StatusLine << '<'
<< itoa( status, buffer, 16 )
<< '>';
}
return;
}
*StatusLine << "Status: ";
break;
case ALT_F9 :
status = (RS232Error) Port->Read();
if ( status >= 0 ) {
*Windows[ FocusWindow ] << (char) status;
return;
}
*StatusLine << "Read character returns: ";
break;
case ALT_F10 :
{
void *p = malloc( 1024 );
if ( !p )
return;
memset( p, 'A', 1024 );
status = (RS232Error) Port->Write( p, 1024, 2500 );
free( p );
}
*StatusLine << "Write 1K ByteCount: "
<< itoa( Port->ByteCount, buffer, 10 )
<< " Elapsed Time: ";
*StatusLine << ltoa( Port->ElapsedTime, buffer, 10 )
<< " Status: ";
break;
default :
return;
}
*StatusLine << Port->ErrorName( status );
}
void DrawDump( void )
{
int i;
char buffer[ 81 ];
UserWindow->SetPosition( 0, 0 );
for ( i = 0 ;
i < Port->DebugLineCount() ;
i++ ) {
Port->FormatDebugOutput( buffer, i );
if ( i != 0 )
*UserWindow << '\n';
*UserWindow << buffer;
}
Windows[ FocusWindow ]->Goto();
}
void DrawHelp( void )
{
UserWindow->Clear();
UserWindow->SetPosition( 0, 0 );
*UserWindow << "F1 Help Toggle ALT F1 Toggle XON/XOFF\n";
*UserWindow << "F2 Next port ALT F2 Toggle RTS/CTS\n";
*UserWindow << "F3 Spew Toggle ALT F3 Toggle DTR/DSR\n";
*UserWindow << "F4 Reading Toggle ALT F4 Toggle RTS\n";
*UserWindow << "F5 Set baud rate ALT F5 Toggle DTR\n";
*UserWindow << "F6 Set parity ALT F6 Flush RX Buffer\n";
*UserWindow << "F7 Set word length ALT F7 Flush TX Buffer\n";
*UserWindow << "F8 Set stop bits ALT F8 Peek at 1 byte\n";
*UserWindow << "F9 Send break ALT F9 Read a byte\n";
*UserWindow << "F10 Exit ALT F10 Send 1K block\n";
Windows[ FocusWindow ]->Goto();
}
// ******************** END OF TEST04.CPP ********************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -