home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / cmanual-3.0.lha / CManual / Devices / SerialDevice / Example1.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  23KB  |  549 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Serial Device               Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-26                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This program demonstrates how you can use the Serial Device */
  23. /* to read and write information from and to the Serial Port.  */
  24. /* Since I do not know what you have connected to your serial  */
  25. /* port, this program does not do very much. However, the      */
  26. /* functions are easy to call, so it should not be hard for    */
  27. /* you to change this program into a nice serial communication */
  28. /* package.                                                    */
  29. /*                                                             */
  30. /* If you have a Sharp JX-100 scanner you can run this program */
  31. /* since it will try to turn the lamp on and then off again.   */
  32. /* Very useful! (hmmm...) The progarm does not check if there  */
  33. /* is any contact with the scanner, nor if the lamp really was */
  34. /* turned on or not.                                           */
  35.  
  36.  
  37.  
  38. #include <exec/types.h>        /* STRPTR         */
  39. #include <devices/serial.h>    /* Serial Device  */
  40.  
  41.  
  42.  
  43. /* Size of our data buffer: */
  44. #define MY_BUFFER_SIZE 200
  45.  
  46. /* Size of the Serial Device's own input buffer: (Must be */
  47. /* at least 512 bytes, but more is recommended.)          */
  48. #define INPUT_BUFFER_SIZE 1024
  49.  
  50. /* Read 8 bits/character: */
  51. #define READ_LENGTH   8
  52.  
  53. /* Write 8 bits/character: */
  54. #define WRITE_LENGTH  8
  55.  
  56. /* Sen 1 stop bit between each character: */
  57. #define STOP_LENGTH  1
  58.  
  59. /* Break time in microseconds: (1/2 sec) */
  60. #define BREAK_TIME    500000
  61.  
  62. /* Baud rate: */
  63. #define BAUD_RATE     9600
  64.  
  65. /* Serial flags: (Parity enabled, checking for end of file characters.) */
  66. #define SERIAL_FLAGS SERF_PARTY_ON|SERF_EOFMODE
  67.  
  68. /* Additional flags: (Mark-space parity not used.) */
  69. #define ADDITIONAL_FLAGS 0
  70.  
  71.  
  72.  
  73.  
  74.  
  75. /* Declare a pointer to our reply port: */
  76. struct MsgPort *replymp = NULL;
  77.  
  78. /* Declare a pointer to our serial request block: */
  79. struct IOExtSer *serial_req = NULL;
  80.  
  81. /* Store the serial device error here: */
  82. UWORD serial_dever = TRUE;
  83.  
  84. /* Declare our data buffer: */
  85. BYTE buffer[ MY_BUFFER_SIZE ];
  86.  
  87.  
  88.  
  89.  
  90. /* Declare our functions: */
  91.  
  92. /* Our main function: */
  93. void main();
  94.  
  95. /* Clears and removes everything nice and neatly: */
  96. void clean_up( UBYTE error, STRPTR text );
  97.  
  98. /* Sets serial parameters: */
  99. UBYTE SetSerParams(
  100.   struct IOExtSer *ioreq,
  101.   ULONG buffer_length,
  102.   ULONG baud_rate,
  103.   ULONG break_time,
  104.   UBYTE read_length,
  105.   UBYTE write_length,
  106.   UBYTE stop_length,
  107.   UBYTE serial_flags,
  108.   ULONG extended_flags,
  109.   UBYTE *eof_chars
  110. );
  111.  
  112. /* Prints some information about the error: */
  113. void SerError( UBYTE error );
  114.  
  115. /* Sends data to the Serial Port: */
  116. UBYTE SerWrite(
  117.   struct IOExtSer *ioreq,
  118.   BYTE *data,
  119.   ULONG length
  120. );
  121.  
  122. /* Reads data from the Serial Port: */
  123. UBYTE SerRead(
  124.   struct IOExtSer *ioreq,
  125.   BYTE *data,
  126.   ULONG length
  127. );
  128.  
  129.  
  130.  
  131. void main()
  132. {
  133.   /* Error number: */
  134.   UBYTE error;
  135.   
  136.   /* The eight end-of-file characters: */
  137.   /* They MUST be in descending order! */
  138.   UBYTE eof_char[8]={ 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00 };
  139.   
  140.   
  141.   
  142.   /* Get a reply port: (No name, priority 0) */
  143.   replymp = (struct MsgPort *)
  144.     CreatePort( NULL, 0 );
  145.   if( !replymp )
  146.     clean_up( 0, "Could not create the reply port!" );
  147.  
  148.  
  149.  
  150.   /* Create a serial request block: */
  151.   serial_req = (struct IOExtSer *)
  152.     CreateExtIO( replymp, sizeof( struct IOExtSer ) );
  153.   if( !serial_req )
  154.     clean_up( 0, "Not enough memory for the serial request block!" );
  155.  
  156.  
  157.  
  158.   /* Open the Serial Device: */
  159.   serial_dever = OpenDevice( SERIALNAME, 0, serial_req, 0 );
  160.   if( serial_dever )
  161.     clean_up( 0, "Could not open the Serial Device!" );
  162.  
  163.  
  164.  
  165.   error = (UBYTE) SetSerParams(
  166.     serial_req,        /* Pointer to our serial request block.          */
  167.     INPUT_BUFFER_SIZE, /* Size of the Serial Device's own input buffer. */
  168.     BAUD_RATE,         /* Baud rate (read and write). [112 - 292000]    */
  169.     BREAK_TIME,        /* Break time in microseconds.                   */
  170.     READ_LENGTH,       /* # of bits/char (1-8). Parity not included.    */
  171.     WRITE_LENGTH,      /* # of bits/char (1-8). Parity not included.    */
  172.     STOP_LENGTH,       /* # of stop bits between the characters. (1-2)  */
  173.     SERIAL_FLAGS,      /* Serial flags.                                 */
  174.     ADDITIONAL_FLAGS,  /* Additional flags.                             */
  175.     eof_char           /* Pointer to an array of eight end-of-file chr. */
  176.   );
  177.  
  178.   /* OK? */
  179.   if( error )
  180.     clean_up( error, "Could not set the serial parameters!" );
  181.  
  182.  
  183.  
  184.   /* These two bytes will turn on the lamp on a Sharp JX-100 */
  185.   /* color scanner, if it happens to be connected to your    */
  186.   /* serial port: (It is a nice scanner...)                  */ 
  187.   buffer[ 0 ] = 0x4C;
  188.   buffer[ 1 ] = 0x31;
  189.   
  190.   /* Send 2 bytes to the serial port: */
  191.   error = SerWrite( serial_req, buffer, 2 ); 
  192.   if( error )
  193.     SerError( error );       /* ERROR */
  194.   else
  195.     printf( "Lamp ON!\n" );  /* OK    */
  196.  
  197.  
  198.  
  199.   /* These two bytes will turn off the lamp on the scanner: */
  200.   buffer[ 0 ] = 0x4C;
  201.   buffer[ 1 ] = 0x30;
  202.   
  203.   /* Send 2 bytes to the serial port: */
  204.   error = SerWrite( serial_req, buffer, 2 ); 
  205.   if( error )
  206.     SerError( error );       /* ERROR */
  207.   else
  208.     printf( "Lamp OFF!\n" ); /* OK    */
  209.  
  210.  
  211.  
  212.   /* Clean up and quit: */
  213.   clean_up( 0, "The End!" );
  214. }
  215.  
  216.  
  217.  
  218. /* Close and return everything that has been */
  219. /* opened and allocated before we quit:      */
  220.  
  221. void clean_up( UBYTE error, STRPTR text )
  222. {
  223.   /* Print some information about the problem: */
  224.   if( error )
  225.     SerError( error );
  226.  
  227.   /* Close the Serial Device: */ 
  228.   if( !serial_dever )
  229.     CloseDevice( serial_req );
  230.  
  231.   /* Deallocate the serial request block: */
  232.   if( serial_req )
  233.     DeleteExtIO( serial_req, sizeof( struct IOExtSer ) );
  234.  
  235.   /* Remove the replyport: */
  236.   if( replymp )
  237.     DeletePort( replymp);
  238.  
  239.   /* Print the message: */
  240.   printf( "\n%s\n", text );
  241.  
  242.   /* Quit: */
  243.   exit( 0 );
  244. }
  245.  
  246.  
  247.  
  248. /* SetSerParams() sets the serial parameters. It initializes a IOExtSer    */
  249. /* structure, and does a SDCMD_SETPARAMS commad. If everything is OK it    */
  250. /* returns NULL, else an error number is returned.                         */
  251. /*                                                                         */
  252. /* Synopsis: er = SetSerParams( io, bl, br, bt, rl, wl, sl, sf, ef, chr ); */
  253. /*                                                                         */
  254. /* er:       (UBYTE) SetSerParams() returns 0 if everything was OK, else   */
  255. /*           an error value is returned. See function SerError() for more  */
  256. /*           information.                                                  */
  257. /*                                                                         */ 
  258. /* io:       (struct IOExtSer *) Pointer to the serial request block you   */
  259. /*           want to initialize.                                           */
  260. /*                                                                         */
  261. /* bl:       (ULONG) Size of the internal serial buffer which will be used */
  262. /*           when you read data. Must be at least 512 (bytes), but more is */
  263. /*           recommended. The faster and more data you want to read, the   */
  264. /*           bigge