#include "serial2.h" #include #include #include CSerial2::CSerial2() { } CSerial2::~CSerial2() { tcsetattr(fd,TCSANOW,&oldtio); close(fd); } void CSerial2::Open(char *cPortName, speed_t spBaud) { fd = open(cPortName, O_RDWR | O_NOCTTY); if (fd < 0) {perror(cPortName); _exit(-1); } tcgetattr(fd, &oldtio); // Save current port settings. bzero(&newtio, sizeof(newtio)); //Initialize the struct. tcgetattr(fd, &newtio); //Get the current port settings. newtio.c_cflag |= (CLOCAL | CREAD); //Enable the receiver and set local mode... cfsetispeed(&newtio, spBaud); cfsetospeed(&newtio, spBaud); newtio.c_cflag &= ~PARENB; //No parity. newtio.c_cflag &= ~CSTOPB; //1 stop bit. newtio.c_cflag &= ~CSIZE; //Mask the character size bits . newtio.c_cflag |= CS8; //8 data bits. newtio.c_cflag &= ~CRTSCTS; ; //No hardware flow control. // set raw input mode (non-canonical, no echo,...) newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // set raw output mode (non-canonical, no echo,...) newtio.c_oflag &= ~OPOST; //newtio.c_cc[VTIME] = 1; /* inter-character timer unused */ //newtio.c_cc[VMIN] = 1; /* blocking read until 5 chars received */ tcflush(fd, TCIFLUSH); tcflush(fd, TCOFLUSH); tcsetattr(fd,TCSANOW,&newtio); } int CSerial2::SendData(const unsigned char* buffer, int size) { int iBytesWritten; iBytesWritten = write(fd, buffer, size); if(iBytesWritten < 0) { printf("A packet failed to be sent! Exiting...\n"); _exit(-1); } return iBytesWritten; } int CSerial2::SendData(const unsigned char *buffer) { return SendData(buffer, strlen((const char*)buffer)); } int CSerial2::ReadDataBlocking(unsigned char* buffer, int limit, int iVerbose) { int accumBytesRead = 0; unsigned char tmpBuff[6000]; do { int dwBytesRead = read(fd, tmpBuff, limit); if(dwBytesRead < 0) dwBytesRead = 0; if( dwBytesRead != 0 ) { for( int i=0; i