Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

# serial.h File Reference

## Detailed Description

Serial API header file.

Serial utility functions, it helps programmer easily operating serial port. It provides mostly standard functions we usually use, and SerialSetMode() is the private function can be use in UC box.

History: Date Author Comment 08-01-2005 AceLan Kao. Create it.

Author:
  AceLan Kao.(acelan_kao@moxa.com.tw)

Definition in file serial.h.

`#include <stdio.h>`
`#include <string.h>`
`#include <unistd.h>`
`#include <fcntl.h>`
`#include <errno.h>`
`#include <termios.h>`
`#include <asm/ioctls.h>`
`#include "moxadevice.h"`

Include dependency graph for serial.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Defines
#define | HW_FLOW_CONTROL 1
#define | MAX_PORT_NUM 8
#define | PORT1 0
#define | PORT2 1
#define | PORT3 2
#define | PORT4 3
#define | PORT5 4
#define | PORT6 5
#define | PORT7 6
#define | PORT8 7
#define | SERIAL_ERROR_FD -1
| Could not find the fd in the map, device not opened.
#define | SERIAL_ERROR_OPEN -2
| Could not open the port or port has been opened.
#define | SERIAL_OK 0
#define | SerialRead SerialBlockRead
#define | SW_FLOW_CONTROL 2
## Functions
int | FindFD (int port)
| use port number to find out the fd of the port
int | SerialBlockRead (int port, char *buf, int len)
| block read from serial port
int | SerialClose (int port)
| close serial port
int | SerialDataInInputQueue (int port)
| test how much data in input queue
int | SerialFlowControl (int port, int control)
| set flow control
int | SerialNonBlockRead (int port, char *buf, int len)
| non-block read from serial port
int | SerialOpen (int port, int open_flag, struct termios *tio)
| open serial port
int | SerialSetMode (int port, unsigned int mode)
| set serial port mode for RS232/RS422/RS485
int | SerialSetParam (int port, int parity, int databits, int stopbit)
| set serial port parameter
int | SerialSetSpeed (int port, unsigned int speed)
| set serial speed and make changes now
int | SerialWrite (int port, char *str, int len)
| write to serial port

## Define Documentation

#define HW_FLOW_CONTROL 1

| Definition at line 42 of file serial.h. Referenced by SerialFlowControl().

#define MAX_PORT_NUM 8

| Definition at line 40 of file serial.h.

#define PORT1 0

| Definition at line 31 of file serial.h.

#define PORT2 1

| Definition at line 32 of file serial.h.

#define PORT3 2

| Definition at line 33 of file serial.h.

#define PORT4 3

| Definition at line 34 of file serial.h.

#define PORT5 4

| Definition at line 35 of file serial.h.

#define PORT6 5

| Definition at line 36 of file serial.h.

#define PORT7 6

| Definition at line 37 of file serial.h.

#define PORT8 7

| Definition at line 38 of file serial.h.

#define SERIAL_ERROR_FD -1

| Could not find the fd in the map, device not opened. Definition at line 48 of file serial.h. Referenced by FindFD().

#define SERIAL_ERROR_OPEN -2

| Could not open the port or port has been opened. Definition at line 49 of file serial.h. Referenced by SerialOpen().

#define SERIAL_OK 0

| Definition at line 47 of file serial.h. Referenced by SerialClose(), SerialFlowControl(), SerialSetParam(), and SerialSetSpeed().

#define SerialRead SerialBlockRead

| Definition at line 45 of file serial.h.

#define SW_FLOW_CONTROL 2

| Definition at line 43 of file serial.h. Referenced by SerialFlowControl().

## Function Documentation

int FindFD | ( | int | port | ) |

  | use port number to find out the fd of the port Parameters:
  | port | port number
Returns: fd which is opened by using SerialOpen() Definition at line 30 of file serial.c. References fd_map, and SERIAL_ERROR_FD. Referenced by MBSerialReadASCII(), MBSerialReadRTU(), serial_master(), SerialBlockRead(), SerialClose(), SerialDataInInputQueue(), SerialFlowControl(), SerialNonBlockRead(), SerialSetMode(), SerialSetParam(), SerialSetSpeed(), and SerialWrite().
    00031 {
    00032         if( fd_map[ port] == -1)
    00033                 return SERIAL_ERROR_FD;
    00034
    00035         return fd_map[ port];
    00036 }

int SerialBlockRead | ( | int | port,
|  | char * | buf,
|  | int | len
| ) |

  | block read from serial port Parameters:
  | port | port number
  | buf | input buffer
  | len | buffer length
Returns: return length of read str for success, on error return error code < error Definition at line 124 of file serial.c. References FindFD(). Referenced by MBSerialReadASCII(), and MBSerialReadRTU().
    00125 {
    00126         int res= 0;
    00127         int bytes= 0;
    00128         int fd= FindFD( port);
    00129
    00130         if( fd < 0)
    00131                 return fd;
    00132
    00133         fcntl( fd, F_SETFL, 0);
    00134         res = read( fd, buf, len);
    00135         return res;
    00136 }
Here is the call graph for this function:

int SerialClose | ( | int | port | ) |

  | close serial port Parameters:
  | port | port number
Returns: return SERIAL_OK for success, on error return error code < error Definition at line 145 of file serial.c. References fd_map, FindFD(), oldtio, and SERIAL_OK. Referenced by MBSerialClose().
    00146 {
    00147         int fd= FindFD( port);
    00148
    00149         if( fd < 0)
    00150                 return fd;
    00151
    00152         tcsetattr( fd, TCSANOW, &(oldtio[ port]));
    00153         close( fd);
    00154
    00155         fd_map[ port]= -1;
    00156
    00157         return SERIAL_OK;
    00158 }
Here is the call graph for this function:

int SerialDataInInputQueue | ( | int | port | ) |

  | test how much data in input queue Parameters:
  | port | port number
Returns: return number of data to be read for success, on error return error code < error Definition at line 168 of file serial.c. References FindFD(). Referenced by MBSerialReadASCII(), MBSerialReadRTU(), and serial_master().
    00169 {
    00170         int bytes= 0;
    00171         int fd= FindFD( port);
    00172
    00173         if( fd < 0)
    00174                 return fd;
    00175
    00176         ioctl( fd, FIONREAD, &bytes);
    00177         return bytes;
    00178 }
Here is the call graph for this function:

int SerialFlowControl | ( | int | port,
|  | int | control
| ) |

  | set flow control Parameters:
  | port | port number
  | control | HW_FLOW_CONTROL or SW_FLOW_CONTROL
Returns: return SERIAL_OK for success, on error return error code < error Definition at line 188 of file serial.c. References FindFD(), HW_FLOW_CONTROL, newtio, SERIAL_OK, and SW_FLOW_CONTROL. Referenced by MBSerialFlowCtrl().
    00189 {
    00190         int fd= FindFD( port);
    00191
    00192         if( fd < 0)
    00193                 return fd;
    00194
    00195         if( control == HW_FLOW_CONTROL)
    00196                 newtio[ port].c_cflag |= CRTSCTS;
    00197         else if( control == SW_FLOW_CONTROL)
    00198                 newtio[ port].c_iflag |= (IXON | IXOFF | IXANY);
    00199
    00200         tcflush( fd, TCIFLUSH);
    00201         tcsetattr( fd, TCSANOW, &newtio[ port]);
    00202
    00203         return SERIAL_OK;
    00204 }
Here is the call graph for this function:

int SerialNonBlockRead | ( | int | port,
|  | char * | buf,
|  | int | len
| ) |

  | non-block read from serial port Parameters:
  | port | port number
  | buf | input buffer
  | len | buffer length
Returns: return length of read str for success, on error return error code < error Definition at line 100 of file serial.c. References FindFD(). Referenced by MBSerialNonBlockRead().
    00101 {
    00102         int res= 0;
    00103         int bytes= 0;
    00104         int fd= FindFD( port);
    00105
    00106         if( fd < 0)
    00107                 return fd;
    00108
    00109         fcntl( fd, F_SETFL, FNDELAY);
    00110         res = read( fd, buf, len);
    00111         return res;
    00112 }
Here is the call graph for this function:

int SerialOpen | ( | int | port,
|  | int | open_flag,
|  | struct termios * | tio
| ) |

  | open serial port Parameters:
  | port | port number
  | open_flag | flag used in open() function
  | tio | termios structure
Returns: return fd for success, on error return error code < port already opened < save current serial port settings Definition at line 47 of file serial.c. References fd_map, newtio, oldtio, and SERIAL_ERROR_OPEN. Referenced by MBSerialOpen().
    00048 {
    00049         int fd;
    00050         char device[ 80];
    00051
    00052         if( fd_map[ port] != -1)
    00053                 return SERIAL_ERROR_OPEN;
    00054
    00055         sprintf( device, "/dev/ttyM%d", port);
    00056         fd = open( device, open_flag);
    00057         if( fd <0)
    00058                 return SERIAL_ERROR_OPEN;
    00059
    00060         fd_map[ port]= fd;
    00061
    00062         tcgetattr( fd, &(oldtio[ port]));
    00063         newtio[ port]= *tio;
    00064
    00065         tcflush( fd, TCIFLUSH);
    00066         tcsetattr( fd, TCSANOW, &newtio[ port]);
    00067
    00068         return fd;
    00069 }

int SerialSetMode | ( | int | port,
|  | unsigned int | mode
| ) |

  | set serial port mode for RS232/RS422/RS485 Parameters:
  | port | port number
  | mode | serial port mode {RS232_MODE/RS485_2WIRE_MODE/RS422_MODE/RS485_4WIRE_MODE}
Returns: return SERIAL_OK for success, on error return error code < error Definition at line 250 of file serial.c. References FindFD(). Referenced by MBSerialSetMode().
    00251 {
    00252         int fd= FindFD( port);
    00253         if( fd < 0)
    00254                 return fd;
    00255
    00256         return ioctl( fd, MOXA_SET_OP_MODE, &mode);
    00257 }
Here is the call graph for this function:

int SerialSetParam | ( | int | port,
|  | int | parity,
|  | int | databits,
|  | int | stopbit
| ) |

  | set serial port parameter Parameters:
  | port | port number
  | parity | parity check, 0: none, 1: odd, 2: even, 3: space
  | databits | data bits
  | stopbit | stop bit
Returns: return fd for success, on error return error code < error Definition at line 269 of file serial.c. References FindFD(), newtio, and SERIAL_OK. Referenced by MBSerialSetParam().
    00270 {
    00271         int fd= FindFD( port);
    00272         if( fd < 0)
    00273                 return fd;
    00274
    00275         if( parity == 0)
    00276                 newtio[ port].c_cflag &= ~PARENB;
    00277         else if( parity == 1)
    00278         {
    00279                 newtio[ port].c_cflag |= PARENB;
    00280                 newtio[ port].c_cflag |= PARODD;
    00281         }
    00282         else if( parity == 2)
    00283         {
    00284                 newtio[ port].c_cflag |= PARENB;
    00285                 newtio[ port].c_cflag &= ~PARODD;
    00286         }
    00287         else if( parity == 3)
    00288         {
    00289                 newtio[ port].c_cflag &= ~PARENB;
    00290                 newtio[ port].c_cflag &= ~CSTOPB;
    00291         }
    00292
    00293         if( databits == 5)
    00294                 newtio[ port].c_cflag |= CS5;
    00295         else if( databits == 6)
    00296                 newtio[ port].c_cflag |= CS6;
    00297         else if( databits == 7)
    00298                 newtio[ port].c_cflag |= CS7;
    00299         else if( databits == 8)
    00300                 newtio[ port].c_cflag |= CS8;
    00301
    00302         if( stopbit == 1)
    00303                 newtio[ port].c_cflag &= ~CSTOPB;
    00304         else if( stopbit == 2)
    00305                 newtio[ port].c_cflag |= CSTOPB;
    00306
    00307         tcflush( fd, TCIFLUSH);
    00308         tcsetattr( fd, TCSANOW, &newtio[ port]);
    00309
    00310         return SERIAL_OK;
    00311 }
Here is the call graph for this function:

int SerialSetSpeed | ( | int | port,
|  | unsigned int | speed
| ) |

  | set serial speed and make changes now Parameters:
  | port | port number
  | speed | unsigned integer for new speed
Returns: return SERIAL_OK for success, on error return error code < error < i start from 1, bellow 50 will be set to B0 Definition at line 214 of file serial.c. References FindFD(), newtio, and SERIAL_OK. Referenced by MBSerialSetSpeed(), serial_master(), and serial_slave().
    00215 {
    00216         int i, table_size= 23;
    00217         int speed_table1[]={ 0, 50, 75, 110, 134, 150, 200, 300,
    00218                              600, 1200, 1800, 2400, 4800, 9600,
    00219                              19200, 38400, 57600, 115200, 230400,
    00220                              460800, 500000, 576000, 921600};
    00221         int speed_table2[]={ B0, B50, B75, B110, B134, B150, B200, B300,
    00222                              B600, B1200, B1800, B2400, B4800, B9600,
    00223                              B19200, B38400, B57600, B115200, B230400,
    00224                              B460800, B500000, B576000, B921600};
    00225         int fd= FindFD( port);
    00226
    00227         if( fd < 0)
    00228                 return fd;
    00229
    00230         for( i= 1; i< table_size; i++)
    00231                 if( speed_table1[ i] >= speed)
    00232                         break;
    00233
    00234         cfsetispeed( &newtio[ port], speed_table2[ i]);
    00235         cfsetospeed( &newtio[ port], speed_table2[ i]);
    00236         tcsetattr( fd, TCSANOW, &newtio[ port]);
    00237
    00238         return SERIAL_OK;
    00239 }
Here is the call graph for this function:

int SerialWrite | ( | int | port,
|  | char * | str,
|  | int | len
| ) |

  | write to serial port Parameters:
  | port | port number
  | str | string to write
  | len | length of str
Returns: return length of str for success, on error return error code < error Definition at line 80 of file serial.c. References FindFD(). Referenced by MBSerialWrite().
    00081 {
    00082         int fd= FindFD( port);
    00083
    00084         if( fd < 0)
    00085                 return fd;
    00086
    00087         return write( fd, str, len);
    00088 }
Here is the call graph for this function: