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.
Definition in file serial.c.
#include "serial.h"
Include dependency graph for serial.c:
Go to the source code of this file.
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 | |
Variables | |
static int | fd_map [MAX_PORT_NUM] = { -1, -1, -1, -1, -1, -1, -1, -1} |
-1 means SERIAL_ERROR_FD | |
static struct termios | newtio [MAX_PORT_NUM] |
static struct termios | oldtio [MAX_PORT_NUM] |
|
use port number to find out the fd of the port
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 }
|
|
block read from serial port
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: ![]() |
|
close serial port
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: ![]() |
|
test how much data in input queue
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: ![]() |
|
set flow control
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: ![]() |
|
non-block read from serial port
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: ![]() |
|
open serial port
< 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 }
|
|
set serial port mode for RS232/RS422/RS485
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: ![]() |
|
set serial port parameter
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: ![]() |
|
set serial speed and make changes now
< 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: ![]() |
|
write to serial port
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: ![]() |
|
-1 means SERIAL_ERROR_FD
Definition at line 21 of file serial.c. Referenced by FindFD(), SerialClose(), and SerialOpen(). |
|
Definition at line 20 of file serial.c. Referenced by SerialFlowControl(), SerialOpen(), SerialSetParam(), and SerialSetSpeed(). |
|
Definition at line 20 of file serial.c. Referenced by SerialClose(), and SerialOpen(). |