master_read_to_file.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /***************************************************************************
  2. master_read_to_file.c
  3. -------------------
  4. Example program which uses gpib c library. I use this with
  5. slave_write_from_file in order to test read/write speed between two boards.
  6. Unlike master_write_from_file, we don't use ibrdf() here because I want
  7. to separate gpib transfer speed and disk io speed in my benchmarking.
  8. copyright : (C) 2003 by Frank Mori Hess
  9. email : fmhess@users.sourceforge.net
  10. ***************************************************************************/
  11. /***************************************************************************
  12. * *
  13. * This program is free software; you can redistribute it and/or modify *
  14. * it under the terms of the GNU General Public License as published by *
  15. * the Free Software Foundation; either version 2 of the License, or *
  16. * (at your option) any later version. *
  17. * *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include "gpib/ib.h"
  27. int main( int argc, char *argv[] )
  28. {
  29. int dev;
  30. int board_index = 0;
  31. int pad = 1;
  32. int sad = 0;
  33. int send_eoi = 1;
  34. int eos_mode = 0;
  35. char *file_path;
  36. int status;
  37. struct timeval start_time, end_time;
  38. float elapsed_time;
  39. FILE *filep;
  40. uint8_t *buffer;
  41. static const unsigned long buffer_length = 10000000;
  42. if( argc < 2 )
  43. {
  44. fprintf( stderr, "Must provide file path as arguement\n" );
  45. return -1;
  46. }
  47. file_path = argv[ 1 ];
  48. filep = fopen( file_path, "w" );
  49. if( filep == NULL )
  50. {
  51. perror( "fopen()");
  52. return -1;
  53. }
  54. buffer = malloc( buffer_length );
  55. if( buffer == NULL )
  56. {
  57. perror( "malloc()");
  58. return -1;
  59. }
  60. dev = ibdev( board_index, pad, sad, TNONE, send_eoi, eos_mode );
  61. if( dev < 0 )
  62. {
  63. fprintf( stderr, "ibdev() failed\n" );
  64. fprintf( stderr, "%s\n", gpib_error_string( ThreadIberr() ) );
  65. return -1;
  66. }
  67. printf( "Device online: board index=%i, pad=%i, sad=%i\n"
  68. "\tfile path=%s\n", board_index, pad, sad, file_path );
  69. gettimeofday( &start_time, NULL );
  70. status = ibrd( dev, buffer, buffer_length );
  71. if( status & ERR )
  72. {
  73. fprintf( stderr, "ibrd() failed\n" );
  74. fprintf( stderr, "%s\n", gpib_error_string( ThreadIberr() ) );
  75. return -1;
  76. }
  77. gettimeofday( &end_time, NULL );
  78. elapsed_time = end_time.tv_sec - start_time.tv_sec +
  79. ( end_time.tv_usec - start_time.tv_usec ) / 1e6;
  80. printf( "Transferred %lu bytes in %g seconds: %g bytes/sec\n",
  81. ThreadIbcntl(), elapsed_time, ThreadIbcntl() / elapsed_time );
  82. if( fwrite( buffer, 1, ThreadIbcntl(), filep ) != ThreadIbcntl() )
  83. {
  84. perror( "fwrite()" );
  85. return -1;
  86. }
  87. fclose( filep );
  88. free( buffer );
  89. return 0;
  90. }