📄 port.c_backup
字号:
/******************************************************************************** PORT OPERATION* * Filename : Port.c** Description : functions used for operating of port ** Author : Jun Jiang** Date : 2008.09.27********************************************************************************//********************************************************************************* INCLUDE HEADER********************************************************************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h> // Unix standard function definition#include <sys/types.h>#include <string.h>#include <sys/stat.h>#include <fcntl.h> // Archive control definition#include <termios.h> // Terminal I/O definition#include <errno.h> // Error number definition#include "Port.h"/********************************************************************************* MACRO DEFINITION ********************************************************************************/#define WAIT_RECV(len, baud) \ do { \ if ( (len * 10) / baud ) \ sleep( (len * 10) / baud ); \ if ( (len * 10) % baud ) { \ usleep( ((len * 10) % baud) * 10000 / (baud / 100) ); \ } \ } while (0)/********************************************************************************* VARIABLE DEFINITION ********************************************************************************/int SpdArray[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300};int NameArray[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300};/******************************************************************************** SET PORT * * Description: setting parity check for serial port line control. * * Arguments : int fd handler of device* int databits data bit number of serial line* int stopbits stop bit number of serial line* int parity parity check bit of serial line* * Return : int* 0 setting successful* -1 setting failure** Note : ********************************************************************************/int SetPort(int fd, int databits, int stopbits, int parity){ struct termios options; bzero(&options, sizeof(options)); // set 0 into the area if ( tcgetattr(fd, &options) != 0) { perror("SetupSerial 1"); return(-1); }// cfmakeraw(&options); // set terminal as raw mode options.c_cflag |= CLOCAL; // control mode, ensure no-occupied options.c_cflag |= CREAD; // control mode, ensure readable options.c_cflag &= ~CSIZE; // set data bit switch (databits) { case 7: options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS8; break; default: fprintf(stderr,"Unsupported data sizen"); return (-1); } options.c_cflag &= ~CRTSCTS; // no flow control switch (parity) { case 'n': case 'N': options.c_cflag &= ~PARENB; // clear parity enable break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); // Configed to Odd checking. break; case 'e': case 'E': options.c_cflag |= PARENB; // Enable parity */ options.c_cflag &= ~PARODD; // configed to even checking. break; default: fprintf(stderr,"Unsupported parityn"); return (-1); } switch (stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: fprintf(stderr,"Unsupported stop bitsn"); return (-1); } options.c_oflag &= ~OPOST; // raw data output tcflush(fd, TCIFLUSH); // rx but do not read overflowed data options.c_cc[VTIME] = 1; // waiting time for first byte(unit:100ms)) options.c_cc[VMIN] = 1; // minimum of reading data fcntl(fd, F_SETFL, FNDELAY); // set "read" is not block mode if (tcsetattr(fd, TCSANOW, &options) != 0) { perror("SetupSerial failed"); return (-1); } return (0);}/******************************************************************************** OPEN DEVICE* * Description: Open a device of Linux system * * Arguments : char* DevName name string of device* * Return : int* -1 open failured* otherwise handler of device** Note : ********************************************************************************/int OpenDev(char* Dev){// int fd = open(Dev, O_RDWR);// int fd = open(Dev, O_RDWR | O_NOCTTY | O_NONBLOCK); int fd = open(Dev, O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY); if (-1 == fd) { perror("Can't Open Serial Port"); return -1; } else return fd;}/******************************************************************************** SET SPEED* * Description: Set speed of serial port* * Arguments : int fd handler of device* int speed speed of serial port* * Return : int* 0 success* -1 failed ** Note : ********************************************************************************/int SetSpeed(int fd, int speed){ int i; int status; struct termios Opt; tcgetattr(fd, &Opt); for ( i= 0; i < sizeof(SpdArray) / sizeof(SpdArray[0]); i++) { if (speed == NameArray[i]) { tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, SpdArray[i]); cfsetospeed(&Opt, SpdArray[i]); status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) { perror("tcsetattr fd"); return -1; } tcflush(fd, TCIOFLUSH); } // if } // for return 0;}/******************************************************************************** WRITE COM* * Description: sending data by comm* * Arguments : int fd handler of comm device* char* p_bff pointer of buffer* int len length of sended data* * Return : int* -1 transmit failed* writelen length of transmitted data* * Note : ********************************************************************************/int WriteCom(int fd, char* p_bff, int len){ int length = 0; length = write(fd, p_bff, len); if (length == len) return len; // transmitted success else { tcflush(fd, TCOFLUSH); // flush queue of output return -1; }}/******************************************************************************** READ COM* * Description: receive data from comm* * Arguments : int fd handler of comm device* char* p_bff pointer of received buffer* int len length of sended data* int baudrate baud rate of port* int timeout value of timeout(unit:1ms)* * Return : int* -1 timeout* -2 select function error* readlen length of received data* * Note : ********************************************************************************/int ReadCom(int fd, char* p_bff, int len, int baudrate, int timeout){ int readlen; int fs_sel; fd_set fs_read; struct timeval tv_timeout; FD_ZERO(&fs_read); FD_SET(fd, &fs_read); // get handler's monited data tv_timeout.tv_sec = timeout / 1000; tv_timeout.tv_usec = (timeout % 1000) * 1000; fs_sel = select(fd + 1, &fs_read, NULL, NULL, &tv_timeout); // trigerred by the changing of handler's readable status if (fs_sel > 0) { // can been read WAIT_RECV(len, baudrate); // waiting for receiving finished. readlen = read(fd, p_bff, len); return readlen; } else if (0 == fs_sel) { return -1; // timeout! } else { return -2; // select function error! }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -