赞
踩
- #ifndef SERIALCOM_H
- #define SERIALCOM_H
- #include <sys/select.h>
-
- #include <stdio.h>
- class SerialCom
- {
- public:
- SerialCom();
- ~SerialCom();
- int openCom(char *pcom,int baud);
- int sent_to(char *pstr,int len);
- int recv_from(char *precvBuf);
- void closeCom();
- int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);
- private:
- int comHandle;
- fd_set m_stRfds;
- };
-
- #endif // SERIALCOM_H
- #include "serialcom.h"
-
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <termios.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
-
- SerialCom::SerialCom()
- {
- comHandle = 0;
- }
- SerialCom::~SerialCom()
- {
- if(comHandle)
- closeCom();
- comHandle = 0;
- }
-
- int SerialCom::set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
- {
- struct termios newtio,oldtio;
- if ( tcgetattr( fd,&oldtio) != 0) {
- perror("SetupSerial 1");
- g_MainCoreLog->LogError("[%s][%s]()-[%d]SetupSerial "
- , __FILE__, __FUNCTION__, __LINE__);
-
- return -1;
- }
- bzero( &newtio, sizeof( newtio ) );
- newtio.c_cflag |= CLOCAL | CREAD;
- newtio.c_cflag &= ~CSIZE;
-
- switch( nBits )
- {
- case 7:
- newtio.c_cflag |= CS7;
- break;
- case 8:
- newtio.c_cflag |= CS8;
- break;
- }
-
- switch( nEvent )
- {
- case 'O':
- newtio.c_cflag |= PARENB;
- newtio.c_cflag |= PARODD;
- newtio.c_iflag |= (INPCK );
- break;
- case 'E':
- newtio.c_iflag |= (INPCK);
- newtio.c_cflag |= PARENB;
- newtio.c_cflag &= ~PARODD;
- break;
- case 'N':
- newtio.c_cflag &= ~PARENB;
- break;
- }
-
- switch( nSpeed )
- {
- case 2400:
- cfsetispeed(&newtio, B2400);
- cfsetospeed(&newtio, B2400);
- break;
- case 4800:
- cfsetispeed(&newtio, B4800);
- cfsetospeed(&newtio, B4800);
- break;
- case 9600:
- cfsetispeed(&newtio, B9600);
- cfsetospeed(&newtio, B9600);
- break;
- case 115200:
- cfsetispeed(&newtio, B115200);
- cfsetospeed(&newtio, B115200);
- break;
- case 460800:
- cfsetispeed(&newtio, B460800);
- cfsetospeed(&newtio, B460800);
- break;
- default:
- cfsetispeed(&newtio, B9600);
- cfsetospeed(&newtio, B9600);
- break;
- }
- if( nStop == 1 )
- {
- newtio.c_cflag &= ~CSTOPB;
- }
- else if ( nStop == 2 )
- {
- newtio.c_cflag |= CSTOPB;
- }
-
- newtio.c_cc[VTIME] = 1;///* 设置超时10 seconds*/
- newtio.c_cc[VMIN] = 8;
- tcflush(fd,TCIFLUSH);
- if((tcsetattr(fd,TCSANOW,&newtio))!=0)
- {
- perror("com set error");
- g_MainCoreLog->LogError("[%s][%s]()-[%d]com set error "
- , __FILE__, __FUNCTION__, __LINE__);
- return -1;
- }
-
- return 0;
- }
- int SerialCom::openCom(char *pcom,int baud)
- {
- struct termios termios;
- g_MainCoreLog->LogInfo("[%s][%s]()-[%d]openCom "
- , __FILE__, __FUNCTION__, __LINE__);
- bzero(&termios,sizeof(termios));
-
- comHandle = open(pcom,O_RDWR|O_NOCTTY); //|O_NOCTTY|O_NONBLOCK
- if(comHandle < 0)
- {
- g_MainCoreLog->LogError("[%s][%s]()-[%d]com open failed "
- , __FILE__, __FUNCTION__, __LINE__);
- return -1;
- }
- g_MainCoreLog->LogInfo("[%s][%s]()-[%d]opecom open successed,COM %d Baud:%d "
- , __FILE__, __FUNCTION__, __LINE__,comHandle,baud);
- set_opt(comHandle, baud, 8, 'N', 1);
-
- return 0;
- }
- int SerialCom::sent_to(char *pstr,int len)
- {
- if(comHandle <= 0)
- return -1;
-
- if (write(comHandle, pstr, len) != len)
- {
- g_MainCoreLog->LogError("[%s][%s]()-[%d]send cmd failed "
- , __FILE__, __FUNCTION__, __LINE__);
- return -1;
- }
-
- return 0;
-
- }
- int SerialCom::recv_from(char *precvBuf)
- {
- if(comHandle <= 0)
- {
- printf("failed comHandle <= 0 \r\n");
- g_MainCoreLog->LogError("[%s][%s]()-[%d]failed comHandle <= 0 "
- , __FILE__, __FUNCTION__, __LINE__);
- return -1;
- }
-
- int rlen = 0;
- //for(int i=0;i<8;i++)
- {
- // printf("start recv 1 byte \r\n");
- tcflush(comHandle,TCIFLUSH);
- int iNum = read(comHandle,(void*)precvBuf,8);
- // int iNum = read(comHandle,(void*)precvBuf,8);
- //printf("iNum:%d \r\n",iNum);
- rlen+=iNum;
- }
- return rlen;
- }
- void SerialCom::closeCom()
- {
- if(comHandle <= 0)
- return;
- close(comHandle);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。