Train_RFID_Linux/code/BaseComPort/BaseComPort.cpp

251 lines
5.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by matrixai on 3/26/24.
//
#include "BaseComPort.h"
//构造函数
//输入参数:无
//输出参数:无
//返回值:无
BaseComPort::BaseComPort()
{
memset(chbuffer, 0x00, sizeof(AEI_COMM_BUFFER_SIZE));
m_map_parity = {
{"n",0},
{"N",1},
{"o",2},
{"O",3},
{"e",4},
{"E",5},
{"s",6},
{"S",7}
};
}
//析构函数
//输入参数:无
//输出参数:无
//返回值:无
BaseComPort::~BaseComPort()
{
ttyClose();
}
//串口打开
int32_t BaseComPort::ttyOpen(std::string strName)
{
LogInfo<<"m_serial_data.name.c_str() is: "<<strName;
// 打开的文件名称O_RDWR表示文件可读可写非阻塞式文件位终端设备
fd = open(strName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
LogInfo<<"fd value is: "<<fd;
if (fd < 0)
{
LogError<<"open device failure.";
return -1;
}
/* 测试该设备是否为tty设备 */
if (isatty(fd) == 0)
{
LogError<<"not tty device.";
return -1;
}
LogInfo<<"tty device is ok";
return fd;
}
//串口关闭
int32_t BaseComPort::ttyClose()
{
close(fd);
fd = -1;
return fd;
}
bool BaseComPort::bRuning()
{
return (fd >= 0);
}
//属性参数设置
int32_t BaseComPort::ttySetBaud(int nbaud, int ndatabits, std::string strparitybits, int nstopbits)
{
int32_t ret = 0;
bzero(&ntm, sizeof(ntm)); //将自己的前n个数清0
//获取终端的控制参数成功return 0;
if(tcgetattr(fd,&ntm) != 0)
{
LogError<<"setup serial failure.";
return -1;
}
// ntm.c_cflag = CS8 | CLOCAL | CREAD; //收发采用8位数据位忽略所有调制解调器的状态行启动字符接收器
ntm.c_cflag &= ~CSIZE; //清楚数据位掩码
switch(nbaud)
{
case 300:
//ntm.c_cflag |= B300;
cfsetispeed(&ntm, B300);
cfsetospeed(&ntm, B300);
break;
case 1200:
cfsetispeed(&ntm, B1200);
cfsetospeed(&ntm, B1200);
break;
case 2400:
cfsetispeed(&ntm, B2400);
cfsetospeed(&ntm, B2400);
break;
case 4800:
cfsetispeed(&ntm, B4800);
cfsetospeed(&ntm, B4800);
break;
case 9600:
cfsetispeed(&ntm, B9600);
cfsetospeed(&ntm, B9600);
break;
case 19200:
cfsetispeed(&ntm, B19200);
cfsetospeed(&ntm, B19200);
break;
case 38400:
cfsetispeed(&ntm, B38400);
cfsetospeed(&ntm, B38400);
break;
case 115200:
cfsetispeed(&ntm, B115200);
cfsetospeed(&ntm, B115200);
break;
default:
LogError<<"the value of m_serial_data.baud is error, please check.";
ret = -1;
break;
}
//设置数据位数
switch (ndatabits)
{
case 5:
ntm.c_cflag |= CS5;
break;
case 6:
ntm.c_cflag |= CS6;
break;
case 7:
ntm.c_cflag |= CS7;
break;
case 8:
ntm.c_cflag |= CS8;
break;
default:
LogError<<"the data_bits in config.yaml is error, please check.";
return -1;
break;
}
// 设置奇偶校验位数
int32_t parity = -1;
auto findResult = m_map_parity.find(strparitybits);
if(findResult != m_map_parity.end())
{
parity = m_map_parity[strparitybits];
}
switch (parity)
{
case 0:
case 1:
ntm.c_cflag &= ~PARENB;
ntm.c_iflag &= ~INPCK;
break;
case 2:
case 3:
ntm.c_cflag |= (PARODD|PARENB);
ntm.c_iflag |= INPCK;
break;
case 4:
case 5:
ntm.c_cflag |= PARENB;
ntm.c_cflag &= ~PARODD;
ntm.c_iflag |= INPCK;
break;
case 6:
case 7:
ntm.c_cflag &= ~PARENB;
ntm.c_cflag &= ~CSTOPB;
break;
default:
LogError<<"the parity_bits in config.yaml is error, please check.";
return -1;
break;
}
// 设置停止位
switch (nstopbits)
{
case 1:
ntm.c_cflag &= ~CSTOPB;
break;
case 2:
ntm.c_cflag |= CSTOPB;
break;
default:
LogError<<"the stop_bits in config.yaml is error, please check.";
return -1;
break;
}
//ntm.c_lflag = 0;
ntm.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
ntm.c_cc[VTIME] = 10; //设置超时时间10s
ntm.c_cc[VMIN] = 1; //至少读取1个字节的数据或者设为0
tcflush(fd, TCIFLUSH); //清空终端未完成的输入/输出请求及数据
//设置终端控制参数TCSANOW立刻生效
if (tcsetattr(fd,TCSANOW,&ntm) != 0)
{
LogError<<"setup serial failure";
return -1;
}
return ret;
}
//串口数据读取
int32_t BaseComPort::ttyRead()
{
memset(chbuffer, 0x00, sizeof(chbuffer));
int nRev = read(fd, chbuffer, (sizeof(chbuffer) - 1));
//LogInfo<<fd<<":"<<chbuffer<<":"<<nRev;
return nRev;
}
//串口数据写入
int32_t BaseComPort::ttyWrite(char *buf, int32_t count)
{
int nRev = write(fd, buf, count);
return nRev;
}
int32_t BaseComPort::vGetBuffer(char *chOutBuff, int32_t nBufferSize)
{
chbuffer[sizeof(chbuffer) - 1] = 0x00;
int nBufferLength = strlen(chbuffer);
int nOutLength = nBufferLength < nBufferSize ? nBufferLength : nBufferSize;
memset(chOutBuff, 0x00, nBufferSize);
memcpy(chOutBuff, chbuffer, nOutLength);
return nOutLength;
}
int32_t BaseComPort::vGetMaxBufferSize()
{
return AEI_COMM_BUFFER_SIZE;
}