【代碼備份01】
uart.c
/******************************************************************************* Copyright (C), *** Tech. Co., Ltd. FileName: uart.c Author: Version : Date: 2008-1-11 Description: 串口的通用操作文件 other: Function List: History: // 歷史修改記錄 1. Date: Author: Version: Modification: *******************************************************************************/ #include "uart.h" #include "system.h" int SpeedArr[6] = { B1200, B1800, B2400, B4800, B9600, B19200 }; /******************************************************************************* Function: UartOpen Description: 初始化串口 Calls: Called By: Global Accessed: Global Updated: Input: 1.UartInitParam:串口初始化參數 Output: Return: -1為失敗, 成功返回串口的文件描述符 Others: *******************************************************************************/ int UartOpen (int UartIndex, UART485_INIT_CONFIG *pUartInitConfig, int *pUartFd) { #ifndef MCU_I686 #else //上位機模擬 char dev[20]; struct termios opt; sprintf (dev, "/dev/ttyS%d", UartIndex); if (((*pUartFd) = open (dev, O_RDWR | O_NOCTTY)) < 0) { printf ("can not open ttyS%d device\n", UartIndex); return -1; } tcgetattr ((*pUartFd), &opt); //設置波特率 if ((pUartInitConfig->speed) > 5) return -1; cfsetispeed (&opt, SpeedArr[(int) pUartInitConfig->speed]); cfsetospeed (&opt, SpeedArr[(int) pUartInitConfig->speed]); //設置數據位 opt.c_cflag &= (~CSIZE); switch (pUartInitConfig->databit) { case 7: opt.c_cflag |= CS7; break; case 8: opt.c_cflag |= CS8; break; default: printf ("Unsupported data size\n"); return -1; } //設置停止位 switch (pUartInitConfig->stopbit) { case 1: opt.c_cflag &= ~CSTOPB; break; case 2: opt.c_cflag |= CSTOPB; break; default: printf ("Unsupported stop bits\n"); return -1; } //設置校驗位 switch (pUartInitConfig->verify) { case 'n': opt.c_cflag &= ~PARENB; opt.c_iflag &= ~INPCK; break; case 'o': opt.c_cflag |= (PARODD | PARENB); opt.c_iflag |= INPCK; break; case 'e': opt.c_cflag |= PARENB; opt.c_cflag &= ~PARODD; opt.c_iflag |= INPCK; break; default: printf ("Unsupported parity\n"); return -1; } //設置超時時間 opt.c_cc[VTIME] = 10; opt.c_cc[VMIN] = 0; //設置為RAW模式 opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); opt.c_iflag &= ~(IXON | IXOFF | ICRNL); opt.c_oflag &= ~OPOST; tcflush ((*pUartFd), TCIOFLUSH); return tcsetattr ((*pUartFd), TCSANOW, &opt); #endif } /******************************************************************************* Function: UartClose Description: 關閉串口 Calls: Called By: Global Accessed: Global Updated: Input: 1.UartFd:串口的文件描述符 Output: Return: -1為失敗 Others: *******************************************************************************/ int UartClose (int UartFd) { return close (UartFd); } /******************************************************************************* Function: UartSend Description: 串口的發送函數 Calls: Called By: Global Accessed: Global Updated: Input: 1.UartFd:串口的文件描述符 2.SendBuff:發送緩沖區 3.SendNum:發送字節數 Output: Return: -1為失敗, Others: *******************************************************************************/ int UartSend (int UartFd, unsigned char *pSendBuff, int SendNum) { int n_send, write_num; for (n_send = 0; n_send < SendNum;) { write_num = write (UartFd, &pSendBuff[n_send], SendNum - n_send); if (write_num <= 0) //發送失敗 return -1; n_send = n_send + write_num; } return n_send; } /******************************************************************************* Function: UartRcv Description: 串口的接收函數 Calls: Called By: Global Accessed: Global Updated: Input: 1.UartFd:串口的文件描述符 2.RcvBuff:接收緩沖區 3.RcvNum:接收字節數 Output: Return: -1為失敗, Others: *******************************************************************************/ int UartRcv (int UartFd, unsigned char *pRcvBuff, int RcvNum, int OverTime) { #ifndef MCU_I686 #else int n_rcv, read_num; for (n_rcv = 0; n_rcv < RcvNum;) { read_num = read (UartFd, &pRcvBuff[n_rcv], RcvNum - n_rcv); if (read_num <= 0) //接收超時或失敗 return -1; n_rcv = n_rcv + read_num; } return n_rcv; #endif } /******************************************************************************* Function: UartClearRcvBuf Description: 串口緩沖區的清空函數 Calls: Called By: Global Accessed: Global Updated: Input: 1.UartFd:串口的文件描述符 Output: Return: -1為失敗, Others: *******************************************************************************/ int UartClearRcvBuf (int UartFd) { #ifndef MCU_I686 #else return tcflush (UartFd, TCIOFLUSH); #endif } View Code
uart.h
/******************************************************************************* Copyright (C), *** Tech. Co., Ltd. File name: uart.h Author: Version: Date: 2008-1-11 Description: 串口通用操作的頭文件 Others: Function List: 1. int UartOpen( UART_INIT_PARAM UartInitParam ); 2. int UartClose( int UartFd ); 3. int UartSend( int UartFd, unsigned char *SendBuff, int SendNum ); 4. int UartRcv( int UartFd, unsigned char *RcvBuff, int RcvNum ); 5. int UartRcv( int UartFd ); History: 1. Date: Author: Modification: *******************************************************************************/ #ifndef _UART_H_ #define _UART_H_ #include <stdio.h> #include <sys/time.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <time.h> #include <pthread.h> #include <string.h> #include <stdlib.h> #include <dirent.h> #include <semaphore.h> #include <mntent.h> #include <sys/vfs.h> #include <getopt.h> #include <termios.h> #include <errno.h> #include <sys/ioctl.h> #include <linux/ioctl.h> extern int SpeedArr[6]; typedef struct _UART485_INIT_CONFIG { char speed; //串口速度, 0-5代表1200,1800,2400,4800,9600,19200 char databit; //串口數據位 5,6,7或8 char stopbit; //串口停止位 1或2 char verify; //串口校驗位 'n','o','e'分別代表無校驗,奇校驗,偶校驗 } UART485_INIT_CONFIG; int UartOpen (int UartIndex, UART485_INIT_CONFIG *pUartInitConfig, int *pUartFd); int UartClose (int UartFd); int UartSend (int UartFd, unsigned char *pSendBuff, int SendNum); int UartRcv (int UartFd, unsigned char *pRcvBuff, int RcvNum, int OverTime); int UartClearRcvBuf (int UartFd); #endif //end _UART_H_ View Code
【代碼備份02】
【完結】