/* Revison 1.0 */
/********************************************************************
TCP/IP Speed Test Program
This program will test the TCP transmit and recive rates.
Test Procedure:
1. Download program to Rabbit module
2. When module boots, note IP address
3. When "Waiting for PC program to start" message appears, run
SpeedPC.exe
4. Module will send 10MBytes of data to the PC as quickly as possible.
5. SpeedPC and the module program will display the amount of time and
data transmitted.
6. The second test is similar, with 10MBytes of data being sent
from the PC to the module. Since any real product would do
something with the received data, a simple "if" statement is
used to determine if the received characters are printable
ASCII.
*******************************************************************/
#define TCPCONFIG 1
#define _PRIMARY_STATIC_IP "10.1.1.51"
#define _PRIMARY_NETMASK "255.255.255.0"
#define MY_GATEWAY "10.1.1.1"
#define MY_NAMESERVER "10.1.1.1"
#memmap xmem
#use "dcrtcp.lib"
#define PORT 1234 // module tcp listen port number
#define NUM_OF_BYTES 10000000 // send/receive 10MBytes of data
#define TCP_BUFFER_SEGMENTS 4
#define BUFFER_SIZE 20000 // read/write buffer storage
char DataBuffer[BUFFER_SIZE];
tcp_Socket fda;
main() {
int status;
long ipaddr;
int i, n;
unsigned long dw1, dw2;
unsigned long BytesSent, BytesReceived;
float DataRate;
unsigned long PrintableChars;
unsigned long ulTemp, *puLong;
printf("Calling sock_init()....");
status = sock_init();
while (ifpending(IF_DEFAULT) == IF_COMING_UP)
{
tcp_tick(NULL);
}
printf("complete, status = %d\n", status );
ifconfig(IF_ETH0, IFG_IPADDR, &ipaddr, IFS_END);
printf("IP Address: %lX\n", ipaddr);
// Initialize data buffer values
for ( n = 0; n < BUFFER_SIZE; n++ )
DataBuffer[n] = n;
printf("Waiting for PC program to start...\r\n");
while(1)
{
// Set up listening port and wait for incomming connection
tcp_listen(&fda,PORT,0,0,NULL,0);
sock_wait_established(&fda,0,NULL,&status);
printf("Receiving incoming connection\n");
sock_mode(&fda, TCP_MODE_ASCII);
while(tcp_tick(&fda))
{
// Block to read first byte from client, indicates start of test
printf("Waiting for Client to signal start of test\r\n");
n = sock_read( &fda, DataBuffer, 1 );
DataBuffer[n] = '\0';
printf( "DataBuffer: %s\r\n", DataBuffer );
// Transmit test from rabbit to pc
if ( DataBuffer[0] == 'T' )
{
printf("Starting Transmit Test\r\n");
BytesSent = 0;
dw1 = MS_TIMER;
while( BytesSent < NUM_OF_BYTES )
{
n = sock_write( &fda, DataBuffer, TCP_BUFFER_SEGMENTS * 1460 );
if ( n < 0 )
{
printf( "Error in sock_write(), %d\n", n );
break;
}
else
BytesSent += n;
}
dw2 = MS_TIMER;
printf( "Bytes Sent = %ld, MS_TIMER count: %ld\r\n", BytesSent, dw2-dw1 );
sock_close( &fda );
}
else if ( DataBuffer[0] == 'R' ) // Receive Test from pc to rabbit
{
BytesReceived = 0;
PrintableChars = 0;
dw1 = MS_TIMER;
while( BytesReceived < NUM_OF_BYTES )
{
n = sock_read( &fda, DataBuffer, TCP_BUFFER_SEGMENTS * 1460 );
if ( n < 0 )
{
break;
}
else
{
// Look at each byte and count printable characters
for ( i = 0; i < n; i++ )
{
if ( ( DataBuffer[i] >= ' ' ) && ( DataBuffer[i] < 128 ) )
PrintableChars++;
}
BytesReceived += n;
}
}
dw2 = MS_TIMER;
sock_close( &fda );
DataRate = (float)BytesReceived / (float)(dw2-dw1) * 1000.0 * 8.0 / 1000000.0;
printf("Bytes Received: %ld, MS_TIMER: %ld, Data Rate: %g, Printable Chars: %ld\r\n",
BytesReceived, dw2-dw1, DataRate, PrintableChars );
}
else
{
sock_close( &fda );
printf("Unrecognized test command: 0x%X, socket closed\n", DataBuffer[0]);
}
}
sock_err:
switch(status) {
case 1: /* foreign host closed */
printf("User closed session\n");
break;
case -1: /* time-out */
printf("\nConnection timed out\n");
break;
}
}
}