zoukankan      html  css  js  c++  java
  • Linux应用编程之串口操作20170901

    主要介绍在Linux应用程序下对串口的操作:

    1.串口初始化

    int InitCom()

    {

             int Ret;

                 Ret = SerailComm.OpenCom( ComPortDevPath, 0 );

                 if( Ret < 0 )

                 {

                     return Ret;

                 }

             if(SerailComm.SetComSpeed( ComBaudRate ) < 0 )

             {

                       SerailComm.CloseCom();

                       return Ret;

             }

             if(SerailComm.SetComParity(8, 1, 'N', 0, 0) < 0)

             {

                       SerailComm.CloseCom();

                       return Ret;

             }

             Ret = SerailComm.SetComRawMode();

             if( Ret < 0 )

             {

                       SerailComm.CloseCom();

                       return  Ret;

             }

             if( SerailComm.SetComSelectTimeOut(1, 0, READ_TIMEOUT_F) < 0 )

             {

                       CloseCom();

                     return  Ret;

             }

             if( SerailComm.SetComSelectTimeOut( 0, 500000, WRITE_TIMEOUT_F) < 0 )

             {

                       SerailComm.CloseCom();

                     return  Ret;

             }

             SerailComm.SetSerialCommBlock( 0 );

         return 0;

    }

    2.读数据

    int ReadCom()

    {

             char cRecvBuf[256];

             int iBufSize = 256;

             memset(cRecvBuf, 0, sizeof(cRecvBuf));

             return SerailComm.ReadBinCom(cRecvBuf, iBufSize))

    }

    3.写数据

    int WriteCom(u8 *WriteBuf, int Size)

    {

        return SerailComm.WriteBinCom((char*)WriteBuf, Size);

    }

    4.关闭串口

    void CloseCom()

    {

        SerailComm.CloseCom();

    }

    5.附:上述使用到的串口类

      1 #include "ComTransmit.h"
      2 #include <assert.h>
      3 #include <unistd.h>
      4 #include <termios.h>
      5 #include <ctype.h>
      6 #include <errno.h>
      7 
      8 #include "tracelevel.h"
      9 #include "MdvrModulCom.h"
     10 
     11 extern HANDLE_DVR  g_hDvr;
     12 
     13 pthread_mutex_t ComCommutex = PTHREAD_MUTEX_INITIALIZER;
     14 
     15 //pthread_rwlock_t  rwlock = PTHREAD_RWLOCK_INITIALIZER;
     16 
     17 speedPara speed_atr[] =
     18 {
     19     {B300,300},
     20     {B600,600},
     21     {B1200,1200},
     22     {B2400,2400},
     23     {B4800,4800},
     24     {B9600,9600},
     25     {B19200,19200},
     26     {B38400,38400},
     27     {B57600,57600},
     28     {B115200,115200}    ,
     29     {B230400,230400}
     30 };
     31 
     32 CComTransmit::CComTransmit():m_fp(NULL),m_fd(-1)
     33 {
     34     memset(&Rtimeout,0,sizeof(Rtimeout));
     35     memset(&Wtimeout,0,sizeof(Wtimeout));
     36     m_binit = 0;
     37 }
     38 
     39 CComTransmit::~CComTransmit()
     40 {
     41 
     42 }
     43 
     44 int CComTransmit::OpenCom(const char * deviceName, int isMutex)
     45 {
     46     assert(deviceName != NULL);
     47 
     48     if(m_binit == 1)
     49         return 0;
     50 
     51     m_fd = open(deviceName,O_RDWR);
     52     if(m_fd < 0)
     53     {
     54         printf("Open Com [%s] failure! %s
    ",deviceName, strerror(errno));
     55         return -1;
     56     }
     57 
     58     printf("Open Com [%s] Sucess
    ",deviceName);
     59 
     60     m_fp = fdopen(m_fd, "r+b");
     61     if(m_fp == NULL)
     62     {
     63         return -1;
     64     }
     65 
     66     m_binit = 1;
     67     m_mutex_flag = isMutex;
     68 
     69     return 0;
     70 }
     71 
     72 void CComTransmit::CloseCom()
     73 {
     74     int ret = close(m_fd);
     75     if(ret < 0)
     76     {
     77         printf("close Com failure!
    ");
     78     }
     79     m_fp = NULL;
     80     m_binit = 0;
     81 }
     82 
     83 int CComTransmit::SetComSpeed(unsigned int speed)
     84 {
     85     int comspeed = 0;
     86     int status = 0;
     87     struct termios   Opt;
     88     int ret = -1;
     89 
     90     for(unsigned int i = 0;i < sizeof(speed_atr)/sizeof(speedPara);i++)
     91     {
     92         if(speed == speed_atr[i].userSpeed)
     93         {
     94             comspeed = speed_atr[i].sysSpeed;
     95             break;
     96         }
     97     }
     98 
     99     if(!comspeed)
    100     {
    101         assert(comspeed != 0);
    102     }
    103 
    104     if(m_fd < 0)
    105     {
    106         return -1;
    107     }
    108     tcgetattr(m_fd, &Opt);
    109     tcflush(m_fd, TCIOFLUSH);
    110         ret = cfsetispeed(&Opt, comspeed);
    111     if(ret < 0)
    112     {
    113         perror("set input speed error!
    ");
    114         return -1;
    115     }
    116 
    117         ret = cfsetospeed(&Opt, comspeed);
    118     if(ret < 0)
    119     {
    120         perror("set output speed error!
    ");
    121         return -1;
    122     }
    123 
    124         status = tcsetattr(m_fd, TCSANOW, &Opt);
    125         if(status != 0)
    126         {
    127                 perror("tcsetattr fd1");
    128         return -1;
    129         }
    130 
    131     tcflush(m_fd,TCIOFLUSH);
    132     return 0;
    133  }
    134 
    135 int CComTransmit::SetComParity(int databits, int stopbits, int parity,int flowctrl, int Rs485)
    136 {
    137     struct termios options;
    138 
    139     if(m_fd < 0)
    140     {
    141         return -1;
    142     }
    143 
    144     if  ( tcgetattr( m_fd,&options)  !=  0)
    145     {
    146         printf("SetComParity 1");
    147         return-1;
    148     }
    149 
    150     options.c_cflag &= ~CSIZE;
    151       switch (databits) /*设置数据位数*/
    152       {
    153           case 5:
    154             options.c_cflag |= CS5;
    155             break;
    156         case 6:
    157             options.c_cflag |= CS6;
    158             break;
    159           case 7:
    160               options.c_cflag |= CS7;
    161               break;
    162           case 8:
    163             options.c_cflag |= CS8;
    164             break;
    165         default:
    166             fprintf(stderr,"Unsupported data size
    ");
    167             return -1;
    168     }
    169       switch (parity)
    170       {
    171           case 'n':
    172         case 'N':
    173         case 0:
    174             options.c_cflag &= ~PARENB;   /* Clear parity enable */
    175             options.c_iflag &= ~INPCK;     /* Enable parity checking */
    176             break;
    177         case 'o':
    178         case 'O':
    179         case 1:
    180             options.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/
    181             options.c_iflag |= INPCK;             /* Disnable parity checking */
    182             break;
    183         case 'e':
    184         case 'E':
    185         case 2:
    186             options.c_cflag |= PARENB;     /* Enable parity */
    187             options.c_cflag &= ~PARODD;   /* 转换为偶效验*/
    188             options.c_iflag |= INPCK;       /* Disnable parity checking */
    189             break;
    190         case 'S':
    191         case 's':  /*as no parity*/
    192             options.c_cflag &= ~PARENB;
    193             options.c_cflag &= ~CSTOPB;
    194             break;
    195         default:
    196             fprintf(stderr,"Unsupported parity
    ");
    197             return -1;
    198         }
    199 
    200       /* 设置停止位*/
    201       switch (stopbits)
    202       {
    203           case 1:
    204               options.c_cflag &= ~CSTOPB;
    205             break;
    206         case 2:
    207             options.c_cflag |= CSTOPB;
    208         break;
    209         default:
    210             fprintf(stderr,"Unsupported stop bits
    ");
    211             return (-1);
    212     }
    213 
    214     switch(flowctrl)    /* 设置流控制*/
    215     {
    216         case 0:
    217             options.c_cflag &= ~CRTSCTS;   /*no flow control*/
    218             break;
    219         case 1:
    220             options.c_cflag |= CRTSCTS;    /*hardware flow control*/
    221             break;
    222         case 2:
    223             options.c_cflag |= IXON|IXOFF|IXANY; /*software flow control*/
    224             break;
    225         default:
    226             options.c_cflag &= ~CRTSCTS;   /*no flow control*/
    227             break;
    228     }
    229 #if 0
    230     if(Rs485)
    231     {
    232         options.c_cflag |= (0x00000001<<23);
    233     }
    234     else
    235     {
    236         options.c_cflag &= (~(0x00000001<<23));
    237     }
    238 #else
    239 
    240     PRODUCT_INFO_ST productInfo;
    241 
    242      EX_GetProductInfo(g_hDvr, &productInfo);
    243      if((productInfo.productTypeID == MDVR_JH6S8_720P)||(productInfo.productTypeID == MDVR_JH6S4_720P))
    244      {
    245          if(Rs485)
    246          {
    247              options.c_cflag |= (0x00000001<<30);
    248          }
    249          else
    250          {
    251              options.c_cflag &= (~(0x00000001<<30));
    252          }
    253 
    254      }
    255 #endif
    256       /* Set input parity option */
    257       if (parity != 'n')
    258           options.c_iflag |= INPCK;
    259 
    260         options.c_cc[VTIME] = 10; // 1 seconds
    261         options.c_cc[VMIN] = 0;
    262 
    263       tcflush(m_fd,TCIFLUSH); /* Update the options and do it NOW */
    264       if (tcsetattr(m_fd,TCSANOW,&options) != 0)
    265       {
    266           perror("SetComParity 2");
    267         return (-1);
    268     }
    269 
    270       return (0);
    271 }
    272 
    273 //0---read ,1--write
    274 int CComTransmit::SetComSelectTimeOut(int sec,int usec,SELECT_TIMEOUT_F rwflag)
    275 {
    276     if(rwflag == WRITE_TIMEOUT_F)
    277     {
    278         Wtimeout.tv_sec = sec;
    279         Wtimeout.tv_usec = usec;
    280     }
    281     else
    282     {
    283         Rtimeout.tv_sec = sec;
    284         Rtimeout.tv_usec = usec;
    285     }
    286 
    287     return 0;
    288 }
    289 
    290 int CComTransmit::IOFlush()
    291 {
    292     tcflush(m_fd, TCIOFLUSH);
    293     return 0;
    294 }
    295 
    296 int CComTransmit::SetComRawModeEx()
    297 {
    298     int ret = 0;
    299     struct termios options;
    300 
    301     tcgetattr(m_fd, &options);
    302 
    303     tcflush(m_fd, TCIOFLUSH);
    304     options.c_cflag &= ~CSIZE;
    305     options.c_cflag |= CS8;
    306     options.c_cflag &= ~PARENB;
    307     options.c_cflag &= ~CSTOPB;
    308     options.c_cflag &= ~CSTOPB;
    309     options.c_oflag  &= ~OPOST;
    310 
    311 #if 0
    312     printf("before iflag %x, lflag %x, oflag  %x 
    ", options.c_iflag, options.c_lflag, options.c_oflag);
    313 
    314 //    options.c_lflag = 0 ;
    315 //    options.c_oflag = 0 ;
    316 //    options.c_iflag = 0;
    317     options.c_lflag &= ~(ICANON |ISIG);
    318     options.c_iflag &= ~(ICRNL|IGNCR);
    319 
    320           //options.c_iflag |= (IGNBRK|BRKINT);
    321           options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
    322 
    323     printf("iflag %x, lflag %x, oflag  %x 
    ", options.c_iflag, options.c_lflag, options.c_oflag);
    324 
    325 #endif
    326 
    327     //|PARMRK|ISTRIP|IGNCR|ICRNL|IXON|INLCR
    328     tcflush(m_fd, TCIFLUSH);
    329     options.c_cc[VTIME] = 0; //128;
    330     options.c_cc[VMIN] = 1;// 1;
    331 
    332     cfmakeraw(&options);
    333 
    334     ret = tcsetattr(m_fd,TCSANOW,&options);
    335     if (ret < 0)
    336     {
    337         perror("set raw mode error!");
    338         return ret;
    339     }
    340 
    341     return 0;
    342 }
    343 
    344 int CComTransmit::SetComRawMode()
    345 {
    346     int ret = 0;
    347     struct termios options;
    348 
    349     tcgetattr(m_fd, &options);
    350 
    351     cfmakeraw(&options);
    352     ret = tcsetattr(m_fd,TCSANOW,&options);
    353     if (ret < 0)
    354     {
    355         perror("set raw mode error!");
    356     }
    357 
    358     return ret;
    359 }
    360 
    361 int CComTransmit::ReadCom(char *ReadBuffer,int size,int *retSize)
    362 {
    363     int retval = -1;
    364 
    365     fd_set serial;
    366     struct timeval rdtimeout;
    367     *retSize = 0;
    368     if(m_fd < 0)
    369     {
    370         fprintf(stderr,"%s[%d]:serial have not open!
    ",__FILE__,__LINE__);
    371         return -1;
    372     }
    373 
    374     FD_ZERO(&serial);
    375     FD_SET(m_fd, &serial);
    376     rdtimeout = Rtimeout;
    377 #if 1
    378 
    379     do{
    380         retval = select(m_fd+1,  &serial, NULL, NULL, &rdtimeout);
    381         if(retval < 0)
    382         {
    383             if(errno == EINTR)
    384             {
    385                 //printf("select Interruppter!
    ");
    386             }
    387             else
    388             {
    389                 break;
    390             }
    391         }
    392     }while(retval == -1);
    393 
    394     if(retval <= 0)
    395     {
    396         return -1;
    397     }
    398 
    399      //读数据写BUFFER
    400     if(FD_ISSET(m_fd, &serial)!=0)
    401     {
    402         if(NULL != m_fp)
    403         {
    404             if(m_mutex_flag)
    405             {
    406                 pthread_mutex_lock(&ComCommutex);
    407                 fgets(ReadBuffer, size, m_fp);
    408                 pthread_mutex_unlock(&ComCommutex);
    409             }
    410             else
    411             {
    412                 fgets(ReadBuffer, size, m_fp);
    413             }
    414             *retSize = strlen(ReadBuffer);
    415         }
    416     }
    417     return 0;
    418 #else
    419         if(m_fd > 0)
    420         {
    421             retval = read(m_fd, ReadBuffer, size);
    422         }
    423 
    424         if(retval > 0)
    425         {
    426             *retSize = retval;
    427         }
    428         else
    429         {
    430             *retSize = 0;
    431         }
    432 
    433         return retval;
    434 
    435 #endif
    436 }
    437 
    438 int CComTransmit::ReadComEx( char *ReadBuffer,int size,int *retSize )
    439 {
    440     fd_set     read_fds;
    441     int     retval;
    442     struct timeval rdtimeout;
    443 
    444     rdtimeout.tv_sec  = Rtimeout.tv_sec;
    445     rdtimeout.tv_usec = Rtimeout.tv_usec;
    446 
    447     if(m_fd < 0)
    448     {
    449         fprintf(stderr,"%s[%d]:serial have not open!
    ",__FILE__,__LINE__);
    450         return -1;
    451     }
    452 
    453     FD_ZERO(&read_fds);
    454     FD_SET(m_fd, &read_fds);
    455 
    456     retval = select(m_fd + 1,  &read_fds, NULL, NULL, &rdtimeout);
    457     if( retval <= 0 )
    458     {
    459         return -1;
    460     }
    461 
    462     if ( !FD_ISSET(m_fd, &read_fds) )
    463     {
    464         //printf("Serail Com[%s] read time out[%d]
    ", m_SerialPath, time_out);
    465         return -1;
    466     }
    467 
    468     retval = read(m_fd, ReadBuffer, size);
    469     *retSize = retval;
    470 
    471     if(*retSize < 0)
    472     {
    473         return -1;
    474     }
    475 
    476     return 0;
    477 }
    478 
    479 
    480 /*
    481 *函数功能:读取串口binary数据
    482 *创建作者:freeman on 2010/0818
    483 */
    484 int CComTransmit::ReadBinCom(char *ReadBuffer,int size)
    485 {
    486     int retval = -1;
    487 
    488 #if 0
    489 
    490     fd_set serial;
    491     struct timeval rdtimeout;
    492 
    493     if(m_fd < 0)
    494     {
    495         fprintf(stderr,"%s[%d]:serial have not open!
    ",__FILE__,__LINE__);
    496         return -1;
    497     }
    498 
    499     FD_ZERO(&serial);
    500     FD_SET(m_fd, &serial);
    501     rdtimeout = Rtimeout;
    502 
    503     do{
    504         retval = select(m_fd+1,  &serial, NULL, NULL, &rdtimeout);
    505         if(retval < 0)
    506         {
    507             if(errno == EINTR)
    508             {
    509                 //printf("select Interruppter!
    ");
    510             }
    511             else
    512             {
    513                 break;
    514             }
    515         }
    516     }while(retval == -1);
    517 
    518     if(retval <= 0)
    519     {
    520         return -1;
    521     }
    522 
    523      //读数据写BUFFER
    524     if(FD_ISSET(m_fd,&serial)!=0)
    525     {
    526         if(NULL != m_fp)
    527         {
    528             return  read(m_fd,ReadBuffer,size);
    529         }
    530     }
    531     return 0;
    532 
    533 #else
    534 
    535     if(m_fd > 0)
    536     {
    537         retval = read(m_fd, ReadBuffer, size);
    538     }
    539     else
    540     {
    541         TRACE_NET(ERR,"ReadBinCom ERR:%d
    ",m_fd);
    542     }
    543     return retval;
    544 
    545 #endif
    546 }
    547 
    548 
    549 int CComTransmit::WriteCom(char *WriteBuffer,int size)
    550 {
    551     int     ret = -1;
    552 #if 1
    553     fd_set serial;
    554     int retval =0;
    555     struct timeval WTtimeout;
    556 
    557     if(m_fd < 0)
    558     {
    559         TRACE_NET(ERR, "---serial have not open!
    ");
    560         return -1;
    561     }
    562 
    563 //    printf("Snd Com %s  
    ", WriteBuffer);
    564 
    565     FD_ZERO(&serial);
    566     FD_SET(m_fd, &serial);
    567     WTtimeout = Wtimeout;
    568     if ((retval = select(m_fd+1,  NULL, &serial, NULL, &WTtimeout)) < 0)
    569     {
    570         TRACE_NET(ERR, "select error! ---- 
    ");
    571         return -1;
    572     }
    573 
    574     if(retval == 0)
    575     {
    576         TRACE_NET(ERR, "select timeout! ---- 
    ");
    577         return -1;
    578     }
    579 
    580     //读数据写BUFFER
    581     if(FD_ISSET(m_fd,&serial)!=0)
    582     {
    583         pthread_mutex_lock(&ComCommutex);
    584         ret = write(m_fd, WriteBuffer ,size);
    585         pthread_mutex_unlock(&ComCommutex);
    586 
    587         if(ret == -1)
    588         {
    589             TRACE_NET(ERR, "serial send data failed -----!
    ");
    590             return  -1;
    591         }
    592     }
    593 
    594     return ret;
    595 #else
    596     ret = write(m_fd, WriteBuffer ,size);
    597     //      printf("Send cmd %d  
    ", ret);
    598     if(ret == -1)
    599     {
    600         printf("serial send data failed -----error!
    ");
    601         return  -1;
    602     }
    603     return ret;
    604 
    605 #endif
    606 }
    607 
    608 int CComTransmit::WriteBinCom(char *WriteBuffer,int size)
    609 {
    610     int     ret = -1;
    611 #if 0
    612     fd_set serial;
    613        int retval =0;
    614     struct timeval WTtimeout;
    615 
    616     if(m_fd < 0)
    617     {
    618         fprintf(stderr,"%s:%d---serial have not open!
    ", __FILE__, __LINE__);
    619         return -1;
    620     }
    621 
    622 //    printf("Snd Com %s  
    ", WriteBuffer);
    623 
    624        FD_ZERO(&serial);
    625        FD_SET(m_fd, &serial);
    626     WTtimeout = Wtimeout;
    627        if ((retval = select(m_fd+1,  NULL, &serial, NULL, &WTtimeout)) < 0)
    628     {
    629               fprintf(stderr,"%s[%d]:select error!---------------
    ",__FILE__,__LINE__);
    630         return -1;
    631        }
    632 
    633     if(retval == 0)
    634     {
    635         fprintf(stderr,"%s[%d]:select timeout!---------------
    ",__FILE__,__LINE__);
    636         return -1;
    637     }
    638 
    639     //读数据写BUFFER
    640        if(FD_ISSET(m_fd,&serial)!=0)
    641     {
    642         pthread_mutex_lock(&Commutex);
    643                  ret = write(m_fd, WriteBuffer ,size);
    644         pthread_mutex_unlock(&Commutex);
    645 
    646         if(ret == -1)
    647         {
    648             printf("serial send data failed -----error!
    ");
    649             return  -1;
    650         }
    651     }
    652 
    653         return ret;
    654 #else
    655     if(m_fd > 0)
    656     ret = write(m_fd, WriteBuffer ,size);
    657     //       printf("Send cmd %d  
    ", ret);
    658     if(ret == -1)
    659     {
    660         TRACE_NET(ERR, "serial send data failed -----!
    ");
    661         return  -1;
    662     }
    663 
    664     return ret;
    665 #endif
    666 }
    667 
    668 
    669 int CComTransmit::GetPortFD()
    670 {
    671     return m_fd;
    672 }
    673 
    674 FILE* CComTransmit::GetPortFP()
    675 {
    676     return m_fp;
    677 }
    678 
    679 void  CComTransmit::SetSerialCommBlock(int isBlock)
    680 {
    681     int    nFlags = fcntl(m_fd, F_GETFL, 0);
    682 
    683     if(isBlock)
    684     {
    685         fcntl(m_fd, F_SETFL, 0);
    686     }
    687     else
    688     {
    689         fcntl(m_fd, F_SETFL, nFlags|O_NONBLOCK);
    690     }
    691 }
    692 
    693 
    694 int CComTransmit::ReadComWithBuf(char *ReadBuffer,int size,int *retSize)
    695 {
    696     fd_set serial;
    697     int retval;
    698     struct timeval rdtimeout;
    699     *retSize = 0;
    700     int left = 0;
    701     int recvlen = 0;
    702 
    703     if(m_fd < 0)
    704     {
    705         fprintf(stderr,"%s[%d]:serial have not open!
    ",__FILE__,__LINE__);
    706         return -1;
    707     }
    708 
    709     for(int i = 0;i < size - 1 && i < m_len;i++)
    710     {
    711         ReadBuffer[i] = m_buf[i];
    712         if(m_buf[i] == '
    ')
    713         {
    714             ReadBuffer[i+1] = 0;
    715             memmove(m_buf,&m_buf[i+1],m_len-(i+1));
    716             m_len -= (i+1);
    717             *retSize = i+1;
    718             return *retSize;
    719         }
    720     }
    721 
    722     FD_ZERO(&serial);
    723     FD_SET(m_fd, &serial);
    724     rdtimeout = Rtimeout;
    725 
    726     do{
    727         retval = select(m_fd+1,  &serial, NULL, NULL, &rdtimeout);
    728         if(retval < 0)
    729         {
    730             if(errno == EINTR)
    731             {
    732                 //printf("select Interruppter!
    ");
    733             }
    734             else
    735             {
    736                 break;
    737             }
    738         }
    739     }while(retval == -1);
    740 
    741     if(retval <= 0)
    742     {
    743         return -1;
    744     }
    745 
    746      //读数据写BUFFER
    747     if(FD_ISSET(m_fd,&serial)!=0)
    748     {
    749         if(NULL != m_fp)
    750         {
    751             left = SERIAL_BUF_LEN - m_len;
    752             if(left > 0)
    753             {
    754                 recvlen = read(m_fd,&m_buf[m_len],left);
    755                 if(recvlen > 0)
    756                 {
    757                     m_len += recvlen;
    758                 }
    759             }
    760             else
    761             {
    762         //        WRITE_DBG_LOG("串口接收数据512字节内没有换行符号");
    763                 m_buf[m_len-1] = 0;
    764         //        WRITE_DBG_LOG("%s",m_buf);
    765                 m_len = 0;
    766                 left = SERIAL_BUF_LEN;
    767                 recvlen = read(m_fd,&m_buf[m_len],left);
    768                 if(recvlen > 0)
    769                 {
    770                     m_len += recvlen;
    771                 }
    772             }
    773 
    774             for(int i = 0;i < size - 1 && i < m_len;i++)
    775             {
    776                 ReadBuffer[i] = m_buf[i];
    777                 if(m_buf[i] == '
    ')
    778                 {
    779                     ReadBuffer[i+1] = 0;
    780                     memmove(m_buf,&m_buf[i+1],m_len-(i+1));
    781                     m_len -= (i+1);
    782                     *retSize = i+1;
    783                     return *retSize;
    784                 }
    785             }
    786         }
    787     }
    788 
    789     return -1;
    790 }
    CComTransmit.cpp
     1 #ifndef _COM_TRANSMIT_H_
     2 #define _COM_TRANSMIT_H_
     3 
     4 #include <string.h>
     5 #include <pthread.h>
     6 #include <fcntl.h>
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 
    10 #define SERIAL_BUF_LEN  512
    11 
    12 typedef struct {
    13     unsigned long         baudrate;    /*波特率*/
    14     unsigned char         databit;        /*数据位,取值5、6、7、8*/
    15     unsigned char         parity;        /*奇偶校验位,取值'n':无校验'o':奇校验'e'偶校验*/
    16     unsigned char         stopbit;        /*停止位,取值1,2*/
    17     unsigned char         reserve;        /*保留字节*/
    18 }Terminal;
    19 
    20 typedef struct
    21 {
    22     int  sysSpeed;
    23     unsigned long  userSpeed;
    24 }speedPara;
    25 
    26 typedef enum
    27 {
    28     READ_TIMEOUT_F = 0,
    29     WRITE_TIMEOUT_F
    30 }SELECT_TIMEOUT_F;
    31 
    32 class CComTransmit
    33 {
    34 private:
    35     //
    36     FILE     *m_fp;
    37     int         m_fd;
    38     int        m_mutex_flag;
    39     struct timeval Rtimeout;
    40     struct timeval Wtimeout;
    41     int     m_binit;
    42     char    m_buf[SERIAL_BUF_LEN];
    43     int        m_len;
    44 
    45 public:
    46     //
    47     CComTransmit();
    48     ~CComTransmit();
    49     int OpenCom(const char *deviceName, int isMutex = 1);
    50     void CloseCom();
    51     int SetComSpeed(unsigned int speed);
    52     int SetComParity(int databits, int stopbits, int parity,int flowctrl, int Rs485);
    53     int SetComRawMode();
    54     int SetComRawModeEx();
    55     int SetComSelectTimeOut(int sec,int usec,SELECT_TIMEOUT_F RWflag);//0--read,1--wirte
    56     int IOFlush();
    57     int ReadCom(char *ReadBuffer,int size,int *retSize);
    58     int ReadComEx( char *ReadBuffer,int size,int *retSize );
    59     int ReadBinCom(char *ReadBuffer,int size);
    60     int WriteCom(char *WriteBuffer,int size);
    61     int WriteBinCom(char *WriteBuffer,int size);
    62 
    63     int GetPortFD();
    64     FILE* GetPortFP();
    65     void  SetSerialCommBlock(int isBlock);
    66     int ReadComWithBuf(char *ReadBuffer,int size,int *retSize);
    67 
    68 
    69 protected:
    70     //
    71 };
    72 
    73 #endif//_COM_TRANSMIT_H_
    CComTransmit.h
  • 相关阅读:
    Windows上部署MySql
    LeetCode 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树
    LeetCode 把二叉搜索树转换为累加树
    Spring Cloud Eureka 分布式开发之服务注册中心、负载均衡、声明式服务调用实现
    mysql事务详解
    Java并发编程之ThreadLocal解析
    redis之mq实现发布订阅模式
    Zookeeper之Leader选举过程
    Spring Boot MyBatis 数据库集群访问实现
    分布式配置中心之spring-cloud-config
  • 原文地址:https://www.cnblogs.com/yuweifeng/p/7464836.html
Copyright © 2011-2022 走看看