📄 serial.c
字号:
tcflush(fd,TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &option) == -1)
{
perror("SetupSerial");
return -1;
}
return 0;
}
/******************************************************************/
/*函数名称: static int open_dev(char * dev) */
/*功能描述: 设置串口数据位,停止位和效验位 */
/*输入参数: dev 数据类型 char * 串口名称 */
/*输出函数: 无 */
/*返回值: fd 正常文件描述符 -1 有错误 */
/*其他说明: 无 */
/*修改日期: 版本号 修改人 修改内容 */
/*----------------------------------------------------------------*/
/*2007/5/24 V1.0 */
/******************************************************************/
static int open_dev(char *dev)
{
int fd;
if (dev == NULL)
return -1;
fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port\n");
return -1;
}
else
return fd;
}
/*********************************************************************/
/*函数名称: static int read_from_com(int s_fd, char *buff, int len) */
/*功能描述: 串口读取数据 */
/*输入参数: s_fd 数据类型 int 文件描述符 */
/* buf 数据类型 char * 接收缓冲 */
/* int len 接收数据长度 */
/*输出函数: buf 数据类型 char * 接收完成的数据 */
/*返回值: 0 读数据正常 -1 读数据有错误 */
/*其他说明: 无 */
/*修改日期: 版本号 修改人 修改内容 */
/*-------------------------------------------------------------------*/
/*2007/5/24 V1.0 */
/*********************************************************************/
/*
int read_from_com(int fd, BYTE *buff, int len)
{
fd_set rset;
int ret, nread, total = 0;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 5000;
fprintf(stderr, "enter read_from_com\n");
if (len <= 0)
return -1;
if ((buff == NULL) || (fd < 0))
return -1;
do
{
FD_ZERO(&rset);
FD_SET(fd, &rset);
ret = select(fd + 1, &rset, NULL, NULL, &tv);
if (ret > 0 && FD_ISSET(fd, &rset))
{
read_again:
nread = read(fd, buff + total, len - total);
if (nread < 0)
{
if (errno == EINTR)
goto read_again;
return -1;
}
total += nread;
}
} while (total < len);
fprintf(stderr, "leave_from_com\n total is %d", total);
return total;
}
*/
int read_from_com(int s_fd, char * buf, int len)
{
int bytes, bytes_read ;
bytes_read = 0;
while (bytes_read < len)
{
bytes = read(s_fd, buf + bytes_read, len - bytes_read);
if (bytes > 0)
bytes_read += bytes;
else if (bytes == 0)
usleep(2); // how many time interval may be good ?
else
return bytes_read;
}
return bytes_read;
}
/*****************************************************************************/
/*函数名称: static int write_to_com(struct serial *com, char *buf, int len) */
/*功能描述: 串口写入数据 */
/*输入参数: s_fd 数据类型 int 文件描述符 */
/* buf 数据类型 char * 发送缓冲 */
/* int len 发送数据长度 */
/*输出函数: */
/*返回值: 0 写数据正常 -1 写数据有错误 */
/*其他说明: 无 */
/*修改日期: 版本号 修改人 修改内容 */
/*---------------------------------------------------------------------------*/
/*2007/5/24 V1.0 */
/*****************************************************************************/
int write_to_com(struct serial *com, char * buf, int *len, int mode)
{
int bytes , bytes_left, bytes_total;
bytes_total = 0;
bytes_left = *len;
while (bytes_total < *len)
{
bytes = write(com->fd, buf + bytes_total, bytes_left);
if (bytes == -1)
{
break;
}
bytes_total += bytes;
bytes_left -= bytes;
}
*len = bytes_total;
return bytes_total == -1 ? -1:0;
}
/*
static int write_to_com(const struct serial *com, char * buf, int len)
{
int bytes , bytes_write;
bytes_write = 0;
while (bytes_write < len)
{
bytes = write(com->s_fd, buf + bytes_write, len - bytes_write);
if (bytes > 0)
bytes_write += bytes;
else if (0 == bytes)
usleep(1);
else
return 0;
}
return bytes_write;
}
*/
/***************************************************************************/
/*函数名称: static int open_serial(struct serial *com) */
/*功能描述: 串口操作,打开串口,设置串口 */
/*输入参数: com 数据类型 const struct serial * 串口结构体指针 */
/*输出函数: */
/*返回值: 0 串口设置正常 -1 串口设置有错误 */
/*其他说明: 无 */
/*修改日期: 版本号 修改人 修改内容 */
/*-------------------------------------------------------------------------*/
/*2007/5/24 V1.0 */
/***************************************************************************/
int open_serial(struct serial *com)
{
int fd;
if (com == NULL)
{
return -1;
}
fd = open_dev(strcomdev[com->com_id]);
if (fd < 0)
{
return -1;
}
com->fd = fd;
if (set_speed(fd, com->baudrate) == -1)
{
close(fd);
fprintf(stderr, "1 wrong return -1");
return -1;
}
if (set_parity(fd, com) == -1)
{
close(fd);
return -1;
}
return 0;
}
/**********************************************************************************/
/*函数名称: void open_485mode(struct serial *com, BYTE on) */
/*功能描述: 设置串口通讯模式 485 or 232, */
/*输入参数: com 类型 const struct serial* 打开串口的文件句柄 */
/* on 类型 BYTE 模式485 时 1 软件跳线 0 软件不跳线 */
/*输出函数: 无 */
/*返回值: 0 正常 -1 有错误 */
/*其他说明: 无 */
/*修改日期: 版本号 修改人 修改内容 */
/*--------------------------------------------------------------------------------*/
/*2007/5/24 V1.0 */
/**********************************************************************************/
void open_485mode(struct serial *com, BYTE on)
{
if (com->mode == RS485)
{
iopl(3);
if (1 == on)
{
outb(0x08, com->io_addr + 4);
}
else
{
outb(0x0a, com->io_addr + 4);
}
iopl(0);
}
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -