ezV24.h File Reference

Reference of the ezV24 API. More...

#include <stdlib.h>

Go to the source code of this file.

Data Structures

struct  __EZV24_PORT_HANDLE
 internal data structure of the port handle. More...

Defines

#define __EZV24_H__   1
#define V24_SZ_PORTNAME   25

Typedefs

typedef struct __EZV24_PORT_HANDLE v24_port_t

Enumerations

enum  __EZV24_ERROR_CODES {
  V24_E_OK = 0, V24_E_ILLPARM, V24_E_ILLBAUD, V24_E_ILLDATASZ,
  V24_E_ILLPARITY, V24_E_ILLHANDLE, V24_E_ILLTIMEOUT, V24_E_CREATE_LOCK,
  V24_E_OPEN_LOCK, V24_E_KILL_LOCK, V24_E_WRITE_LOCK, V24_E_LOCK_EXIST,
  V24_E_NOMEM, V24_E_NULL_POINTER, V24_E_OPEN, V24_E_READ,
  V24_E_WRITE, V24_E_NOT_INIT, V24_E_NO_PROC_FILE, V24_E_NOT_IMPLEMENTED,
  V24_E_TIMEOUT, V24_E_DBG_MESSAGES = 100, V24_E_DBG_STALE_LOCK
}
 

Error code of the library.

More...
enum  __EZV24_BAUDRATE {
  V24_B0 = 0, V24_B50, V24_B75, V24_B110,
  V24_B134, V24_B150, V24_B200, V24_B300,
  V24_B600, V24_B1200, V24_B1800, V24_B2400,
  V24_B4800, V24_B9600, V24_B19200, V24_B38400,
  V24_B57600, V24_B115200, V24_NUM_BAUDRATES
}
 

Valid Baudrates.

More...
enum  __EZV24_DATASIZE {
  V24_5BIT = 0, V24_6BIT, V24_7BIT, V24_8BIT,
  V24_NUM_DATASIZES
}
 

Valid datasizes.

More...
enum  __EZV24_PARITY_FLAGS { V24_NONE = 0, V24_EVEN, V24_ODD, V24_IGNORE }
 

Valid parity settings.

More...
enum  __EZV24_OPEN_FLAGS {
  V24_STANDARD = 0x0000, V24_LOCK = 0x0001, V24_NO_DELAY = 0x0002, V24_RTS_CTS = 0x0004,
  V24_XON_XOFF = 0x0008, V24_DROP_DTR = 0x0010, V24_NON_BLOCK = 0x0020, V24_SYNC = 0x0040,
  V24_DEBUG_ON = 0x8000
}
 

Valid open flags.

More...
enum  __EZV24_STATUS_FLAGS {
  V24_DSR_LOW = 0, V24_DSR_HIGH, V24_CTS_LOW, V24_CTS_HIGH,
  V24_DSRCTS_UNKNOWN
}
 

Valid status flags.

More...

Functions

int v24CountPorts (unsigned long *BitMask)
const char * v24PortName (int PortNo, char *PortName)
v24_port_tv24OpenPort (const char *PortName, unsigned int OpenFlags)
int v24ClosePort (v24_port_t *port)
int v24SetParameters (v24_port_t *port, int Baudrate, int Datasize, int Parity)
int v24SetStopbits (v24_port_t *port, int Stops)
int v24SetTimeouts (v24_port_t *port, int TenthOfSeconds)
int v24Getc (v24_port_t *port)
int v24Putc (v24_port_t *port, unsigned char TheData)
int v24Read (v24_port_t *port, unsigned char *Buffer, size_t Len)
int v24Write (v24_port_t *port, const unsigned char *Buffer, size_t Len)
int v24Gets (v24_port_t *port, char *Buffer, size_t BuffSize)
int v24Puts (v24_port_t *port, const char *Buffer)
int v24HaveData (v24_port_t *port)
int v24FlushRxQueue (v24_port_t *port)
int v24FlushTxQueue (v24_port_t *port)
int v24SetDTR (v24_port_t *port, int NewState)
int v24SetRTS (v24_port_t *port, int NewState)
int v24GetDSR (v24_port_t *port, int *CurrState)
int v24GetCTS (v24_port_t *port, int *CurrState)
const char * v24QueryPortName (v24_port_t *port)
int v24QueryFileHandle (v24_port_t *port)
int v24QueryErrno (v24_port_t *port)

Detailed Description

Reference of the ezV24 API.


Enumeration Type Documentation

Valid Baudrates.

See also:
v24SetParameters
Enumerator:
V24_B0 

used to drop DTR

V24_B50 

50 baud

V24_B75 

75 baud

V24_B110 

110 baud

V24_B134 

134.5 baud

V24_B150 

150 baud

V24_B200 

200 baud

V24_B300 

300 baud

V24_B600 

600 baud

V24_B1200 

1200 baud

V24_B1800 

1800 baud

V24_B2400 

2400 baud

V24_B4800 

4800 baud

V24_B9600 

9600 baud

V24_B19200 

19200 baud

V24_B38400 

38400 baud

V24_B57600 

57,600 baud

V24_B115200 

115,200 baud

V24_NUM_BAUDRATES 

the number of entries

Valid datasizes.

See also:
v24SetParameters
Enumerator:
V24_5BIT 

5 bits

V24_6BIT 

6 bits

V24_7BIT 

7 bits

V24_8BIT 

8 bits

V24_NUM_DATASIZES 

number of datasize values

Error code of the library.

Enumerator:
V24_E_OK 

no errors, all went fine.

V24_E_ILLPARM 

`general' illegal parameter.

V24_E_ILLBAUD 

illegal baudrate.

V24_E_ILLDATASZ 

illegal datasize.

V24_E_ILLPARITY 

illegal parity.

V24_E_ILLHANDLE 

illegal handle. The parameter specifying the port handle is bad. Make sure unused handles are set to NULL.

V24_E_ILLTIMEOUT 

illegal timeout value.

V24_E_CREATE_LOCK 

creation of the lock file failed.

V24_E_OPEN_LOCK 

lock file can't be opened.

V24_E_KILL_LOCK 

unlink of the lock file failed.

V24_E_WRITE_LOCK 

can't write to the lock file.

V24_E_LOCK_EXIST 

foreign lock file exist. This means that another process already has locked the port.

V24_E_NOMEM 

not enough memory.

V24_E_NULL_POINTER 

pointer is NULL.

V24_E_OPEN 

open failed.

V24_E_READ 

read failed.

V24_E_WRITE 

write failed.

V24_E_NOT_INIT 

library is not initialized.

V24_E_NO_PROC_FILE 

no proc file system. We can't open the virtual file /proc/tty/...

V24_E_NOT_IMPLEMENTED 

function not implemented.

V24_E_TIMEOUT 

timeout waiting for data.

V24_E_DBG_MESSAGES 

debugging. Error codes greater than this value are debug messages.

V24_E_DBG_STALE_LOCK 

stale lock file overwritten. The PID stored in the lock file doesn't exist, so we can overwrite it.

Valid open flags.

See also:
v24OpenPort
Enumerator:
V24_STANDARD 

just empty

V24_LOCK 

lock the port

V24_NO_DELAY 

no wait on DCD while opening the port

V24_RTS_CTS 

use hardware handshakes

V24_XON_XOFF 

use software handshakes

V24_DROP_DTR 

drop DTR on close the port

V24_NON_BLOCK 

non blocking read

V24_SYNC 

synchronous writes, i.e. not using buffers

V24_DEBUG_ON 

enable stderr messages

Valid parity settings.

See also:
v24SetParameters
Enumerator:
V24_NONE 

disable parity bit

V24_EVEN 

even parity

V24_ODD 

odd parity

V24_IGNORE 

use parity but do not test it

Valid status flags.

Enumerator:
V24_DSR_LOW 

DSR state is down.

V24_DSR_HIGH 

DSR state is up.

V24_CTS_LOW 

CTS state is down.

V24_CTS_HIGH 

CTS state is up.

V24_DSRCTS_UNKNOWN 

just in case the function returns abruptly


Function Documentation

int v24ClosePort ( v24_port_t port  ) 

This function closes a previously opened device. If a lock file is used, this file will be removed. As a result, the V24_* error code is returned. Possible errors are V24_E_ILLHANDLE and V24_E_KILL_LOCK. If all went fine, V24_E_OK is returned.

Parameters:
port pointer to handle of the opened port.
Returns:
(int) the V24_* error code.
See also:
v24OpenPort
int v24CountPorts ( unsigned long *  BitMask  ) 

Detect installed serial devices. The goal of this function is to detect which serial devices are installed. Therefore the /proc file system is used. The content of the virtual file /proc/tty/driver/serial must be present. It must hold a specially formatted line for each device (see below). A device is recognized as `installed' if this line doesn't contain the word ``unknown'' but contain the ``tx:''.

The line format of the virtual file should look like this:

   0: uart:16550A port:3F8 irq:4 tx:0 rx:0
   1: uart:16550A port:2F8 irq:3 tx:0 rx:0
   2: uart:unknown port:3E8 irq:4
   

The result of the detection is stored in a unsigned long variable. For every port found, the corresponding bit is set. The above sample shows us that port0 (ttyS0) and port1 (ttyS1) are present. So the bit0 and bit1 of the variable referenced by BitMask are set.

If the function fails to detect the installed ports, a -1 is returned. If all went fine, the number of detected ports is returned.

Note: this function can be used without an opened device!

Parameters:
BitMask pointer to a unsigned long variable.
Returns:
(int) number of detected ports or -1.
int v24FlushRxQueue ( v24_port_t port  ) 

All remaining characters in the receive-queue are removed.

Parameters:
port pointer to handle of the opened port.
Returns:
(int) the V24_* error code.
int v24FlushTxQueue ( v24_port_t port  ) 

All remaining characters in the transmit-queue are removed.

Parameters:
port pointer to handle of the opened port.
Returns:
(int) the V24_* error code.
int v24Getc ( v24_port_t port  ) 

The function tries to read a single character from the opened device. To do this, the function v24Read is used. If we have got some data, the character is returned as integer value. The caller have to cast it to the data type he need.

In case of an error, the function returns -1. The caller has to use v24QueryErrno to get the exact error code. Contrarily to v24Read, a timeout is reported as error!

Possible error code are V24_OK, V24_E_NULL_POINTER, V24_E_ILLHANDLE, V24_E_TIMEOUT or V24_READ.

Parameters:
port pointer to handle of the opened port.
Returns:
(int) the character read or -1.
See also:
v24QueryErrno, v24Read.
int v24GetCTS ( v24_port_t port,
int *  CurrState 
)

Get the state of the CTS line and store it on the parameter CurrState. If CurrState is 0 the CTS signal is unset, otherwise it is set. This will only happen if the port uses hardware handshaking. This means v24OpenPort is called with V24_RTS_CTS. If no hardware handshaking is used while calling this function, it aborts and returns V24_E_ILLPARM as error.

The values returned are: V24_E_OK, V24_E_ILLHANDLE, V24_E_ILLPARM, V24_E_NOT_IMPLEMENTED.

Parameters:
port pointer to handle of the opened port.
CurrState pointer to the current state of CTS (see V24_CTS_ constants). If there is a problem getting CurrState, it will be set to V24_DSRCTS_UNKNOWN.
Returns:
(int) the V24_* error code.
int v24GetDSR ( v24_port_t port,
int *  CurrState 
)

Get the state of the DSR line and store it on the parameter CurrState. If CurrState is 0 the DSR signal is unset, otherwise it is set.

Parameters:
port pointer to handle of the opened port.
CurrState pointer to the current state of DSR (see V24_DSR_ constants). If there is a problem getting CurrState, it will be set to V24_DSRCTS_UNKNOWN.
Returns:
(int) the V24_* error code.
int v24Gets ( v24_port_t port,
char *  Buffer,
size_t  BuffSize 
)

Receive a string. Unlike v24Read, which tries to read a fixed number of character, v24Gets read characters unless BuffSize characters are fetched, or a end of line (\n) is received. At the end of the game, the number of characters read is returned. If an error occurs, a -1 is returned. The characters are stored in Buffer. The function ensures a 0 terminated string!

Possible error code are V24_OK, V24_E_NULL_POINTER, V24_E_ILLPARM, V24_E_ILLHANDLE or V24_WRITE. To get this error code, use v24QueryErrno.

A string should at least have 1 character and the terminating 0. The parameter BuffSize must therefore be greater than 1! If not, the operation results in a V24_E_ILLPARM error. To read a single character, we have v24Getc.

Implementation note: the current release of the library defines the end of a string as a hard coded constant! Search the internal headers EZV24_END_OF_STRING.

Parameters:
port pointer to handle of the opened port.
Buffer pointer to buffer.
BuffSize the maximum size of the buffer.
Returns:
(int) number of fetched characters or -1.
See also:
v24QueryErrno, v24Getc, v24Read.
int v24HaveData ( v24_port_t port  ) 

If implemented by the operating system, this function returns the number of character waiting in the receive-queue. If the function isn't available or if an error happens, a -1 is returned. Use v24QueryErrno to retrieve the error code.

Possible error code are V24_OK, V24_E_NOT_IMPLEMENTED and V24_E_ILLHANDLE.

Parameters:
port pointer to handle of the opened port.
Returns:
(int) number of character in the receive-queue.
See also:
v24QueryErrno.
v24_port_t* v24OpenPort ( const char *  PortName,
unsigned int  OpenFlags 
)

This function opens a serial device for reading and writing. A pointer to a special handle is returned. This handle must be passed to all further functions. If the function fails to open the port, a value of NULL is returned to indicate the error.

The portname is a string which represents the serial device. This representation is platform dependent. The following table shows the naming on several different operating systems. To avoid the usage of these names, you can use v24PortName to build the name for you.

System

port #1

port #2

Linux

/dev/ttyS0

/dev/ttyS1

NetBSD

/dev/tty00

/dev/tty01

IRIX

/dev/ttyf1

/dev/ttyf2

HP-UX

/dev/tty1p0

/dev/tty2p0

Solaris/SunOS

/dev/ttya

/dev/ttyb

Digital UNIX

/dev/tty01

/dev/tty02

Cygwin

/dev/ttyS0

/dev/ttyS1

Windows

com1:

com2:

The open flags are used to specify a special behaviour of the library. The following flag can be OR'ed together.

V24_STANDARD
just empty. The defaults are used.
V24_LOCK
lock the port. Therefore a lock file is created. After the port is closed, the lock file is removed automagically.
V24_NO_DELAY
don't wait on DCD while opening the port.
V24_RTS_CTS
use RTS/CTS hardware handshake.
V24_XON_XOFF
use Xon/Xoff software handshake.
V24_DROP_DTR
drop DTR after closing the port.
V24_NON_BLOCK
non blocking read. Using this flag, the read access is not delayed. Without this, read will wait until a character is received or the timeout time is reached. Note that in non blocking mode, there is no time error!
V24_SYNC
do all writes synchronously, i.e., do not use any buffering for writes.
V24_DEBUG_ON
enable stderr messages.

Some more word about the lock files: the handling of the lock file (hopefully) accords to the specification from http://www.pathname.com/fhs/2.0/fhs-5.5.html.

Device lock files, such as the serial device lock files that were originally found in either /usr/spool/locks or /usr/spool/uucp, must now be stored in /var/lock. The naming convention which must be used is LCK.. followed by the base name of the device. For example, to lock /dev/ttyS0 the file LCK..ttyS0 would be created.

The format used for device lock files must be the HDB UUCP lock file format. The HDB format is to store the process identifier (PID) as a ten byte ASCII decimal number, with a trailing newline. For example, if process 1230 holds a lock file, it would contain the eleven characters: space, space, space, space, space, space, one, two, three, zero, and newline.

Parameters:
PortName the name of the device.
OpenFlags the OR'ed open flags.
Returns:
(v24_port_t*) pointer to a handle of the opened device.
See also:
v24ClosePort, v24SetParameters, v24SetTimeouts
const char* v24PortName ( int  PortNo,
char *  PortName 
)

Build a valid port name. This function can be used to build the platform dependent name of the serial device used by v24OpenPort. The exact format of the name is explained at v24OpenPort.

The parameter PortNo is the number of the port. The parameter PortName is a pointer to a string buffer where the built name is stored. Important: the caller must ensure that the string buffer referenced by PortName can hold V24_SZ_PORTNAME characters.

To ease the use, the function returns the pointer PortName.

Note: this function can be used without an opened device!

Parameters:
PortNo number of the port.
PortName resulting name for v24OpenPort.
Returns:
(const char*) copy of the pointer PortName.
int v24Putc ( v24_port_t port,
unsigned char  TheData 
)

This function simply sends one character. Nothing more and nothing less.

The values returned are: V24_E_OK, V24_E_ILLHANDLE.

Parameters:
port pointer to handle of the opened port.
TheData the character to be sent.
Returns:
(int) the V24_* error code.
int v24Puts ( v24_port_t port,
const char *  Buffer 
)

Send a string. This function simply sends all characters of the ASCIIZ string. A single \n is not expanded to \n\r! The function returns the number of characters sent, or a -1 if something fails. Use v24QueryErrno to retrieve the error code.

Possible error code are V24_OK, V24_E_NULL_POINTER, V24_E_ILLHANDLE or V24_WRITE.

Implementation note: the current release of the library defines the end of a string as a hard coded constant! Search the internal headers EZV24_END_OF_STRING.

Parameters:
port pointer to handle of the opened port.
Buffer a pointer to an ASCIIZ string.
Returns:
(int) number of characters sent.
See also:
v24QueryErrno, v24Putc, v24Write.
int v24QueryErrno ( v24_port_t port  ) 

If possible, all functions return the result of the operation as return code. Some functions don't do this. Therefore, the last error code could be retrieved with this function.

For example: v24Read reports an error by returning a -1. To get the exact error code, the application can call v24QueryErrno.

Parameters:
port pointer to handle of the opened port.
Returns:
the last error code as V24_* constant.
int v24QueryFileHandle ( v24_port_t port  ) 

In some cases it may be necessary to know the file handle returned by the internal open call. To retrieve the current file handle for an opened port, use this function.

Parameters:
port pointer to handle of the opened port.
Returns:
(int) internal used file handle.
const char* v24QueryPortName ( v24_port_t port  ) 

This function returns the platform dependent name of the currently opened device.

Parameters:
port pointer to handle of the opened port.
Returns:
(const char*) the name of the device.
int v24Read ( v24_port_t port,
unsigned char *  Buffer,
size_t  Len 
)

v24Read is the basic function to get one or more received data bytes out of the receive queue. If the queue is empty, the behaviour of the function depends on the used open flags. Without any open flags, the function will wait for the reception of a character. The wait time is limited to the given timeout time. If this limit is exceeded, the function aborts waiting. If nothing is read, the returned value is 0, and the error code is set to V24_E_TIMEOUT. At this level it's no real error, so there is no error report for the debug output (see V24_DEBUG_ON in v24OpenPort).

If the flag V24_NON_BLOCK is used, a empty receive queue leads to an error. The fuctions doesnt wait for the reception of characters.

The parameter Buffer references a buffer that should hold the received characters. The parameter Len is the number of characters to read.

The function returns the number of character read. An error is indicated by a return value of -1. Use v24QueryErrno to get the exact error code.

Possible error code are V24_OK, V24_E_NULL_POINTER, V24_E_ILLHANDLE, V24_E_TIMEOUT or V24_READ.

Note: the caller has to ensure that Buffer can hold the Len characters.

Parameters:
port pointer to handle of the opened port.
Buffer pointer to buffer to hold the data.
Len number of characters to receive.
Returns:
(int) number of received characters or -1.
See also:
v24QueryErrno, v24Getc, v24Gets.
int v24SetDTR ( v24_port_t port,
int  NewState 
)

Set the state of the DTR line according to the parameter NewState. If NewsState is 0 the DTR signal is unset, otherwise it is set.

Parameters:
port pointer to handle of the opened port.
NewState the new state to set DTR to.
Returns:
(int) the V24_* error code.
int v24SetParameters ( v24_port_t port,
int  Baudrate,
int  Datasize,
int  Parity 
)

After a serial device is opened, the user have to setup the communication parameter. The parameter port references the handle of the opened device. With Baudrate, the speed can be set as bitrate (bits per second). Use the __EZV24_BAUDRATE values as parameter. The size of a transmitted data byte can be passed as __EZV24_DATASIZE value to Datasize. The last parameter Parity defines the parity checking. The possible values are defined in __EZV24_PARITY_FLAGS.

The number of stop bits is set to 1. If you need 2 stop bits, you have to call v24SetStopbits.

The values returned are: V24_E_ILLHANDLE, V24_E_ILLBAUD, V24_E_ILLDATASZ, V24_E_ILLPARITY and V24_E_OK.

Parameters:
port pointer to handle of the opened port.
Baudrate a V24_* (__EZV24_BAUDRATE) constant representing the baudrate.
Datasize a V24_* (__EZV24_DATASIZE) constant representing the size of a data byte.
Parity a V24_* (__EZV24_PARITY_FLAGS) constant representing the parity check mode.
Returns:
(int) the V24_* error code.
int v24SetRTS ( v24_port_t port,
int  NewState 
)

Set the state of the RTS line according to the parameter NewState. If NewsState is 0 the RTS signal is unset, otherwise it is set. This is only allowed if the port doesn't use hardware handshaking. This means v24OpenPort isn't called with V24_RTS_CTS. If hardware handshaking is used while calling this function, it aborts and returns V24_E_ILLPARM as error.

The values returned are: V24_E_OK, V24_E_ILLHANDLE, V24_E_ILLPARM, V24_E_NOT_IMPLEMENTED.

Parameters:
port pointer to handle of the opened port.
NewState the new state to set DTR to.
Returns:
(int) the V24_* error code.
int v24SetStopbits ( v24_port_t port,
int  Stops 
)

Setup the number of stop bits. This function should be called directly after v24SetParameters. The parameter Stops have to be set to the correct number of stopbits. Valid values are 1 and 2.

The values returned are: V24_E_OK, V24_E_ILLHANDLE, V24_E_ILLPARM, V24_E_NOT_INIT.

Parameters:
port pointer to handle of the opened port.
Stops the number of stop bits.
Returns:
(int) the V24_* error code.
See also:
v24SetParameters
int v24SetTimeouts ( v24_port_t port,
int  TenthOfSeconds 
)

All read functions may use a timeout mechanism while waiting for characters. If this time limit is exceeded, the function abort reading. The timeout mechanism is only active if the open flag V24_NON_BLOCK isn't specified.

The timeout duration is specified by the parameter TenthOfSeconds. This value is given as a multiple of a tenth of a second.

The values returned are: V24_E_OK, V24_E_ILLHANDLE, V24_E_ILLTIMEOUT, V24_E_NOT_INIT.

Parameters:
port pointer to handle of the opened port.
TenthOfSeconds the timeout duration.
Returns:
(int) the V24_* error code.
int v24Write ( v24_port_t port,
const unsigned char *  Buffer,
size_t  Len 
)

Send a buffer. This function sends all Len characters of the array referenced by Buffer. The number of sent bytes is returned. If an error occurs, a -1 is returned. Use v24QueryErrno to retrieve the error code.

Possible error code are V24_OK, V24_E_NULL_POINTER, V24_E_ILLHANDLE or V24_WRITE.

Parameters:
port pointer to handle of the opened port.
Buffer pointer to buffer.
Len number of characters to send.
Returns:
(int) number of sent characters or -1.
See also:
v24QueryErrno, v24Putc, v24Puts.

Generated on Tue Apr 12 10:20:56 2011 for ezV24 by  doxygen 1.6.1