📄 serial.c
字号:
/******************************************************************/
/* Copyright (c) 2005, */
/* All rights reserved. */
/* 文件名称:serail.c */
/* 摘 要:串口操作函数集,包括打开、设置串口参数,读写串口等 */
/* 当前版本:1.0 */
/* 作 者: */
/* 完成日期:2007年6月 */
/* 修改记录: */
/* 修改日期 版本号 修改人 修改内容 */
/******************************************************************/
#include <signal.h>
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h> /*类型定义*/
#include <sys/time.h> /*时间定义*/
#include <sys/io.h> /*IO 头文件*/
#include <linux/serial.h> /*串口头文件*/
#include <sys/stat.h> /*文件系统定义*/
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX 终端控制定义*/
#include <errno.h> /*错误号定义*/
#include <time.h> /*时间的头文件*/
#include "serial.h"
#include "dlt645.h"
short int speed_arr[] = {B38400, B19200, B9600, B4800, B2400, B1200, B600, B300};
short int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 600, 300};
struct serial com_arry[8];
struct meter meter_arry[1000];
BYTE log[100] = {0};
char *strcomdev[] =
{
"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3",
"/dev/ttyS4", "/dev/ttyS5", "/dev/ttyS6", "/dev/ttyS7",
"/dev/ttyS8", "/dev/ttyS9", "/dev/ttyS10", "/dev/ttyS11",
"/dev/ttyS12", "/dev/ttyS13", "/dev/ttyS14", "/dev/ttyS15",
"/dev/ttyS16", "/dev/ttyS17", "/dev/ttyS18", "/dev/ttyS19"
};
/******************************************************************/
/*函数名称: static int set_speed(int fd, int speed) */
/*功能描述: 设置串口的波特率 */
/*输入参数: fd 类型 int 打开串口的文件句柄 */
/* baudrate 类型 int 串口速度 */
/*输出函数: 无 */
/*返回值: 0 串口设置正常 -1 串口设置异常 */
/*其他说明: 无 */
/*修改日期: 版本号 修改人 修改内容 */
/*----------------------------------------------------------------*/
/*2007/5/24 V1.0 */
/******************************************************************/
int set_speed(int fd, short int baudrate)
{
unsigned int i;
int ret;
struct termios opt;
tcgetattr(fd, &opt);
ret = sizeof (speed_arr) / sizeof (short int);
for (i = 0; i < ret; i++)
{
if (baudrate == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
if (cfsetispeed(&opt, speed_arr[i]) == -1)
{
perror("cfsetispeed");
return -1;
}
if (cfsetospeed(&opt, speed_arr[i]) == -1)
{
perror("cfsetospeed");
return -1;
}
if (tcsetattr(fd, TCSANOW, &opt) == -1)
{
perror("tcsetattr fd1");
return -1;
}
return 0;
}
tcflush(fd,TCIOFLUSH);
}
return 0;
}
/**********************************************************************/
/*函数名称: static int set_parity(int fd, const struct serial *com) */
/*功能描述: 设置串口数据位,停止位和效验位 */
/*输入参数: fd 类型 int 打开串口的文件句柄 */
/* databits 类型 int 数据位个数 取值为7 或者8 */
/* stopbits 类型 int 停止位个数 取值为1 或者2 */
/* parity 类型 int 校验 */
/*输出函数: 无 */
/*返回值: 0 正常 -1 有错误 */
/*其他说明: 无 */
/*修改日期: 版本号 修改人 修改内容 */
/*--------------------------------------------------------------------*/
/*2007/5/24 V1.0 */
/**********************************************************************/
static int set_parity(int fd, const struct serial *com)
{
struct termios option;
if (com == NULL)
{
printf("wrong return");
return -1;
}
if (tcgetattr(fd,&option) == -1)
{
perror("SetupSerial 1");
return -1;
}
option.c_cflag &= ~CSIZE;
/*设置数据位数*/
switch (com->databit)
{
case 5:
{
option.c_cflag |= CS5;
break;
}
case 6:
{
option.c_cflag |= CS6;
break;
}
case 7:
{
option.c_cflag |= CS7;
break;
}
case 8:
{
option.c_cflag |= CS8;
break;
}
default:
{
printf("Unsupported data size\n");
return -1;
}
}
/*设置奇偶校验位*/
switch (com->parity)
{
case 'n':
case 'N':
{
option.c_cflag &= ~PARENB; /* Clear parity enable */
option.c_iflag &= ~INPCK; /* Enable parity checking */
//options.c_iflag = 0 ;
break;
}
case 'o':
case 'O':
{
option.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
option.c_iflag |= INPCK; /* Disnable parity checking */
break;
}
case 'e':
case 'E':
{
option.c_cflag |= PARENB; /* Enable parity */
option.c_cflag &= ~PARODD; /* 转换为偶效验*/
option.c_iflag |= INPCK; /* Disnable parity checking */
break;
}
case 's':
case 'S': /*as no parity*/
{
option.c_cflag &= ~PARENB;
option.c_cflag &= ~CSTOPB;
break;
}
default:
{
printf("Unsupported parity\n");
return -1;
}
}
/* 设置停止位*/
switch (com->stopbit)
{
case 1:
{
option.c_cflag &= ~CSTOPB;
break;
}
case 2:
{
option.c_cflag |= CSTOPB;
break;
}
default:
{
printf("Unsupported stop bits\n");
return -1;
}
}
/* Set input parity option */
if (com->parity != 'n')
{
option.c_iflag |= INPCK;
}
option.c_iflag &= ~INPCK;
option.c_iflag |= IGNPAR;
option.c_iflag &= ~ICRNL;
option.c_iflag |= IGNBRK;
option.c_iflag &= ~(IXON | IXOFF | IXANY);
option.c_cc[VTIME] = 0;
option.c_cc[VMIN] = 1;
option.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
option.c_oflag &= ~OPOST;
option.c_cflag |= CLOCAL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -