home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / cmanual-3.0.lha / CManual / Devices / SerialDevice / Example2.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  23KB  |  561 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:    Example2.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. /* This example is rather similar to Example 1, but this time  */
  31. /* we do not wait for the serial port to complete our request. */
  32. /* Instead we do somethings (well not very much) and now and   */
  33. /* then checks if the request has been completed.              */
  34.  
  35.  
  36.  
  37. #include <exec/types.h>        /* STRPTR         */
  38. #include <devices/serial.h>    /* Serial Device  */
  39.  
  40.  
  41.  
  42. /* Size of our data buffer: */
  43. #define MY_BUFFER_SIZE 200
  44.  
  45. /* Size of the Serial Device's own input buffer: (Must be */
  46. /* at least 512 bytes, but more is recommended.)          */
  47. #define INPUT_BUFFER_SIZE 1024
  48.  
  49. /* Read 8 bits/character: */
  50. #define READ_LENGTH   8
  51.  
  52. /* Write 8 bits/character: */
  53. #define WRITE_LENGTH  8
  54.  
  55. /* Sen 1 stop bit between each character: */
  56. #define STOP_LENGTH  1
  57.  
  58. /* Break time in microseconds: (1/2 sec) */
  59. #define BREAK_TIME    500000
  60.  
  61. /* Baud rate: */
  62. #define BAUD_RATE     9600
  63.  
  64. /* Serial flags: (Parity enabled, checking for end of file characters.) */
  65. #define SERIAL_FLAGS SERF_PARTY_ON|SERF_EOFMODE
  66.  
  67. /* Additional flags: (Mark-space parity not used.) */
  68. #define ADDITIONAL_FLAGS 0
  69.  
  70.  
  71.  
  72.  
  73.  
  74. /* Declare a pointer to our reply port: */
  75. struct MsgPort *replymp = NULL;
  76.  
  77. /* Declare a pointer to our serial request block: */
  78. struct IOExtSer *serial_req = NULL;
  79.  
  80. /* Store the serial device error here: */
  81. UWORD serial_dever = TRUE;
  82.  
  83. /* Declare our data buffer: */
  84. BYTE buffer[ MY_BUFFER_SIZE ];
  85.  
  86.  
  87.  
  88.  
  89. /* Declare our functions: */
  90.  
  91. /* Our main function: */
  92. void main();
  93.  
  94. /* Clears and removes everything nice and neatly: */
  95. void clean_up( UBYTE error, STRPTR text );
  96.  
  97. /* Sets serial parameters: */
  98. UBYTE SetSerParams(
  99.   struct IOExtSer *ioreq,
  100.   ULONG buffer_length,
  101.   ULONG baud_rate,
  102.   ULONG break_time,
  103.   UBYTE read_length,
  104.   UBYTE write_length,
  105.   UBYTE stop_length,
  106.   UBYTE serial_flags,
  107.   ULONG extended_flags,
  108.   UBYTE *eof_chars
  109. );
  110.  
  111. /* Prints some information about the error: */
  112. void SerError( UBYTE error );
  113.  
  114. /* Sends data to the Serial Port without going to sleep: */
  115. void SerWriteNoWait(
  116.   struct IOExtSer *ioreq,
  117.   BYTE *data,
  118.   ULONG length
  119. );
  120.  
  121. /* Reads data from the Serial Port without going to sleep: */
  122. void SerReadNoWait(
  123.   struct IOExtSer *ioreq,
  124.   BYTE *data,
  125.   ULONG length
  126. );
  127.  
  128.  
  129.  
  130. void main()
  131. {
  132.   /* Error number: */
  133.   UBYTE error;
  134.   
  135.   /* The eight end-of-file characters: */
  136.   /* They MUST be in descending order! */
  137.   UBYTE eof_char[8]={ 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00 };
  138.  
  139.   /* Declare a pointer and set it to NULL: */
  140.   struct IOExtSer *ptr = NULL;
  141.   
  142.   
  143.   
  144.   /* Get a reply port: (No name, priority 0) */
  145.   replymp = (struct MsgPort *)
  146.     CreatePort( NULL, 0 );
  147.   if( !replymp )
  148.     clean_up( 0, "Could not create the reply port!" );
  149.  
  150.  
  151.  
  152.   /* Create a serial request block: */
  153.   serial_req = (struct IOExtSer *)
  154.     CreateExtIO( replymp, sizeof( struct IOExtSer ) );
  155.   if( !serial_req )
  156.     clean_up( 0, "Not enough memory for the serial request block!" );
  157.  
  158.  
  159.  
  160.   /* Open the Serial Device: */
  161.   serial_dever = OpenDevice( SERIALNAME, 0, serial_req, 0 );
  162.   if( serial_dever )
  163.     clean_up( 0, "Could not open the Serial Device!" );
  164.  
  165.  
  166.  
  167.   error = (UBYTE) SetSerParams(
  168.     serial_req,        /* Pointer to our serial request block.          */
  169.     INPUT_BUFFER_SIZE, /* Size of the Serial Device's own input buffer. */
  170.     BAUD_RATE,         /* Baud rate (read and write). [112 - 292000]    */
  171.     BREAK_TIME,        /* Break time in microseconds.                   */
  172.     READ_LENGTH,       /* # of bits/char (1-8). Parity not included.    */
  173.     WRITE_LENGTH,      /* # of bits/char (1-8). Parity not included.    */
  174.     STOP_LENGTH,       /* # of stop bits between the characters. (1-2)  */
  175.     SERIAL_FLAGS,      /* Serial flags.                                 */
  176.     ADDITIONAL_FLAGS,  /* Additional flags.                             */
  177.     eof_char           /* Pointer to an array of eight end-of-file chr. */
  178.   );
  179.  
  180.   /* OK? */
  181.   if( error )
  182.     clean_up( error, "Could not set the serial parameters!" );
  183.  
  184.  
  185.  
  186.   /* These two bytes will turn on the lamp on a Sharp JX-100 */
  187.   /* color scanner, if it happens to be connected to your    */
  188.   /* serial port: (To make this example simple we do not     */
  189.   /* turn it off.)                                           */ 
  190.   buffer[ 0 ] = 0x4C;
  191.   buffer[ 1 ] = 0x31;
  192.   
  193.  
  194.   /* Send 2 bytes to the serial port and return immediately: */
  195.   SerWriteNoWait( serial_req, buffer, 2 ); 
  196.  
  197.  
  198.   /* As long as the pointer is not pointing to */
  199.   /* the request we should stay in the loop:   */
  200.   while( ptr == NULL )
  201.   {
  202.     /*   ... do something ...    */
  203.     /* Well, I do not know what. */
  204.   
  205.     /* Check if the request has been completed: (If the  */
  206.     /* request has been compleded CheckIO() will return  */
  207.     /* a pointer to the request, else NULL is returned.) */
  208.     ptr = (struct IOExtSer *) CheckIO( serial_req );
  209.   }
  210.  
  211.   /* At last the request was completed! */
  212.  
  213.  
  214.   /* Remove the requstblock's message. (The ptr and ioreq */
  215.   /* are in this example identical, so it does not matter */
  216.   /* whichever you will use. The paranteces around the    */
  217.   /* expression is actually unnecessary, but this looks   */
  218.   /* better.)                                             */
  219.   Remove( &(ptr->IOSer.io_Message.mn_Node) );
  220.  
  221.  
  222.   /* Check if everything is OK? */
  223.   if( serial_req->IOSer.io_Error )
  224.     SerError( error );       /* ERROR */
  225.   else
  226.     printf( "Lamp OFF!\n" ); /* OK    */
  227.  
  228.  
  229.  
  230.   /* Clean up and quit: */
  231.   clean_up( 0, "The End!" );
  232. }
  233.  
  234.  
  235.  
  236. /* Close and return everything that has been */
  237. /* opened and allocated before we quit:      */
  238.  
  239. void clean_up( UBYTE error, STRPTR text )
  240. {
  241.   /* Print some information about the problem: */
  242.   if( error )
  243.     SerError( error );
  244.  
  245.   /* Close the Serial Device: */ 
  246.   if( !serial_dever )
  247.     CloseDevice( serial_req );
  248.  
  249.   /* Deallocate the serial request block: */
  250.   if( serial_req )
  251.     DeleteExtIO( serial_req, sizeof( struct IOExtSer ) );
  252.  
  253.   /* Remove the replyport: */
  254.   if( replymp )
  255.     DeletePort( replymp);
  256.  
  257.   /* Print the message: */
  258.   printf( "\n%s\n", text );
  259.  
  260.   /* Quit: */
  261.   exit( 0 );
  262. }
  263.  
  264.  
  265.  
  266. /* SetSerParams() sets the serial parameters. It initializes a IOExtSer    */
  267. /* structure, and does a SDCMD_SETPARAMS commad. If everything is OK it    */
  268. /* returns NULL, else an error number is returned.                         */
  269. /*                                                                         */
  270. /* Synopsis: er = SetSerParams( io, bl, br, bt, rl, wl, sl, sf, ef, chr ); */
  271. /*                                                                         */
  272. /* er:       (UBYTE) SetSerParams() returns 0 if eve