/***********************************************************************
* $Id$
* Project: Power System Monitor
* File:
* Description:
*
* @version: V1.0
* @date: 26th March 2012
*
*-----------------------------------------------------------------
*
* Revise:
* date version author description
*
*
*
*
**********************************************************************/
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include "./typedef.h"
#include "./rs485.h"
/*********************************************************************************************************
* Function name: port_speed
* Descriptions:
* input parameters:
* output parameters:
* Returned value:
*********************************************************************************************************/
uint16_t port_speed(int fd, int speed)
{
const int speed_arr[] = {B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,};
const int name_arr[] = {115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300,};
int i;
int status;
struct termios Opt;
DEBUGMSG(0, ("%s ... %d\n", __FUNCTION__, speed));
tcgetattr(fd, &Opt);
for( i= 0; i < sizeof(speed_arr)/sizeof(int); i++) {
if(speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if(status != 0) {
perror("tcsetattr fd1");
}
return TRUE;
}
tcflush(fd, TCIOFLUSH);
}
return FALSE;
}
/*********************************************************************************************************
* Function name: port_set
* Descriptions:
* input parameters:
* output parameters:
* Returned value:
*********************************************************************************************************/
int port_set(int fd, int databits, int stopbits, int parity)
{
DEBUGMSG(0,(" fd:%d...stopbits:%d ...parity:%c....\n", fd, stopbits, parity));
struct termios options;
if(tcgetattr(fd, &options) != 0) {
perror("SetupSerial 1");
return FALSE;
}
options.c_cflag &= ~CSIZE;
switch (databits) { /*设置数据位数*/
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return FALSE;
}
switch (parity) {
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Clear parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* enable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* enable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return FALSE;
}
switch (stopbits) { /* 设置停止位*/
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return FALSE;
}
/* Set input parity option */ /*have set it befor*/
// if (parity != 'n')
// options.c_iflag |= INPCK;
options.c_iflag = 0; /*直接设置为0最方便;否则以前的设置会影响接收*/
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST; /*Output*/
tcflush(fd, TCIFLUSH);
/* unit : 100ms or 1/10秒 设置超时 50 = 5 seconds; 2 = 200ms*/
options.c_cc[VTIME] = 1;
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd, TCSANOW, &options) != 0) {
perror(" SetupSerial err");
return FALSE;
}
return TRUE;
}
/*********************************************************************************************************
** Function name: WhichisReadable
** Descriptions: find which fd can be read
** input parameters: *fd : 串口描述符指针
** nmsTimeOut : milliseconds
** output parameters:
** Returned value: 1 : data ready
** 0 : timeout
** -1 : wait error
*********************************************************************************************************/
int CommListen(int *fds,fd_set *readfds,fd_set *writefds,fd_set *errfds,int num,int time)
{
int maxfd,i,rnt;
struct timeval tv;
tv.tv_usec = time*1000; // usec
tv.tv_sec = 0; // seconds
if(num <= 0)
return -1;
for(i = 0;i < num - 1; i++)
maxfd =(*(fds + i + 1)> *(fds + i))? *(fds +i+1) : *(fds + i);
FD_ZERO(readfds);
FD_ZERO(writefds);
FD_ZERO(errfds);
for(i = 0;i<num;i++){
FD_SET(*(fds + i),readfds);
FD_SET(*(fds + i),writefds);
FD_SET(*(fds + i),errfds);
}
rnt = select(maxfd+1,readfds,writefds,errfds,&tv);
return rnt;
}
/*********************************************************************************************************
* Function name: Rs485dir
* Descriptions:
* input parameters:
* output parameters:
* Returned value:
*********************************************************************************************************/
void Rs485dir(int fdcom, uint16_t bOut)
{
int i;
int ret1, ret2, iostatus;
iostatus = 0;
DEBUGMSG(0, ("%s ...\n", __FUNCTION__));
if(bOut)
{
for(i = 0; i < 10; i++) {
ret1 = ioctl(fdcom, TIOCMGET, &iostatus);
if(0 != ret1) {
//return FAILURE;
/*wait 2ms */
usleep(2000);
continue;
} else
break;
}
for(i = 0 ; i < 10 ; i++) {
iostatus &= ~(TIOCM_RTS | TIOCM_DTR);
ret2 = ioctl(fdcom, TIOCMSET, &iostatus);
if(ret2 == 0)
break;
}
} else {
for(i = 0 ; i < 10; i++) {
ret1 = ioctl(fdcom, TIOCMGET, &iostatus);
if(0 != ret1) {
usleep(2000);
continue;
} else
break;
}
for(i = 0 ; i < 10 ; i++) {
iostatus = (TIOCM_RTS | TIOCM_DTR);
ret2 = ioctl(fdcom, TIOCMSET, &iostatus);
if(ret2 == 0)
break;
}
}
return;
}
/*********************************************************************************************************
** Function name: WaitFiledReadable
** Descriptions: serial port receive data
** input parameters: fd : 串口描述符
** nmsTimeOut : milliseconds
** output parameters:
** Returned value: 1 : data ready
** 0 : timeout
** -1 : wait error
*********************************************************************************************************/
#define WAIT_DATA_TIMEOUT 0
#define SELECT_MAX_WAIT_INTERVAL 2 // 2s
#define NMSRXTXTIMEOUT 200 //100
int WaitFiledReadable(int fd, int nmsTimeOut)
{
int iRnt;
fd_set fdset, fderr;
struct timeval tv;
if (nmsTimeOut < (SELECT_MAX_WAIT_INTERVAL*1000)) {
tv.tv_usec = nmsTimeOut%1000*1000; /* usec */
tv.tv_sec = nmsTimeOut/1000; /* seconds */
} else {
tv.tv_usec = 0; /* usec */
tv.tv_sec = SELECT_MAX_WAIT_INTERVAL; /* seconds */
}
FD_ZERO(&fdset); /* Initializes the set to the NULL set. */
FD_SET(fd, &fdset); /* Adds descriptor s to set. */
fderr = fdset;
/* determines the status of on
基于select的Linux串口485读写,文件描述符 fd_set
需积分: 49 95 浏览量
2018-02-05
17:03:42
上传
评论
收藏 5KB ZIP 举报
robin_ee
- 粉丝: 3
- 资源: 9
最新资源
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
- 音乐展示网页、基于Stenography的图像数字水印添加与提取,以及基于颜色矩和Tamura算法的图像相似度评估算法py源码
- 基于EmguCV(OpenCV .net封装),图像数字水印加解密算法的实现,其中包含最低有效位算法,离散傅里叶变换算法+文档书
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈