@#
@# This is the source for the Allegro docs, in a weird custom format.
@# Read makedoc.c for a description of what is going on...
@#
@# If you want to put everything in one big HTML file rather than splitting
@# it into sections, remove this 'multiplefiles' statement:
@#
@multiplefiles
@#
@locale=latin1
@h=<html><head><title>#</title><body>
@f=<p><hr><p><a href="dzcomm.html">Back to Contents</a><p>
@rtfh=Dzcomm - a serial port API
@manh="version 0.9.9 (WIP)" "Dzcomm" "Dzcomm manual"
@mans=#include <dzcomm.h>
@$\input texinfo
@$@setfilename dzcomm.inf
@$@settitle Dzcomm Manual
@$@setchapternewpage odd
@$@paragraphindent 0
@$
@$@ifinfo
@$@direntry
@$* Dzcomm: (dzcomm). The dzcomm serial port API.
@$@end direntry
@$This is the Info version of the dzcomm manual
@$
@$By Neil Townsend
@$@end ifinfo
@$
@$@node Top, , (dir), (dir)
@<html>
@<head>
@<title>Dzcomm - a serial port API</title>
@<body>
@<pre>
@!indent

                    DZCOMM 0.9.9 (WIP)

		    A serial port API.

	      By Neil Townsend, Dec 08, 1999.

		See the AUTHORS file for a
	       complete list of contributors.
@indent
@</pre>



#include &ltstd_disclaimer.h&gt
<i>
   "I do not accept responsibility for any effects, adverse or otherwise, 
    that this code may have on you, your computer, your sanity, your dog, 
    and anything else that you can think of. Use it at your own risk."
</i>


@!text
@heading
Contents

@contents



@text
@heading
General points

See readme.txt for a general introduction, copyright details, and 
information about how to install dzcomm and link your program with it.

Dzcomm is a result of the magnificent Allegro library. It has provided the
inspiration and in many cases the code which has simplfied the process of
developping what I hope will become a multi-platform serial line API.

Hopefully, Dzcomm has now evolved to the point where it can work alongside
allegro but no longer relies on it. It should have alternative routines
for everything it previosuly relied on allegro to do and should decide
when you compile it in whether to use allegros routines or its own.

To use dzcomm in your code you will need to put
<pre>
      #include &ltdzcomm.h&gt
</pre>
in your code. Obviously (I hope) this will only work after you've installed
the library. If you wish to use allegro alongside dzcomm you will need to
include the allegro library. If you do so it is IMPERATIVE that you include
allegro first (this how dzcomm knows where to link to):
<pre>
      #include &ltallegro.h&gt
      #include &ltdzcomm.h&gt
</pre>

If you have more than one C file in a project and you wish to access dzcomm
from more than one of those files, you must put the following #define before
all but one of the dzcomm includes:
<pre>
      #define DZCOMM_SECONDARY_INCLUDE
      #include &ltdzcomm.h&gt
</pre>
However, do remember to inlcude it once without the #define!

@heading
Port configuration

To use dzcomm it is necessary to initialise the library and then to configure
the ports. Each serial line is considered as an indepnedent input/output port
and has certain characteristics associated with it. All the routines which talk
to, read from or somehow control an serial line will take, as one of their
arguemnts, a pointer to the structure which defines that port:
<pre>
      comm_port *port.
</pre>
This structure contains a complete desription of how the port is configured.
However, it is not necessary (or advisable) to alter the contents of this
structure directly. A collection of routines are provided which enable the
port to be configured and used without having to mess around with the guts
of this structure.

The following sequence should be used:
<pre>
      comm_port *my_port;

      dzcomm_init();                      // initialise the library
      my_port = comm_port_init(_comN);    // create the port
      comm_port_set_*(my_port, *);        // define it
      comm_port_install_handler(my_port)  // run the port as per this definition

      // And then read and write from and to the port.
</pre>

@@int @dzcomm_init();
@xref dzcomm_closedown
   Initialises the dzcomm library. A necessary call before using any of the
   other functions. Having done this you will want to initialise one or more
   ports and then use them. The function returns 1 if dzcomm is successfully
   installed, 0 otherwise.

@@void @dzcomm_closedown();
@xref dzcomm_init, comm_port_delete
   Does the opposite to dzcomm_init. Closedown the dzcomm serial API and
   releases system resources as much as it can. Is automatically called
   on program exit (assuming that dzcomm_init has been called) and will
   will uninstall and delete any ports which require it. It is therefore
   not necessary to uninstall or delete ports explicity in code which uses
   the dzcomm library (0.9.4 and above).

@@comm_port *@comm_port_init(comm n);
@xref dzcomm_init, comm_port_install_handler
   Creates a handle (which is a pointer to a structre) for a new port.
   The argument, defined below, simply gives comm_port_init a hint as
   to which potentially built-in port the port should define. If the
   port you are using bears no relation to a standard PC built in port
   just send an arbitrary argument and then use the comm_port_set_*
   routines to describe the port. Even if if you are using a standard
   port you will want to use the comm_port_set_* routines to complete
   the configuration of the port.

   The argument must be one of the following. Please use these values
   rather than infering what the enum values might be.
<pre>
      typedef enum {_com1, _com2, _com3, _com4, _com5, _com6, _com7, _com8} comm;
</pre>

@@int @comm_port_load_settings(comm_port *port, char *file_name)
   Load the setting for a comm port from a file. Traditionally these files
   have had a ".ini" extension but that is not required. Return values are
   1 for success and 0 for failure.

   Format of the file:
<pre>
      baud=<baud value>; //<baud value>={110,150,300,600,1200,2400,...,115200}
      Example : baud=9600;

      data=<data lehgth>; //<data length>={5,6,7,8}
      Example : data=7;

      parity=<parity bits>; //<parity bits>={Even,Odd,No(None)}
      Example : parity=Even;

      stop=<stop bits>; //<stop bits>={1,2}
      Example : stop=1;

      irq=<irq number>;
      Example : irq=5;

      address=<port address>; // port address in hex.
      Example : address=0x2f8; // Note : use 0x prefix always.

      control=<control type>;
      //<control type>={xon_xoff(xon/xoff),rts_cts(rts/cts),no(none)}
      Example : control=xon_xoff;

      name=<port name>; // just name of port. Not used by any lib function.
      Example : name=Modem Courier V.EVERYTHING;
</pre>

@@void @comm_port_set_irq_num(comm_port *port, unsigned char irq_num);
   Set the IRQ number for the port. irq_num should be the IRQ line the port is
   set to use, between 0 and 15 inclusive. The value set will only come into effect
   when a port is reinstalled.

@@void @comm_port_set_name(comm_port *port, char *name);
   Set the name that is associated with the port. name should be a pointer to
   a 0 terminated string which will be copied into an internal string. The
   maximum allowed length of the string is DZCOMM_PORT_NAME_MAX_LENGTH, which
   is defined in dzcomm.h. This name is irrelevant to the actual running of the
   port and this _set_ call will take effect immediately.

@@void @comm_port_set_port_address(comm_port *port, unsigned short int port_address);
   Set the address of the port. The value set will only come into effect
   when a port is reinstalled.

@@void @comm_port_set_baud_rate(comm_port *port, baud_bits baud_rate);
   Set the baud rate the port should use. The value set will only come into effect
   when a port is reinstalled.

   The argument must be one of the following. Please use these values
   rather than infering what the enum values might be.
<pre>
      typedef enum {_110,   _150,   _300,   _600,   _1200,  _2400,  _4800,
                    _9600,  _19200, _38400, _57600, _115200} baud_bits;
</pre>


@@void @comm_port_set_data_bits(comm_port *port, data_bits num_data_bits);
   Set the number of data bits the port should use. The value set will only
   come into effect when a port is reinstalled.

   The argument must be one of the following. Please use these values
   rather than infering what the enum values might be.
<pre>
      typedef enum {BITS_5, BITS_6, BITS_7, BITS_8} data_bits;
</pre>

@@void @comm_port_set_stop_bits(comm_port *port, data_bits num_stop_bits);
   Set the number of stop bits the port should use. The value set will only
   come into effect when a port is reinstalled.

   The argument must be one of the following. Please use these values
   rather than infering what the enum values might be.
<pre>
      typedef enum {STOP_1, STOP_2} stop_bits;
</pre>

@@void @comm_port_set_parity(comm_port *port, parity_bits irq_num);
   Set the parity method for the port. The value set will only
   come into effect when a port is reinstalled.

   The argument must be one of the following. Please use these values
   rather than infering what the enum values might be.
<pre>
      typedef enum {NO_PARITY, ODD_PARITY, EVEN_PARITY, MARK_PARITY, SPACE_PARITY} parity_bits;
</pre>

@@void @comm_port_set_flow_control(comm_port *port, flow_control_type flow_option);
   Set the flow control method the port should employ. The value set will only
   come into effect when a port is reinstalled.

   The argument must be one of the following. Please use these values
   rather than infering what the enum values might be.
<pre>
      typedef enum {NO_CONTROL, XON_XOFF, RTS_CTS} flow_control_type;
</pre>

@@void @comm_port_set_lsr_handler(comm_port *port, dz_ll_handler lsr_handler);
   HANDLE WITH CARE. This sets a routine which will be called when the
   UART returns an LSR condition. The code mnust be locked in memory if
   your system requires it. The lsr_handler argument is the function
   which will be called when the condition arises:
<pre>
      int lsr_hander(comm_port *port, int lsr_contents)
</pre>
   It take two arguments: the port on which the condition arose and the
   contents of the LSR register. You should not reread the LSR in the
   handler as the read which was used to pass the contents to this
   function will have reset it.

   dz_ll_handler is simply a shorthand for a pointer to the above type of
   function.

   This call takes effect immediately.
   
@@void @comm_port_set_msr_handler(comm_port *port, int (*msr_handler)(comm_port *port, int msr_contents));
   HANDLE WITH CARE. This sets a routine which will be called when the
   UART returns an MSR condition. The code mnust be locked in memory if
   your system requires it. the msr_handler argument is the function
   which will be called when the condition arises:
<pre>
      int msr_hander(comm_port *port, int msr_contents)
</pre>
   It take two arguments: the port on which the condition arose and the
   contents of the MSR register. You should not reread the MSR in the
   handler as the read which was used to pass the contents to this
   function will have reset it.

   dz_ll_handler is simply a shorthand for a pointer to the above type of
   function.

   This call takes effect immediately.

@@unsigned char @comm_port_get_irq_num(comm_port *port);
@xref comm_port_set_irq_num
   Returns the IRQ number for the port. Returns the most recent value set by the
   _set_ routine, which may not have been into action yet.

@@char *@comm_port_get_name(comm_port *port, char *name, int max_chars);
@xref comm_port_set_name
   Returns the name that is associated with the port. name should be a pointer to
   a block of memory which can hold up to max_chars characters in which the name
   will be stored. Note that the maximum length of a port name is
   DZCOMM_PORT_NAME_MAX_LENGTH.

@@unsigned short int @comm_port_get_port_address(comm_port *port);
@xref comm_port_set_port_address
   Returns the address of the port. Returns the most recent value set by the
   _set_ routine, which may not have been into action yet.

@@baud_bits @comm_port_get_baud_rate(comm_port *port);
@xref comm_port_set_baud_rate
   Returns the baud rate the port should use. Returns the most recent value set by the
   _set_ routine, which may not have been into action yet.

@@data_bits @comm_port_get_data_bits(comm_port *port);
@xref comm_port_set_data_bits
   Returns the number of data bits the port should use. Returns the most recent value set by the
   _set_ routine, which may not have been into action yet.

@@data_bits @comm_port_get_stop_bits(comm_port *port);
@xref comm_port_set_stop_bits
   Returns the number of stop bits the port should use. Returns the most recent value set by the
   _set_ routine, which may not have been into action yet.

@@parity_bits @comm_port_get_parity(comm_port *port);
@xref comm_port_set_parity
   Returns the flow control method for the port. Returns the most recent value set by the
   _set_ routine, which may not have been into action yet.

@@flow_control_type @comm_port_get_flow_control(comm_port *port);
@xref comm_port_set_flow_control
   Returns the flow control method used by the port.

@@dz_ll_handler @comm_port_get_lsr_handler(comm_port *port);
@xref comm_port_set_lsr_handler
   Return the user's lsr handler, if any. It returns a pointer to a function
   of type:
<pre>
      int lsr_hander(comm_port *port, int lsr_contents)
</pre>
   or NULL if no function has been set.

@@dz_ll_handler @comm_port_get_msr_handler(comm_port *port);
@xref comm_port_set_msr_handler
   Return the user's lsr handler, if any. It returns a pointer to a function
   of type:
<pre>
      int msr_hander(comm_port *port, int msr_contents)
</pre>
   or NULL if no function has been set.


@heading
Data flow

Having set up your ports you will want to use them! To do this you must
install the handler. Having donw this ou can then read from and write
to the ports.

@@int @comm_port_install_handler(comm_port *port);
@xref comm_port_reinsatall, comm_port_uninstall, comm_port_init
   Once the port has been configured, whether from a file or by using the
   comm_port_set_* routines, this function must be called to ensure that
   the port is running with the desired confiuration. It should only be
   called once per port, configuration changes should be enfored using
   comm_port_reinstall. Until this function is called the port cannot be
   written to or read from.

@@int @comm_port_reinsatall(comm_port *port);
@xref comm_port_uninstall, comm_port_install_handler, comm_port_init, comm_port_flush_output
   If there is a need to reconfigure the port after comm_port_install_port
   has been called, this can be achieved in two steps. First, reconfigure
   the comm_port structure either using a file or using the comm_port_set_*
   functions. Then call this function to ensure that they take effect. This
   call will wait for any remaining data on the transmit queue to clear
   before reconfiguring the port.

@@void @comm_port_uninstall(comm_port *port);
@xref comm_port_install_handler, comm_port_reinsatall, comm_port_init, comm_port_delete, comm_port_flush_output
   Removes the port from the list of functioning ports under dzcomm. Once
   this function has been called it is no longer to write to or read from
   the port. Use comm_port_install_handler to get the port running again.
   This function will wait for any remaining data on the transmit queue to
   clear before uninstalling the port.
   
@@void @comm_port_flush_output(comm_port *port);
@xref comm_port_flush_input
   Flushes the input buffer.

@@void @comm_port_flush_input(comm_port *port);
@xref comm_port_flush_output
   Flushes the input buffer.

@@void @comm_port_delete(comm_port *port);
@xref comm_port_uninstall, comm_port_init
   Having uninstalled your port you may decide that you don't need the
   configuration data either and can better use the memory elsewhere.
   Calling this function will free up the memory used to store and run
   the port. However, please don't try to install the port after you've
   called comm_port_delete.

@@int @comm_port_out(comm_port *port, unsigned char c);
@xref comm_port_out_empty, comm_port_string_send, comm_port_command_send, comm_port_test
   Puts 'c' on the software output buffer. The UART will then output the
   contents of this buffer. c must not use more than the number of bits
   specified configured for data. Returns 1 if the character is successfuly
   placed on the buffer, 0 otherwise.

@@int @comm_port_out_empty(comm_port *port);
@xref comm_port_out, comm_port_command_send, comm_port_test, comm_port_in_empty, comm_port_out_full
   Indicates if there is data waiting to be sent to the UART. Returns 0
   if there is data waiting to be sent, -1 if the buffer is empty. Also
   returns 0 on failure.

@@int @comm_port_out_full(comm_port *port);
@xref comm_port_out, comm_port_command_send, comm_port_test, comm_port_in_full, comm_port_out_empty
   Indicates if the buffer of data waiting to be sent is now full. Returns -1
   if this is the case and 0 if there is still room for more data on the
   buffer. Also returns 0 on failure.

@@int @comm_port_string_send(comm_port *port,char *s);
@xref comm_port_out, comm_port_out_empty, comm_port_command_send, comm_port_test
   The string 's' is sent to the output buffer one character at a time; the
   UART will transmit this down the serial line. Returns the number of characters
   successfully placed on the output buffer.

@@int @comm_port_command_send(comm_port *port,char *s);
@xref comm_port_out, comm_port_out_empty, comm_port_string_send, comm_port_test
   The string 's' is sent to the output buffer one character at a time; the
   UART will transmit this down the serial line. At the end of the string a
   '\r' character is sent. Returns the number of characters successfully placed
   on the output buffer, counting the '\r'. A successfuly send will therefore
   return the length of the string given to it plus one.

@@int @comm_port_in_empty(comm_port *port);
@xref comm_port_test, comm_port_out, comm_port_in_full, comm_port_out_empty
   Indicates if data has come in on the serial line. It returns -1 if there is
   no data (ie. the buffer is empty), 0 if there is data waiting to be read.
   Also returns 0 on failure.

@@int @comm_port_in_full(comm_port *port);
@xref comm_port_test, comm_port_out, comm_port_in_empty, comm_port_out_full
   Indicates if the buffer of data which has come in on the serial line is now
   full. It retunr -1 if this is the case (ie. the buffer is full) and 0
   otherwise. Also return 0 on failure.

@@inline int @comm_port_test(comm_port *port);
@xref comm_port_in_empty, comm_port_out
   Checks to see data has come in on the data line. If there is none it returns
   -1, like comm_port_in_empty. If there is some it returns the next character
   from the queue of those received from the serial line (and takes it off the
   queue).

@@void @comm_port_send_break(comm_port *port, int msec_duration);
   A break signal is issued on the serial line. The break signal will be at
   least of the duration specified. It may be longer.

@@int @comm_port_give_line_status(comm_port *port, dzcomm_line line);
@xref comm_port_set_line_status
   Returns the value (1 or 0) of the register requested or -1 on failure. Note
   that the value returned is the inverse of the physical state of the line
   connected to the UART because the control lines on a UART are all low
   when the register is high and vice versa.

   The control line is defined using the following enum:
<pre>
      typedef enum {DZCOMM_DTR, DZCOMM_RTS, DZCOMM_CTS, DZCOMM_DSR} dzcomm_line;
</pre>

@@int @comm_port_set_line_status(comm_port *port, dzcomm_line line, int value);
@xref comm_port_give_line_status
   Sets the value of the register requested and return 1 on success or -1 on
   failure. Note that the physical state of the line connected to the UART
   will be the inverse of the value set here because the control lines on
   a UART are all low when the register is high and vice versa.

   The control line is defined using the following enum:
<pre>
      typedef enum {DZCOMM_DTR, DZCOMM_RTS, DZCOMM_CTS, DZCOMM_DSR} dzcomm_line;
</pre>

   Be careful when using this function - if you are using CTS_RTS flow
   control or if the UART at the other end is snesitive to it, you may
   cause confusion ...

@@int @modem_hangup(comm_port *port);
   Sends "+++ATH0" to the line which tells it to hang up if it's a modem. Returns
   one if the entire string (including the concluding '\r') is placed on the output
   buffer, zero otherwise.

@heading
Queue code

Dzcomm comes with some FIFO queue code whcih is lso available for general use.

@@fifo_queue *@queue_new(unsigned int size);
@xref queue_new_, queue_delete
   This function creates a queue of ints. If you need to store data values
   of a different size use queue_new_ function instead of it. It returns
   a pointer to a structure which serves as a handle for the queue, or
   NULL on failure.
   
   Example :
<pre>
     fifo_queue *q;
     if ((q=queue_new(1024))==NULL) return(0);
</pre>

@@fifo_queue *@queue_new_(unsigned int size,unsigned int dsize);
@xref queue_new, queue_delete
   Create a queue of 'size' elements each of size 'dsize'. It returns a
   pointer to a structure which serves as a handle for the queue or NULL
   on failure.

   Example :
<pre>
      typedef struct {
         int code;
         int p1;
         int p2;
         } msg;

      //...

      fifo_queue *msg_q;
      if ((msg_q=queue_new(1024,sizeof(msg)))==NULL) return(0);
</pre>

@@void @queue_delete(fifo_queue *q);
@xref queue_new_, queue_new
   Delete queue description and free occupied space. Don't use the
   queue after this ...

@@void @queue_reset(fifo_queue *q);
   Set head and tail of the queue to zero (data lost).

@@int @queue_put(fifo_queue *q,int c);
@xref queue_get, queue_put_, queue_peek
   Put value into given integer queue. On success returns
   DZQ_NEARLY_FULL if the queue's getting a tad full,
   DZQ_NOT_NEARLY_FULL otherwise. On failure returns
   DZQ_FAIL_NO_Q if q was a NULL pointer or DZQ_FAIL_FULL
   if the queue was actually full. Note that failure codes
   are less than zero, success codes greater than or equal
   to zero. If you need to store data of a different size
   then use the queue_put_ function.

@@int @queue_put_(fifo_queue *q,void *data)
@xref queue_get_, queue_put, queue_peek_
   Put value into given queue. On success returns 
   DZQ_NEARLY_FULL if the queue's getting a tad full,
   DZQ_NOT_NEARLY_FULL otherwise. On failure returns
   DZQ_FAIL_NO_Q if q was a NULL pointer or DZQ_FAIL_FULL
   if the queue was actually full. Note that failure codes
   are less than zero, success codes greater than or equal
   to zero.

@@int @queue_peek(fifo_queue *q);
@xref queue_peek_, queue_get, queue_put, queue_empty
   See the integer value at the top of an integer queue. Returns the
   value from the top of the queue but does not remove it from the
   queue. If the queue is not of integers then use the queue_peek_
   function. Use queue_empty before calling queue_get to verify that
   there is data to peek at.

@@int @queue_peek_(fifo_queue *q,void *data);
@xref queue_peek, queue_put_, queue_get_, queue_empty
   See the value at the top of the queue. Returns (in the memory
   location pointed to by data) the value from the top of the queue
   but does not remove it from the queue. The returned integer is
   DZQ_NEARLY_FULL if the queue's getting a tad full,
   DZQ_NOT_NEARLY_FULL otherwise. On failure returns
   DZQ_FAIL_NO_Q if q was a NULL pointer or DZQ_FAIL_EMPTY
   if the queue was actually empty. Note that failure codes
   are less than zero, success codes greater than or equal
   to zero. It's not a bad idea to use
   queue_empty before calling queue_peek_ to verify that there is data
   to get.

@@int @queue_get(fifo_queue *q);
@xref queue_get_, queue_peek, queue_put, queue_empty
   Get integer values from an integer queue. Returns the value from
   the top of the queue. If the queue is not of integers then use the
   queue_get_ function. Use queue_empty before calling queue_get to
   verify that there is data to get.

@@int @queue_get_(fifo_queue *q,void *data);
@xref queue_get, queue_peek_, queue_put_, queue_empty
   Get value from queue. Returns the value from the top of the queue
   (in the memory location pointed to by data). The returned integer
   is DZQ_NEARLY_FULL if the queue's getting a tad full,
   DZQ_NOT_NEARLY_FULL otherwise. On failure returns
   DZQ_FAIL_NO_Q if q was a NULL pointer or DZQ_FAIL_EMPTY
   if the queue was actually empty. Note that failure codes
   are less than zero, success codes greater than or equal
   to zero. Use queue_empty before calling queue_get to verify that
   there is data to get.

@@int @queue_empty(fifo_queue *q);
@xref queue_get_, queue_get
   Test queue for emptiness. Returns DZQ_EMPTY if the queue is empty,
   DZQ_NOT_EMPTY if the queue is not full and DZQ_FAIL_INTERNAL on
   failure.

@@int @queue_nearly_full(fifo_queue *q);
@xref queue_full
   Test to indicate if the queue is nearly full (ie over 75% at
   present). Returns DZQ_NEARLY_FULL if the queue is nearly full
   and DZQ_NOT_NEARLY_FULL otherwise.

@@int @queue_full(fifo_queue *q);
@xref queue_nearly_full
   Test to indicate if the queue is full. Returns DZQ_FULL if the
   queue is full and DZQ_NOT_FULL otherwise.

@htmlindex Changes
@htmlindex Thanks
@htmlindex FAQ
@htmlindex Help
@htmlindex dzHack
@headingnocontent
Conclusion

Doubtless I've missed something or not been clear above. Feel free to
suggest how the documentation could be improved!

I hope you find some of this stuff useful.


By Neil Townsned

<link>http://www.robots.ox.ac.uk/dzcomm</a>



@!html
@!text
@headingnocontent
Index

@index

@$@contents
@$@bye

@html
@text
