//------------------------------------------------------------------- // xmitajay.cpp // // This program is for testing our 'ajfschar.c' device-driver's // 'write()' method if an amount of transfer-data is specified. // // to compile: $ g++ xmitajay.cpp -o ajayxmit // to execute: $ ./amitajay // // NOTE: Installing our 'ajfschar.c' device-driver is required. // // programmer: ALLAN CRUSE // written on: 01 JUN 2010 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for write() char devname[] = "/dev/ajay"; char txbuf[ 41000 ]; int main( int argc, char **argv ) { // override default byte-count with a command-line argument int n = (argc > 1) ? atoi( argv[1] ) : 100; if ( n > 41000 ) { fprintf( stderr, "too big\n" ); exit(1); } printf( "\n Writing %d bytes to \'%s\' \n", n, devname ); // open the device-file for writing int fd = open( devname, O_WRONLY ); if ( fd < 0 ) { perror( devname ); exit(1); } // initialize the userspace data-buffer with ascii codes for (int i = 0; i < 41000; i++) { int q = i / 64; int r = i % 64; int ch = 'A' + (q % 32); if (( ch < 0x20 )||( ch > 0x7E )) ch = ','; if ( (r % 8) == 7 ) ch = ' '; if ( r == 63 ) ch = '\n'; txbuf[i] = ch; } // display the data to be transferred printf( "\n" ); for (int i = 0; i < n; i++) printf( "%c", txbuf[i] ); printf( "\n\n" ); // write the specified amount of data to the device-file int txbytes = write( fd, txbuf, n ); if ( txbytes < 0 ) { perror( "write" ); exit(1); } // report the amount of data that our driver has transferred printf( "\n OK, sent %d bytes to \'%s\' \n", txbytes, devname ); }