Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

serial.c File Reference

Low level serial port handlers. More...

#include "serial.h"
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

Defines

#define debug(prg)   { if (debug_flag) { prg } }
 Run debug code if debugging is enabled.


Functions

int SERGetBaud (const char *baud_string, speed_t *serial_speed)
 Get the baud rate from a string, and return the speed_t value.

SerialPortnew_SerialPort ()
 Create and initialise a new SerialPort structure.

SerialPortSEROpenPort (const char *serialportname, speed_t serial_speed, char *logfilename)
 Open a serial port, and set the baud rate.

void SERClosePort (SerialPort *sp)
 Close and free a serial port.

BYTE SERGetByte (SerialPort *sp)
 Get a byte from a serial port.

void SERPutByte (SerialPort *sp, BYTE b)
 Put a byte to a serial port.

int SERPutString (SerialPort *sp, const char *s)
 Put a string of bytes to a serial port.

void SERFlushChannel (SerialPort *sp, int usec)
 Clear any bytes that arrive at a serial port for a period of time.

int SERQueryChannel (SerialPort *sp, int usec)
 Test whether there is data waiting on a serial port.

int SERGetByteTimeout (SerialPort *sp, int usec)
 Read a byte from a serial port with a timeout.

int SERGetBytesTimeout (SerialPort *sp, BYTE *buffer, int count, int usec)
 Read some bytes from a serial port with a timeout.


Variables

const int debug_flag = 0
 Control whether debug code is run.


Detailed Description

Low level serial port handlers.

Copyright (C) 2003, 2004 Kirk Martinez, Alistair Riddoch, The University of Southampton


Function Documentation

SerialPort* new_SerialPort  )  [static]
 

Create and initialise a new SerialPort structure.

For internal use only.

Returns:
a pointer to the new serial port structure on the heap.

void SERClosePort SerialPort sp  ) 
 

Close and free a serial port.

Cleans up, closes and deletes a serial port. If the port had a log file, close the file.

Parameters:
sp serial port to be closed.

void SERFlushChannel SerialPort sp,
int  usec
 

Clear any bytes that arrive at a serial port for a period of time.

Use select to wait for data to arrive at the serial port, and then read all data available.

Parameters:
sp serial port to read from.
usec maximum number of microseconds to wait if no data is available immediatly.

int SERGetBaud const char *  baud_string,
speed_t *  serial_speed
 

Get the baud rate from a string, and return the speed_t value.

Accepts the baud name as a string. Any of the standard baud rates supported by standard serial ports should be accepted.

Parameters:
baud_string is a string describing the baud rate
serial_speed is a pointer used to return the speed
Returns:
zero if baud rate is recognised, non-zero otherwise

BYTE SERGetByte SerialPort sp  ) 
 

Get a byte from a serial port.

Read a byte from a serial port. This blocks if no data is available.

Parameters:
sp serial port to read from.
Returns:
value of byte read.

int SERGetBytesTimeout SerialPort sp,
BYTE *  buffer,
int  count,
int  usec
 

Read some bytes from a serial port with a timeout.

Wait at most usec microseconds for data to be available on the given serial port, and then read some bytes if it they are available. This is repeated until enough bytes have been read. Behavoir on timeout is a little weird. Needs to be examined if you plan to use this code.

Parameters:
sp serial port to read from.
buffer pointer to buffer where bytes are stored.
count number of bytes to read.
usec number of microseconds to wait.
Returns:
number of bytes read.

int SERGetByteTimeout SerialPort sp,
int  usec
 

Read a byte from a serial port with a timeout.

Wait at most usec microseconds for data to be available on the given serial port, and then read one byte if it is available.

Parameters:
sp serial port to read from.
usec number of microseconds to wait.
Returns:
value of byte read, or -1 if a timeout occured.

SerialPort* SEROpenPort const char *  serialportname,
speed_t  serial_speed,
char *  logfilename
 

Open a serial port, and set the baud rate.

Parameters:
serialportname string giving the filename of the serial device
serial_speed baud rate to use
logfilename string giving the filename of a log file to be used in association with this serial port.
Returns:
a pointer to the new serial port structure on the heap.

void SERPutByte SerialPort sp,
BYTE  b
 

Put a byte to a serial port.

Write a byte to a serial port. This blocks if the serial port is not ready to accept new data.

Parameters:
sp serial port to write to.
b value of byte to be written.

int SERPutString SerialPort sp,
const char *  s
 

Put a string of bytes to a serial port.

Write a sequence of bytes to a serial port. This blocks if the serial port is not ready to accept new data.

Parameters:
sp serial port to write to.
s pointer to string of bytes to be written
Returns:
zero if write succeeded, non-zero otherwise.

int SERQueryChannel SerialPort sp,
int  usec
 

Test whether there is data waiting on a serial port.

Use select to wait for data to be available at the serial port for a certain period of time.

Parameters:
sp serial port to check.
usec maximum time in microseconds to wait for data.
Returns:
one if data is now available, zero otherwise.


Variable Documentation

const int debug_flag = 0 [static]
 

Control whether debug code is run.

Set to non-zero if debug code should be run.


Generated on Mon Aug 9 13:41:45 2004 for Glacsweb by doxygen 1.3.5