You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.0 KiB
C++

1 year ago
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <string.h>
#include <unistd.h> /*Unix标准函数定义*/
#include <sys/types.h> /**/
#include <sys/stat.h> /**/
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX终端控制定义*/
#include <errno.h> /*错误号定义*/
/***@brief 设置串口通信速率
13*@param fd int
14*@param speed int
15*@return void
16*/
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300,
38400, 19200, 9600, 4800, 2400, 1200, 300, };
ino_t set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
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)
{
return -1;
}
// ESLOG_debug("[CES_OpenDoor::set_speed] tcsetattr fd1");
return 0;
}
tcflush(fd,TCIOFLUSH);
}
return -2;
}
/*
45*@brief
46*@param fd int *
47*@param databits int 7 8*
48*@param stopbits int 1 2*
49*@param parity int N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
//ESLOG_debug("[CES_OpenDoor::set_Parity] tcgetattr() 1");
return -1;
}
options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
//ESLOG_debug("[CES_OpenDoor::set_Parity] Unsupported data size\n");
return -2;
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
// ESLOG_debug("[CES_OpenDoor::set_Parity] Unsupported parity\n");
return -3;
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
// ESLOG_debug("[CES_OpenDoor::set_Parity] Unsupported stop bits\n");
return -4;
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
//ESLOG_debug("[CES_OpenDoor::set_Parity] tcsetattr
return -5;
}
return 0;
}
/**
*@breif
*/
int OpenDev(char *Dev)
{
int n_fd_com = open( Dev, O_RDWR| O_NOCTTY | O_NDELAY ); //
if (-1 == n_fd_com)
{ /*设置数据位数*/
// ESLOG_debug("[CES_OpenDoor::OpenDev] Can't Open Serial Port");
return n_fd_com;
}
else
{
set_speed(n_fd_com,19200);
if (set_Parity(n_fd_com,8,1,'N') <0)
{
//ESLOG_debug("[CES_OpenDoor::OpenDev] Set Parity Error\n");
return -1;
}
return n_fd_com;
}
}
int main()
{
int fd;
char buf[32];
fd = OpenDev("/dev/pts/1");
if(fd <0)
{
printf("open dev fail\r");
return -1;
}
int i =0;
strcpy(buf,"test");
while(1)
{
sprintf(buf,"the num is %d",i++);
int rv = write(fd,buf,strlen(buf));
if(rv <0)
{
break;
}
usleep(100000);
// sleep(100);
}
close(fd);
return 0;
}