home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / devices / serialdevice / example4.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  34KB  |  758 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:    Example4.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 example does not do anything, but it consists of    */
  23. /* several useful functions that you can use yourself after */
  24. /* small modifications. The functions demonstrates all      */
  25. /* commands there exist for the serial device, so if you    */
  26. /* had problems in understanding how a command was used you */
  27. /* can look here.                                           */
  28.  
  29.  
  30.  
  31. #include <exec/types.h>
  32. #include <exec/errors.h>
  33. #include <devices/serial.h>
  34.  
  35.  
  36.  
  37. /* Declare the functions: */
  38.  
  39. UBYTE SetSerParams(
  40.   struct IOExtSer *ioreq, /* Pointer to our serial request block.          */
  41.   ULONG buffer_length,    /* Size of the Serial Device's own input buffer. */
  42.   ULONG baud_rate,        /* Baud rate (read and write). [110 - 292000]    */
  43.   ULONG break_time,       /* Break time in microseconds.                   */
  44.   UBYTE read_length,      /* Nr of bits, read (1-8). Parity not included.  */
  45.   UBYTE write_length,     /* Nr of bits, write (1-8). Parity not included. */
  46.   UBYTE stop_length,      /* Nr of bits, stop (1 or 2).                    */
  47.   UBYTE serial_flags,     /* Serial flags.                                 */
  48.   ULONG extended_flags,   /* Additional serial flags.                      */
  49.   UBYTE *eof_chars        /* Pointer to an array containing eight end-of-  */
  50.                           /* file characters.                              */
  51. );
  52.  
  53. void SerError( UBYTE error );
  54.  
  55. UBYTE SerWrite(
  56.   struct IOExtSer *ioreq, /* Pointer to our serial request block.     */
  57.   BYTE *data,             /* Pointer to the data you want to send.    */
  58.   ULONG length            /* The length of the data you want to send. */
  59. );
  60.  
  61. UBYTE SerRead(
  62.   struct IOExtSer *ioreq, /* Pointer to our serial request block. */
  63.   BYTE *data,             /* Where the data should be placed.     */
  64.   ULONG length            /* How many bytes you want to read.     */
  65. );
  66.  
  67. void SerWriteNoWait(
  68.   struct IOExtSer *ioreq, /* Pointer to our serial request block.     */
  69.   BYTE *data,             /* Pointer to the data you want to send.    */
  70.   ULONG length            /* The length of the data you want to send. */
  71. );
  72.  
  73. void SerReadNoWait(
  74.   struct IOExtSer *ioreq, /* Pointer to our serial request block. */
  75.   BYTE *data,             /* Where the data should be placed.     */
  76.   ULONG length            /* How many bytes you want to read.     */
  77. );
  78.  
  79. UBYTE SerBreak( struct IOExtSer *ioreq );
  80.  
  81. UBYTE SerClear( struct IOExtSer *ioreq );
  82.  
  83. UBYTE SerFlush( struct IOExtSer *ioreq );
  84.  
  85. UBYTE SerQuery( struct IOExtSer *ioreq );
  86.  
  87. UBYTE SerReset( struct IOExtSer *ioreq );
  88.  
  89. UBYTE SerStop( struct IOExtSer *ioreq );
  90.  
  91. UBYTE SerStart( struct IOExtSer *ioreq );
  92.  
  93.  
  94.  
  95. /* Very short main() module: */
  96. void main();
  97.  
  98. void main()
  99. {
  100.   printf( "See source code for more information..." );
  101. }
  102.  
  103.  
  104.  
  105. /*************************************/
  106. /* SERIAL DEVICE - SUPPORT FUNCTIONS */
  107. /*************************************/
  108.  
  109.  
  110.  
  111. /* SetSerParams() sets the serial parameters. It initializes a IOExtSer    */
  112. /* structure, and does a SDCMD_SETPARAMS commad. If everything is OK it    */
  113. /* returns NULL, else an error number is returned.                         */
  114. /*                                                                         */
  115. /* Synopsis: er = SetSerParams( io, bl, br, bt, rl, wl, sl, sf, ef, chr ); */
  116. /*                                                                         */
  117. /* er:       (UBYTE) SetSerParams() returns 0 if everything was OK, else   */
  118. /*           an error value is returned. See function SerError() for more  */
  119. /*           information.                                                  */
  120. /*                                                                         */ 
  121. /* io:       (struct IOExtSer *) Pointer to the serial request block you   */
  122. /*           want to initialize.                                           */
  123. /*                                                                         */
  124. /* bl:       (ULONG) Size of the internal serial buffer which will be used */
  125. /*           when you read data. Must be at least 512 (bytes), but more is */
  126. /*           recommended. The faster and more data you want to read, the   */
  127. /*           bigger should the internal buffer be. Some recommended sizes: */
  128. /*           512, 1024, 2048, 4096, 8192 or 16384.                         */
  129. /*                                                                         */
  130. /* br:       (ULONG) Baud rate. Can be anything between 110 and 292000.    */
  131. /*           (Up to 292000 is all right for the hardware, but the software */
  132. /*           can not cope with this, especially since other tasks may be   */
  133. /*           running at the same time. You should therefore not use baud   */
  134. /*           rates above 31250.) Some recommended values: 110, 300, 1200,  */
  135. /*           2400, 4800, 9600, 19200 or 31250 (the last is a bit though).  */
  136. /*                                                                         */
  137. /* bt:       (ULONG) Break time in micro seconds. All break requests will  */
  138. /*           be set to this time.                                          */
  139. /*                                                                         */
  140. /* rl:       (UBYTE) How many bits chould be read for each character.      */
  141. /*           Usually 7 or 8 bits.                                          */
  142. /*                                                                         */
  143. /* wl:       (UBYTE) How many bits chould be written for each character.   */
  144. /*           Usually 7 or 8 bits.                                          */
  145. /*                                                                         */
  146. /* sl:       (UBYTE) How many stop bits shoud be written or expected.      */
  147. /*           Normally set to 1, but you may set it to 2 if rl/wl = 7.      */
  148. /*                                                                         */
  149. /* sf:       (UBYTE) You may use the following serial flags:               */
  150. /*                                                                         */
  151. /*           SERF_PARTY_ON   Parity checking/writing is turned on. (The    */
  152. /*                           sum of all data bits are divided by two, and  */
  153. /*                           the remainder is the parity bit. If even      */
  154. /*                           parity is used the bit will be set to 1 if    */
  155. /*                           the remainder is even. If odd parity is used  */
  156. /*                           the parity bit will be set to 0 if the        */
  157. /*                           remainder is even.                            */
  158. /*                                                                         */
  159. /*           SERF_PARTY_ODD  Set this flag if you want to use odd parity.  */
  160. /*                           (The default setting is even parity.)         */
  161. /*                                                                         */
  162. /*           SERF_7WIRE      This flag should only be used when you call   */
  163. /*                           the OpenDevice(), and not by this function.   */
  164. /*                           If the flag is set, seven-wire "handshaking"  */
  165. /*                           will be used. (Default is three-wire.)        */
  166. /*                                                                         */
  167. /*           SERF_QUEUEDBRK  Set this flag if you want break commands to   */
  168. /*                           be queued along with all other signals. The   */
  169. /*                           default is that a break command interrupts    */
  170. /*                           the process immediately.                      */
  171. /*                                                                         */
  172. /*           SERF_RAD_BOOGIE Set this bit if you want high speed mode.     */
  173. /*                           This can be useful when you want to send and  */
  174. /*                           receive signals at high speed. When this flag */
  175. /*                           is set no parity is used, xON/xOFF handling   */
  176. /*                           is turned off, no break signals are allowed,  */
  177. /*                           and finally only eight-bit characters are     */
  178. /*                           used.                                         */
  179. /*                                                                         */
  180. /*           SERF_SHARED     Set this falg if you want to allow other      */
  181. /*                           tasks running at the same time to use the     */
  182. /*                           serial device. The default is exclusive-      */
  183. /*                           access. (If some other task is using the      */
  184. /*                           serial device with the shared bit set, and    */
  185. /*                           you call this function with exclusive access, */
  186. /*                           your request will fail.)                      */
  187. /*                                                                         */
  188. /*           SERF_EOFMODE    Set this flag if you want to check for end of */
  189. /*                           file characters. (You may use up to eight end */
  190. /*                           of file characters, which are specified       */
  191. /*                           below.)                                       */
  192. /*                                                                         */
  193. /*           SERF_XDISABLED  xOn/xOFF handling is turned off. (Default is  */
  194. /*                           on.)                                          */
  195. /*                                                                         */
  196. /* ef:       (ULONG) You may use the following extra flags:                */
  197. /*                                                                         */
  198. /*           SEXTF_MSPON     Set this flag if you want to use mark-space   */
  199. /*                           parity rather than odd-even parity.           */
  200. /*                                                                         */
  201. /*           SEXTF_MARK      If this and the SEXTF_MSPON flag is set, it   */
  202. /*                           will mark.                                    */
  203. /*                                                                         */
  204. /* chr:      (UBYTE *) Pointer to an array containing eight end of file    */
  205. /*           characters. If the serial flag "SERF_EOFMODE" is set, the     */
  206. /*           serial device will check each character which is sent or      */
  207. /*           received, and if it matches one of the end of file characters */
  208. /*           the read/wite request is terminated.                          */
  209.  
  210. UBYTE SetSerParams(
  211.   struct IOExtSer *ioreq, /* Pointer to our serial request block.          */
  212.   ULONG buffer_length,    /* Size of the Serial Device's own input buffer. */
  213.   ULONG baud_rate,        /* Baud rate (read and write). [110 - 292000]    */
  214.   ULONG break_time,       /* Break time in microseconds.                   */
  215.   UBYTE read_length,      /* Nr of bits, read (1-8). Parity not included.  */
  216.   UBYTE write_length,     /* Nr of bits, write (1-8). Parity not included. */
  217.   UBYTE stop_length,      /* Nr of bits, stop (1 or 2).                    */
  218.   UBYTE serial_flags,     /* Serial flags.                                 */
  219.   ULONG extended_flags,   /* Additional serial flags.                      */
  220.   UBYTE *eof_chars        /* Pointer to an array containing eight end-of-  */
  221.                           /* file characters.                              */
  222. )
  223. {
  224.   int loop;           /* Used in the loop.      */
  225.   UBYTE *ptr;         /* Unsigned byte pointer. */
  226.   
  227.   
  228.   /* Set the size of the Serial Device's own input buffer: */
  229.   ioreq->io_RBufLen = buffer_length;
  230.   
  231.   /* Set baud rate: */
  232.   ioreq->io_Baud = baud_rate;
  233.   
  234.   /* Set break time (in microseconds): */
  235.   ioreq->io_BrkTime = break_time;  
  236.   
  237.   /* Nr of bits to read per character: */
  238.   ioreq->io_ReadLen = read_length;
  239.  
  240.   /* Nr of bits to write per character: */
  241.   ioreq->io_WriteLen = write_length;
  242.   
  243.   /* Nr of stop bits: (Normally 1, if write_length is */
  244.   /* equal to 7 you may set stop_length to 2.)        */
  245.   ioreq->io_StopBits = stop_length;
  246.  
  247.   /* Set serial flags: */
  248.   ioreq->io_SerFlags = serial_flags;
  249.  
  250.   /* Set additional flags: */
  251.   ioreq->io_ExtFlags = extended_flags;
  252.   
  253.   
  254.   /* Get the address of the IOTArray: */
  255.   ptr = (UBYTE *) &(ioreq->io_TermArray);
  256.  
  257.   /* Set all eight end of file characters: */
  258.   for( loop=0; loop < 8; loop++ )
  259.   {
  260.     /* Copy character after character: */
  261.     *ptr = eof_chars[ loop ];
  262.  
  263.     /* Step one byte foreward: */
  264.     ptr++;
  265.   }
  266.   
  267.   
  268.   /* All values have now been set, lets do a SDCMD_SETPARAMS request: */
  269.   ioreq->IOSer.io_Command = SDCMD_SETPARAMS;
  270.   
  271.   /* Do our request, and when complete return 0 if */
  272.   /* OK, else an error value:                      */
  273.   return( (UBYTE) DoIO( ioreq ) );
  274. }
  275.  
  276.  
  277.  
  278. /* SerError() tells the user what went wrong. You give it the error code */
  279. /* you received, and SerError() will print a short description of the    */
  280. /* problem. Useful when debugging.                                       */
  281. /*                                                                       */
  282. /* Synopsis: SerError( error );                                          */
  283. /*                                                                       */
  284. /* error:    (UBYTE) The error value you want to have explained.         */
  285.  
  286. void SerError( UBYTE error )
  287. {
  288.   switch( error )
  289.   {
  290.     /* The serial device: */
  291.     case SerErr_DevBusy:
  292.       printf( "Some other task is already using the Serial Device!\n" );
  293.       break;
  294.     case SerErr_BufErr:
  295.       printf( "Not enough memory for the new input buffer!\n" );
  296.       break;
  297.     case SerErr_InvParam:
  298.       printf( "Invalid parameters!\n" );
  299.       break;
  300.     case SerErr_LineErr:
  301.       printf( "Line error!\n" );
  302.       break;
  303.     case SerErr_ParityErr:
  304.       printf( "Problems with the parity!\n" );
  305.       break;
  306.     case SerErr_TimerErr:
  307.       printf( "Timer error!\n" );
  308.       break;
  309.     case SerErr_BufOverflow:
  310.       printf( "Buffer overflowed!\n" );
  311.       break;
  312.     case SerErr_NoDSR:
  313.       printf( "No DSR!\n" );
  314.       break;
  315.     case SerErr_DetectedBreak:
  316.       printf( "A break was detected!\n" );
  317.       break;
  318.  
  319.     /* Exec: */
  320.     case IOERR_OPENFAIL:
  321.       printf( "The device could not be opened!\n" );
  322.       break;
  323.     case IOERR_ABORTED:
  324.       printf( "The request was aborted!\n" );
  325.       break;
  326.     case IOERR_NOCMD:
  327.       printf( "The serial device does not know about this command!\n" );
  328.       break;
  329.     case IOERR_BADLENGTH:
  330.       printf( "The length of the request was not valid!\n" );
  331.       break;
  332.  
  333.     /* Unknown error: */
  334.     default:
  335.       printf( "Unknown error! Error code: %d\n", error );
  336.   }
  337. }
  338.  
  339.  
  340.  
  341. /* SerWrite() sends some data to the Serial Port. You only have to */
  342. /* give it a pointer to the data you want to write and tell it how */
  343. /* many bytes you want to transfer.                                */
  344. /*                                                                 */
  345. /* Synopsis: error = SerWrite( io, data, length );                 */
  346. /*                                                                 */
  347. /* error:    (UBYTE) SerWrite() returns 0 if everything was OK,    */
  348. /*           else an error number is returned.                     */
  349. /*                                                                 */
  350. /* io:       (struct IOExtSer *) Pointer to an initialized serial  */
  351. /*           request block.                                        */
  352. /*                                                                 */
  353. /* data:     (BYTE *) Pointer to the first byte of the data you    */
  354. /*           want to send (write).                                 */
  355. /*                                                                 */
  356. /* length:   (ULONG) How many bytes you want to transfer. If you   */
  357. /*           want to continue to send data until we have received  */
  358. /*           an end-of-file character, set the length to -1. (Note */
  359. /*           that it will then ONLY stop when it receives one of   */
  360. /*           the end-of-file characters.)                          */
  361.  
  362. UBYTE SerWrite(
  363.   struct IOExtSer *ioreq, /* Pointer to our serial request block.     */
  364.   BYTE *data,             /* Pointer to the data you want to send.    */
  365.   ULONG length            /* The length of the data you want to send. */
  366. )
  367. {
  368.   /* We want to send (write) some data: */
  369.   ioreq->IOSer.io_Command = CMD_WRITE;
  370.  
  371.   /* Give the start address of our data: */
  372.   ioreq->IOSer.io_Data = (APTR) data;
  373.  
  374.   /* Set the length of the message: (If you want to continue    */
  375.   /* to write until you have received an end-of-file character, */
  376.   /* set length to -1.)                                         */
  377.   ioreq->IOSer.io_Length = length;
  378.  
  379.   /* Do our request, and return 0 if everything is OK, else */
  380.   /* return an error number:                                */
  381.   return( (UBYTE) DoIO( ioreq ) );
  382. }
  383.  
  384.  
  385.  
  386. /* SerRead() reads some data from the Serial Port. You only have to  */
  387. /* give it a pointer to some memory where the data should be stored, */
  388. /* and tell it how many bytes you want to read. The rest is done     */
  389. /* automatically.                                                    */
  390. /*                                                                   */
  391. /* Synopsis: error = SerRead( io, data, length );                    */
  392. /*                                                                   */
  393. /* error:    (UBYTE) SerRead() returns 0 if everything was OK, eles  */
  394. /*           an error number is returned.                            */
  395. /*                                                                   */
  396. /* io:       (struct IOExtSer *) Pointer to an initialized serial    */
  397. /*           request block.                                          */
  398. /*                                                                   */
  399. /* data:     (BYTE *) Pointer to the memory buffer where you want to */
  400. /*           store all data.                                         */
  401. /*                                                                   */
  402. /* length:   (ULONG) How many bytes you want to read. If you want to */
  403. /*           continue to send data until we have received an end-of- */
  404. /*           file character, set the length to -1. (Note that it     */
  405. /*           will then ONLY stop when it receives one of the end-of- */
  406. /*           file characters.)                                       */
  407.  
  408. UBYTE SerRead(
  409.   struct IOExtSer *ioreq, /* Pointer to our serial request block. */
  410.   BYTE *data,             /* Where the data should be placed.     */
  411.   ULONG length            /* How many bytes you want to read.     */
  412. )
  413. {
  414.   /* We want to read some data: */
  415.   ioreq->IOSer.io_Command = CMD_READ;
  416.  
  417.   /* Give the start address of our data: */
  418.   ioreq->IOSer.io_Data = (APTR) data;
  419.  
  420.   /* Set how many bytes you want to read. (If you want to continue  */
  421.   /* to read data until you have received an end-of-file character, */
  422.   /* set length to -1.)                                             */
  423.   ioreq->IOSer.io_Length = length;
  424.  
  425.   /* Do our request, and return 0 if everything is OK, else */
  426.   /* return an error number:                                */
  427.   return( (UBYTE) DoIO( ioreq ) );
  428. }
  429.  
  430.  
  431.  
  432. /* SerWriteNoWait() sends some data to the Serial Port, but returns */
  433. /* immediately. You only have to give it a pointer to the data you  */
  434. /* want to write and tell it how many bytes you want to transfer.   */
  435. /* Since it does not wait for the request to be completed, you have */
  436. /* to take care of removing the message yourself. Note that all     */
  437. /* requests that have been started must be completed or aborted     */
  438. /* before your program may close the serial device.                 */
  439. /*                                                                  */
  440. /* Synopsis: error = SerWriteNoWait( io, data, length );            */
  441. /*                                                                  */
  442. /* io:       (struct IOExtSer *) Pointer to an initialized serial   */
  443. /*           request block.                                         */
  444. /*                                                                  */
  445. /* data:     (BYTE *) Pointer to the first byte of the data you     */
  446. /*           want to send (write).                                  */
  447. /*                                                                  */
  448. /* length:   (ULONG) How many bytes you want to transfer. If you    */
  449. /*           want to continue to send data until we have received   */
  450. /*           an end-of-file character, set the length to -1. (Note  */
  451. /*           that it will then ONLY stop when it receives one of    */
  452. /*           the end-of-file characters.)                           */
  453.  
  454. void SerWriteNoWait(
  455.   struct IOExtSer *ioreq, /* Pointer to our serial request block.     */
  456.   BYTE *data,             /* Pointer to the data you want to send.    */
  457.   ULONG length            /* The length of the data you want to send. */
  458. )
  459. {
  460.   /* We want to send (write) some data: */
  461.   ioreq->IOSer.io_Command = CMD_WRITE;
  462.  
  463.   /* Give the start address of our data: */
  464.   ioreq->IOSer.io_Data = (APTR) data;
  465.  
  466.   /* Set the length of the message: */
  467.   ioreq->IOSer.io_Length = length;
  468.  
  469.   /* Do our request and return immediately: */
  470.   SendIO( ioreq );
  471. }
  472.  
  473.  
  474.  
  475. /* SerReadNoWait() reads some data from the Serial Port, but returns  */
  476. /* immediately. You only have to give it a pointer to some memory     */
  477. /* where the data should be stored, and tell it how many bytes you    */
  478. /* want to read. Since it does not wait for the request to be         */
  479. /* completed, you have to take care of removing the message yourself. */
  480. /* Note that all requests that have been started must be completed or */
  481. /* aborted before your program may close the serial device.           */
  482. /*                                                                    */
  483. /*                                                                    */
  484. /* Synopsis: error = SerReadNoWait( io, data, length );               */
  485. /*                                                                    */
  486. /* io:       (struct IOExtSer *) Pointer to an initialized serial     */
  487. /*           request block.                                           */
  488. /*                                                                    */
  489. /* data:     (BYTE *) Pointer to the memory buffer where you want to  */
  490. /*           store all data.                                          */
  491. /*                                                                    */
  492. /* length:   (ULONG) How many bytes you want to read. If you want to  */
  493. /*           continue to send data until we have received an end-of-  */
  494. /*           file character, set the length to -1. (Note that it      */
  495. /*           will then ONLY stop when it receives one of the end-of-  */
  496. /*           file characters.)                                        */
  497.  
  498. void SerReadNoWait(
  499.   struct IOExtSer *ioreq, /* Pointer to our serial request block. */
  500.   BYTE *data,             /* Where the data should be placed.     */
  501.   ULONG length            /* How many bytes you want to read.     */
  502. )
  503. {
  504.   /* We want to read some data: */
  505.   ioreq->IOSer.io_Command = CMD_READ;
  506.  
  507.   /* Give the start address of our data: */
  508.   ioreq->IOSer.io_Data = (APTR) data;
  509.  
  510.   /* Set how many bytes you want to read: */
  511.   ioreq->IOSer.io_Length = length;
  512.  
  513.   /* Do our request and return immediately: */
  514.   DoIO( ioreq );
  515. }
  516.  
  517.  
  518.  
  519. /* SerBreak() will send a break signal to the serial device.   */
  520. /* The default is that all serial requests are immediately     */
  521. /* halted, but if the serial flag "SERF_QUEUEDBRK" is set, the */
  522. /* break command will be queued as all other requests and the  */
  523. /* device will first take a break when all previous requests   */
  524. /* have been completed.                                        */
  525. /*                                                             */
  526. /* The serial device will normally take a 250000 microseconds  */
  527. /* (1/4 seconds) long break, but you may change this break     */
  528. /* time by altering the serial device's "io_BrkTime" parameter */
  529. /* (see function SetSerParams()).                              */
  530. /*                                                             */
  531. /* Synopsis: error = SerBreak( io );                           */
  532. /*                                                             */
  533. /* error:    (UBYTE) The function returns 0 if everything was  */
  534. /*           OK, else an error number is returned.             */
  535. /*                                                             */
  536. /* io:       (struct IOExtSer *) Pointer to an initialized     */
  537. /*           serial request block.                             */
  538.  
  539. UBYTE SerBreak( struct IOExtSer *ioreq )
  540. {
  541.   /* We want to take a puse: */
  542.   ioreq->IOSer.io_Command = SDCMD_BREAK;
  543.  
  544.   /* Do our request, and return 0 if everything is OK, else */
  545.   /* return an error number:                                */
  546.   return( (UBYTE) DoIO( ioreq ) );
  547. }
  548.  
  549.  
  550.  
  551. /* SerClear() will clear the internal input buffer.            */
  552. /*                                                             */
  553. /* Synopsis: error = SerClear( io );                           */
  554. /*                                                             */
  555. /* error:    (UBYTE) The function returns 0 if everything was  */
  556. /*           OK, else an error number is returned.             */
  557. /*                                                             */
  558. /* io:       (struct IOExtSer *) Pointer to an initialized     */
  559. /*           serial request block.                             */
  560.  
  561. UBYTE SerClear( struct IOExtSer *ioreq )
  562. {
  563.   /* We want to clear the input buffer: */
  564.   ioreq->IOSer.io_Command = CMD_CLEAR;
  565.  
  566.   /* Do our request, and return 0 if everything is OK, else */
  567.   /* return an error number:                                */
  568.   return( (UBYTE) DoIO( ioreq ) );
  569. }
  570.  
  571.  
  572.  
  573. /* SerFlush() will remove all queued commands.                 */
  574. /*                                                             */
  575. /* Synopsis: error = SerFlush( io );                           */
  576. /*                                                             */
  577. /* error:    (UBYTE) The function returns 0 if everything was  */
  578. /*           OK, else an error number is returned.             */
  579. /*                                                             */
  580. /* io:       (struct IOExtSer *) Pointer to an initialized     */
  581. /*           serial request block.                             */
  582.  
  583. UBYTE SerFlush( struct IOExtSer *ioreq )
  584. {
  585.   /* We want to remove all queued requests: */
  586.   ioreq->IOSer.io_Command = CMD_FLUSH;
  587.  
  588.   /* Do our request, and return 0 if everything is OK, else */
  589.   /* return an error number:                                */
  590.   return( (UBYTE) DoIO( ioreq ) );
  591. }
  592.  
  593.  
  594.  
  595. /* SerQuery() will print some information about the serial     */
  596. /* device. This is very useful for debugging.                  */
  597. /*                                                             */
  598. /* Synopsis: error = SerQuery( io );                           */
  599. /*                                                             */
  600. /* error:    (UBYTE) The function returns 0 if everything was  */
  601. /*           OK, else an error number is returned.             */
  602. /*                                                             */
  603. /* io:       (struct IOExtSer *) Pointer to an initialized     */
  604. /*           serial request block.                             */
  605.  
  606. UBYTE SerQuery( struct IOExtSer *ioreq )
  607. {
  608.   UBYTE error;
  609.  
  610.  
  611.   /* We want to check the serial device: */
  612.   ioreq->IOSer.io_Command = SDCMD_QUERY;
  613.  
  614.   /* Do our request: */
  615.   error = DoIO( ioreq );
  616.  
  617.   /* Everything OK? */
  618.   if( !error )
  619.   {
  620.     /* Check the "io_Status" field: */
  621.  
  622.     printf( "Bit Description                   Status\n" );
  623.     printf( "----------------------------------------\n" );
  624.  
  625.     printf( "  0 Reserved:                     %s\n",
  626.       ioreq->io_Status & 0x0001 ? "On" : "Off" );
  627.  
  628.     printf( "  1 Reserved:                     %s\n",
  629.       ioreq->io_Status & 0x0002 ? "On" : "Off" );
  630.  
  631.     printf( "  2 Connected to parallel select: %s\n",
  632.       ioreq->io_Status & 0x0004 ? "On" : "Off" );
  633.  
  634.     printf( "  3 Data Set Ready:               %s\n",
  635.       ioreq->io_Status & 0x0008 ? "On" : "Off" );
  636.  
  637.     printf( "  4 Clear To Send:                %s\n",
  638.       ioreq->io_Status & 0x0010 ? "On" : "Off" );
  639.  
  640.     printf( "  5 Carrier Detect:               %s\n",
  641.       ioreq->io_Status & 0x0020 ? "On" : "Off" );
  642.  
  643.     printf( "  6 Ready To Send:                %s\n",
  644.       ioreq->io_Status & 0x0040 ? "On" : "Off" );
  645.  
  646.     printf( "  7 Data Terminal Ready:          %s\n",
  647.       ioreq->io_Status & 0x0080 ? "On" : "Off" );
  648.  
  649.     printf( "  8 Read overrun:                 %s\n",
  650.       ioreq->io_Status & 0x0100 ? "On" : "Off" );
  651.  
  652.     printf( "  9 Break sent:                   %s\n",
  653.       ioreq->io_Status & 0x0200 ? "On" : "Off" );
  654.  
  655.     printf( " 10 Break received:               %s\n",
  656.       ioreq->io_Status & 0x0400 ? "On" : "Off" );
  657.  
  658.     printf( " 11 Transmit X-OFF:               %s\n",
  659.       ioreq->io_Status & 0x0800 ? "On" : "Off" );
  660.  
  661.     printf( " 12 Receive X-OFF                 %s\n",
  662.       ioreq->io_Status & 0x1000 ? "On" : "Off" );
  663.  
  664.     printf( " 13 Reserved:                     %s\n",
  665.       ioreq->io_Status & 0x2000 ? "On" : "Off" );
  666.  
  667.     printf( " 14 Reserved:                     %s\n",
  668.       ioreq->io_Status & 0x4000 ? "On" : "Off" );
  669.  
  670.     printf( " 15 Reserved:                     %s\n",
  671.       ioreq->io_Status & 0x8000 ? "On" : "Off" );
  672.  
  673.     printf( "----------------------------------------\n" );
  674.  
  675.     /* Check number of characters left in the input buffer: */
  676.     printf( "Characters left: %ld\n", ioreq->IOSer.io_Actual );
  677.  
  678.     printf( "----------------------------------------\n" );
  679.   }
  680.   else
  681.     printf( "Could not get any information from the device!\n" );
  682.   
  683.   return( error );
  684. }
  685.  
  686.  
  687.  
  688. /* SerReset() will reset the serial device. All commands that  */
  689. /* are qued to the device will be removed, commands that are   */
  690. /* currently executed will be aborted, the internal input      */
  691. /* buffer will be cleared and reallocated to the default size  */
  692. /* and finally all serial flags are resetted.                  */
  693. /*                                                             */
  694. /* Synopsis: error = SerReset( io );                           */
  695. /*                                                             */
  696. /* error:    (UBYTE) The function returns 0 if everything was  */
  697. /*           OK, else an error number is returned.             */
  698. /*                                                             */
  699. /* io:       (struct IOExtSer *) Pointer to an initialized     */
  700. /*           serial request block.                             */
  701.  
  702. UBYTE SerReset( struct IOExtSer *ioreq )
  703. {
  704.   /* We want to reset the serial device: */
  705.   ioreq->IOSer.io_Command = CMD_RESET;
  706.  
  707.   /* Do our request, and return 0 if everything is OK, else */
  708.   /* return an error number:                                */
  709.   return( (UBYTE) DoIO( ioreq ) );
  710. }
  711.  
  712.  
  713.  
  714. /* SerStop() will temporary stop the serial communication.     */
  715. /*                                                             */
  716. /* Synopsis: error = SerStop( io );                            */
  717. /*                                                             */
  718. /* error:    (UBYTE) The function returns 0 if everything was  */
  719. /*           OK, else an error number is returned.             */
  720. /*                                                             */
  721. /* io:       (struct IOExtSer *) Pointer to an initialized     */
  722. /*           serial request block.                             */
  723.  
  724. UBYTE SerStop( struct IOExtSer *ioreq )
  725. {
  726.   /* We want to start serial communication again: */
  727.   ioreq->IOSer.io_Command = CMD_STOP;
  728.  
  729.   /* Do our request, and return 0 if everything is OK, else */
  730.   /* return an error number:                                */
  731.   return( (UBYTE) DoIO( ioreq ) );
  732. }
  733.  
  734.  
  735.  
  736. /* SerStart() will restart the serial communication, which has */
  737. /* previously been halted by a SerStop() call.                 */
  738. /*                                                             */
  739. /* Synopsis: error = SerStart( io );                           */
  740. /*                                                             */
  741. /* error:    (UBYTE) The function returns 0 if everything was  */
  742. /*           OK, else an error number is returned.             */
  743. /*                                                             */
  744. /* io:       (struct IOExtSer *) Pointer to an initialized     */
  745. /*           serial request block.                             */
  746.  
  747. UBYTE SerStart( struct IOExtSer *ioreq )
  748. {
  749.   /* We want to start serial communication again: */
  750.   ioreq->IOSer.io_Command = CMD_START;
  751.  
  752.   /* Do our request, and return 0 if everything is OK, else */
  753.   /* return an error number:                                */
  754.   return( (UBYTE) DoIO( ioreq ) );
  755. }
  756.  
  757.  
  758.