123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936 |
-
- #ifndef __CSERIALPORTUNIXBASE_H__
- #define __CSERIALPORTUNIXBASE_H__
- #include "SerialPortBase.h"
- #include <errno.h> /* Error number definitions */
- #include <fcntl.h> /* File control definitions */
- #include <stdio.h> /* Standard input/output definitions */
- #include <string.h> /* String function definitions */
- #include <termios.h> /* POSIX terminal control definitions */
- #include <unistd.h> /* UNIX standard function definitions */
- #include <sys/ioctl.h> //ioctl
- #include "sigslot.h"
- class CSerialPortUnixBase : public CSerialPortBase
- {
- public:
-
- CSerialPortUnixBase(){
- construct();
- }
-
- CSerialPortUnixBase(const std::string &portName){
- construct();
- }
-
- virtual ~CSerialPortUnixBase(){
- pthread_mutex_destroy(&m_communicationMutex);
- }
-
- virtual void construct(){
- fd = -1;
- m_baudRate = itas109::BaudRate9600;
- m_parity = itas109::ParityNone;
- m_dataBits = itas109::DataBits8;
- m_stopbits = itas109::StopOne;
- m_flowControl = itas109::FlowNone;
- m_readBufferSize = 512;
- m_isThreadRunning = false;
- m_operateMode = itas109::AsynchronousOperate;
- pthread_mutex_init(&m_communicationMutex, NULL);
- }
-
- virtual void init(std::string portName,
- int baudRate = itas109::BaudRate9600,
- itas109::Parity parity = itas109::ParityNone,
- itas109::DataBits dataBits = itas109::DataBits8,
- itas109::StopBits stopbits = itas109::StopOne,
- itas109::FlowControl flowControl = itas109::FlowNone,
- int64 readBufferSize = 512){
- m_portName = portName;
- m_baudRate = baudRate;
- m_parity = parity;
- m_dataBits = dataBits;
- m_stopbits = stopbits;
- m_flowControl = flowControl;
- m_readBufferSize = readBufferSize;
- }
-
- virtual bool openPort(){
- bool bRet = false;
- lock();
-
- fd = open(m_portName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd != -1)
- {
-
- if (fcntl(fd, F_SETFL, 0) >= 0)
- {
-
- if (uart_set(fd, m_baudRate, m_parity, m_dataBits, m_stopbits, m_flowControl) == -1)
- {
- fprintf(stderr, "uart set failed!\n");
-
- bRet = false;
- lastError = itas109:: InvalidParameterError;
- }
- else
- {
- m_isThreadRunning = true;
- bRet = startThreadMonitor();
- if (!bRet)
- {
- m_isThreadRunning = false;
- lastError = itas109:: SystemError;
- }
- }
- }
- else
- {
- bRet = false;
- lastError = itas109:: SystemError;
- }
- }
- else
- {
-
- char str[256];
- snprintf(str, sizeof(str), "open port error: Unable to open %s", m_portName.c_str());
- perror(str);
- bRet = false;
- lastError = itas109:: OpenError;
- }
- if (!bRet)
- {
- closePort();
- }
- unlock();
- return bRet;
- }
-
- virtual void closePort(){
- if (isOpened())
- {
- stopThreadMonitor();
- close(fd);
- fd = -1;
- }
- }
-
- virtual bool isOpened(){
- return fd != -1;
- }
-
- virtual int readData(char *data, int maxSize){
- int iRet = -1;
- lock();
- if (isOpened())
- {
- iRet = read(fd, data, maxSize);
- }
- else
- {
- lastError = itas109:: NotOpenError;
- iRet = -1;
- }
- unlock();
- return iRet;
- }
-
- virtual int readAllData(char *data){
- int readbytes = 0;
-
- ioctl(fd, FIONREAD, &readbytes);
- return readData(data, readbytes);
- }
-
- virtual int readLineData(char *data, int maxSize){
- int iRet = -1;
- lock();
- if (isOpened())
- {
- }
- else
- {
- lastError = itas109:: NotOpenError;
- iRet = -1;
- }
- unlock();
- return iRet;
- }
-
- virtual int writeData(const char *data, int maxSize){
- int iRet = -1;
-
- if (isOpened())
- {
-
- iRet = write(fd, data, maxSize);
- }
- else
- {
- lastError = itas109:: NotOpenError;
- iRet = -1;
- }
-
- return iRet;
- }
-
- virtual void setDebugModel(bool isDebug){
- }
-
- virtual void setReadTimeInterval(int msecs){
- }
-
- virtual void setMinByteReadNotify(unsigned int minByteReadNotify = 2){
- m_minByteReadNotify = minByteReadNotify;
- }
-
- virtual int getLastError() const{
- return lastError;
- }
-
- virtual void clearError(){
- lastError = itas109::NoError;
- }
-
- virtual void setPortName(std::string portName){
- m_portName = portName;
- }
-
- virtual std::string getPortName() const{
- return m_portName;
- }
-
- virtual void setBaudRate(int baudRate){
- m_baudRate = baudRate;
- }
-
- virtual int getBaudRate() const{
- return m_baudRate;
- }
-
- virtual void setParity(itas109::Parity parity){
- m_parity = parity;
- }
-
- virtual itas109::Parity getParity() const{
- return m_parity;
- }
-
- virtual void setDataBits(itas109::DataBits dataBits){
- m_dataBits = dataBits;
- }
-
- virtual itas109::DataBits getDataBits() const{
- return m_dataBits;
- }
-
- virtual void setStopBits(itas109::StopBits stopbits){
- m_stopbits = stopbits;
- }
-
- virtual itas109::StopBits getStopBits() const{
- return m_stopbits;
- }
-
- virtual void setFlowControl(itas109::FlowControl flowControl){
- m_flowControl = flowControl;
- }
-
- virtual itas109::FlowControl getFlowControl() const{
- return m_flowControl;
- }
-
- virtual void setReadBufferSize(int64 size){
- m_readBufferSize = size;
- }
-
- virtual int64 getReadBufferSize() const{
- return m_readBufferSize;
- }
-
- virtual void setDtr(bool set = true){}
-
- virtual void setRts(bool set = true){}
-
- std::string getVersion(){
- std::string m_version = "CSerialPortUnixBase V1.0.1.190728";
- return m_version;
- }
- public:
-
- bool isThreadRunning(){
- return m_isThreadRunning;
- }
- private:
-
- void lock(){
- pthread_mutex_lock(&m_communicationMutex);
- }
-
- void unlock(){
- pthread_mutex_unlock(&m_communicationMutex);
- }
-
- int rate2Constant(int baudrate){
-
- #define B(x) \
- case x: \
- return B##x
- switch (baudrate)
- {
- #ifdef B50
- B(50);
- #endif
- #ifdef B75
- B(75);
- #endif
- #ifdef B110
- B(110);
- #endif
- #ifdef B134
- B(134);
- #endif
- #ifdef B150
- B(150);
- #endif
- #ifdef B200
- B(200);
- #endif
- #ifdef B300
- B(300);
- #endif
- #ifdef B600
- B(600);
- #endif
- #ifdef B1200
- B(1200);
- #endif
- #ifdef B1800
- B(1800);
- #endif
- #ifdef B2400
- B(2400);
- #endif
- #ifdef B4800
- B(4800);
- #endif
- #ifdef B9600
- B(9600);
- #endif
- #ifdef B19200
- B(19200);
- #endif
- #ifdef B38400
- B(38400);
- #endif
- #ifdef B57600
- B(57600);
- #endif
- #ifdef B115200
- B(115200);
- #endif
- #ifdef B230400
- B(230400);
- #endif
- #ifdef B460800
- B(460800);
- #endif
- #ifdef B500000
- B(500000);
- #endif
- #ifdef B576000
- B(576000);
- #endif
- #ifdef B921600
- B(921600);
- #endif
- #ifdef B1000000
- B(1000000);
- #endif
- #ifdef B1152000
- B(1152000);
- #endif
- #ifdef B1500000
- B(1500000);
- #endif
- #ifdef B2000000
- B(2000000);
- #endif
- #ifdef B2500000
- B(2500000);
- #endif
- #ifdef B3000000
- B(3000000);
- #endif
- #ifdef B3500000
- B(3500000);
- #endif
- #ifdef B4000000
- B(4000000);
- #endif
- default:
- return 0;
- }
- #undef B
- }
-
- int uart_set(int fd,
- int baudRate = itas109::BaudRate9600,
- itas109::Parity parity = itas109::ParityNone,
- itas109::DataBits dataBits = itas109::DataBits8,
- itas109::StopBits stopbits = itas109::StopOne,
- itas109::FlowControl flowControl = itas109::FlowNone){
- struct termios options;
-
- if (tcgetattr(fd, &options) < 0)
- {
- perror("tcgetattr error");
- return -1;
- }
-
- int baudRateConstant = 0;
- baudRateConstant = rate2Constant(baudRate);
- if (0 != baudRateConstant)
- {
- cfsetispeed(&options, baudRateConstant);
- cfsetospeed(&options, baudRateConstant);
- }
- else
- {
-
- fprintf(stderr, "Unkown baudrate!\n");
- return -1;
- }
-
- switch (parity)
- {
-
- case itas109::ParityNone:
- case 'N':
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~INPCK;
- break;
-
- case itas109::ParityOdd:
- options.c_cflag |= PARENB;
- options.c_cflag |= PARODD;
- options.c_cflag |= INPCK;
- options.c_cflag |= ISTRIP;
- break;
-
- case itas109::ParityEven:
- options.c_cflag |= PARENB;
- options.c_cflag &= ~PARODD;
- options.c_cflag |= INPCK;
- options.c_cflag |= ISTRIP;
- break;
-
- case itas109::ParitySpace:
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- break;
- default:
- fprintf(stderr, "Unkown parity!\n");
- return -1;
- }
-
- switch (dataBits)
- {
- case itas109::DataBits5:
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS5;
- break;
- case itas109::DataBits6:
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS6;
- break;
- case itas109::DataBits7:
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS7;
- break;
- case itas109::DataBits8:
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS8;
- break;
- default:
- fprintf(stderr, "Unkown bits!\n");
- return -1;
- }
-
- switch (stopbits)
- {
- case itas109::StopOne:
- options.c_cflag &= ~CSTOPB;
- break;
- case itas109::StopOneAndHalf:
- fprintf(stderr, "POSIX does not support 1.5 stop bits!\n");
- return -1;
- case itas109::StopTwo:
- options.c_cflag |= CSTOPB;
- break;
- default:
- fprintf(stderr, "Unkown stop!\n");
- return -1;
- }
-
- options.c_cflag |= CLOCAL;
- options.c_cflag |= CREAD;
-
- switch (flowControl)
- {
- case itas109::FlowNone:
- options.c_cflag &= ~CRTSCTS;
- break;
- case itas109::FlowHardware:
- options.c_cflag |= CRTSCTS;
- break;
- case itas109::FlowSoftware:
- options.c_cflag |= IXON | IXOFF | IXANY;
- break;
- default:
- fprintf(stderr, "Unkown c_flow!\n");
- return -1;
- }
-
- options.c_oflag &= ~OPOST;
-
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
-
- options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
-
-
- options.c_cc[VTIME] = 0;
- options.c_cc[VMIN] = 1;
-
- tcflush(fd, TCIFLUSH);
-
- if (tcsetattr(fd, TCSANOW, &options) < 0)
- {
- perror("tcsetattr failed");
- return -1;
- }
- return 0;
- }
-
- static void *commThreadMonitor(void *pParam){
-
-
- CSerialPortUnixBase *p_base = (CSerialPortUnixBase *)pParam;
- if (p_base)
- {
- for (; p_base->isThreadRunning();)
- {
- int readbytes = 0;
-
- ioctl(p_base->fd, FIONREAD, &readbytes);
- if (readbytes >= p_base->getMinByteReadNotify())
- {
- p_base->readReady._emit();
- }
- else
- {
- usleep(1);
- }
- }
- }
- else
- {
-
- }
- pthread_exit(NULL);
- }
-
- bool startThreadMonitor(){
- bool bRet = true;
-
- int ret = pthread_create(&m_monitorThread, NULL, commThreadMonitor, (void *)this);
- if (ret < 0)
- {
- bRet = false;
- printf("Create read thread error.");
- }
- return bRet;
- }
-
- bool stopThreadMonitor(){
- m_isThreadRunning = false;
- pthread_join(m_monitorThread, NULL);
- return true;
- }
- public:
- sigslot::signal0<> readReady;
- private:
- std::string m_portName;
- int m_baudRate;
- itas109::Parity m_parity;
- itas109::DataBits m_dataBits;
- itas109::StopBits m_stopbits;
- itas109::FlowControl m_flowControl;
- int64 m_readBufferSize;
- int fd;
- private:
- pthread_t m_monitorThread;
- bool m_isThreadRunning;
- pthread_mutex_t m_communicationMutex;
- };
- #endif
|