📄 com.cxx
字号:
#include"COM.hxx"
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,};
CCOM::CCOM()
{
}
CCOM::~CCOM()
{
CloseCOM();
}
void CCOM::OpenCOM()
{
/*以读写方式打开串口*/
fd = open( "/dev/ttyAMA1", O_RDWR);
printf("open COM,fd=%d\n",fd);
if (-1 == fd)
{
/* 不能打开串口一*/
printf(" 打开串口错误!\n");
}
}
void CCOM::set_speed(int speed)
{
int i;
int status;
struct termios Opt;
int ra = tcgetattr(fd, &Opt);
if(-1 == ra)
{
printf("tcgetattr()return %d\n",ra);
}
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
int rb = tcflush(fd, TCIOFLUSH);
if(-1 == rb)
{
printf("tcflush()return %d\n",rb);
}
int rc = cfsetispeed(&Opt, speed_arr[i]);
if(-1 == rc)
{
printf("cfsetispeed() return %d\n",rc);
}
int rd = cfsetospeed(&Opt, speed_arr[i]);
if(-1 == rd)
{
printf("cfsetospeed() return %d\n",rd);
}
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
{
printf("tcsetattr fd1\n");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
printf("set speed %d\n",speed);
}
/**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄
*@param databits 类型 int 数据位 取值 为 7 或者8
*@param stopbits 类型 int 停止位 取值为 1 或者2
*@param parity 类型 int 效验类型 取值为N,E,O,S
*/
bool CCOM::set_Parity(int databits,int stopbits,int parity)
{
struct termios options;
if(tcgetattr(fd,&options) != 0)
{
printf("SetupSerial 1\n");
return false;
}
options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
printf("set databits %d\n",databits);
break;
case 8:
options.c_cflag |= CS8;
printf("set databits %d\n",databits);
break;
default:
printf("Unsupported data sizen");
return (false);
}
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;
printf("set parity %c\n",parity);
break;
default:
printf("Unsupported parity");
return (false);
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
printf("set stopbits %d\n",stopbits);
break;
case 2:
options.c_cflag |= CSTOPB;
printf("set stopbits %d\n",stopbits);
break;
default:
printf("Unsupported stopbits!\n");
return (false);
}
/* Set input parity option */
if (parity != 'n')
{
options.c_iflag |= INPCK;
}
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
printf("SetupSerial 3\n");
return false;
}
return (true);
}
int CCOM::SendData(void*buffer,int Len)
{
int nByte;
if(NULL == buffer)
{
return 0;
}
nByte = write(fd,buffer,Len);
printf("%d bytes data sent\n",nByte);
return nByte;
}
int CCOM::ReadData(void *buffer,int Len)
{
if(NULL == buffer)
{
return 0;
}
int readByte = read(fd,buffer,Len);
return readByte;
}
void CCOM::CloseCOM()
{
if(-1 == close(fd))
{
printf(" 关闭串口错误!\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -