?? serialport.cpp
字號:
#include "SerialPort.h"#include <iostream>#include <sys/ioctl.h>using namespace std ;using namespace SerialPorts ;int SerialPort::open(char * devicename) { this->fd = ::open(devicename, O_RDWR | O_NOCTTY | O_NDELAY); if (this->fd == -1) { /* * Could not open the port. */ //printf("open_port: Unable to open %s\n", serialPort); this->state = -1; } else { /* * Restore blocking behaviour of the port, as we opened it with O_NDELAY */ struct termios options; /* Get Current Settings of the fileDescriptor */ if ( tcgetattr(this->fd, &options) != 0 ){ this->state = -1; return this->state; } options.c_cflag |= (CLOCAL | CREAD); options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); if ( tcsetattr(this->fd, TCSANOW, &options) != 0 ) { this->state = -1; return this->state; } fcntl(fd, F_SETFL, 0); this->state = 1; } return this->state;}int SerialPort::close(void) { this->state = -1; return ::close(this->fd);}int SerialPort::status() { return this->state;}int SerialPort::write(char * data, int lenght) {// cout << "Size van data is : " << sizeof(data) << endl;// cout << "Size van data is : " << sizeof(data) << endl;// cout << "String is : " << data << endl; return ::write(this->fd, data, lenght);}char SerialPort::readChar() { char character = '\0'; char * character_ptr; int bytesRecv; character_ptr = &character; bytesRecv = read(this->fd, character_ptr, sizeof(character)); if ( bytesRecv == sizeof(character) ){ return character; } return 0;}char * SerialPort::readBytes(int bytes) { char * buffer = (char *) malloc(bytes); int bytesRead; bytesRead = ::read(this->fd, buffer, bytes); if ( bytesRead != bytes ) { cout << "OOPS bytes <> bytes_wait" << endl; } return buffer;}int SerialPort::dataWaiting() { int bytes; ioctl(this->fd, FIONREAD, &bytes); return bytes;}enum BaudRateEnum SerialPort::setBaudRate( enum BaudRateEnum baud) { if ( ! this->state ) { this->baud = BAUD_ERR; return this->baud; } switch ( baud ) { case BAUD_50: case BAUD_75: case BAUD_110: case BAUD_134: case BAUD_150: case BAUD_200: case BAUD_300: case BAUD_600: case BAUD_1200: case BAUD_1800: case BAUD_2400: case BAUD_4800: case BAUD_9600: case BAUD_19200: case BAUD_38400: case BAUD_57600: case BAUD_115200: struct termios options; /* Get Current Settings of the fileDescriptor */ if ( tcgetattr(this->fd, &options) != 0 ){ this->state = -1; } /* Set port speed to xxx baud */ if ( cfsetispeed(&options, baud) != 0 ){ this->state = -1; } if ( cfsetospeed(&options, baud) != 0 ){ this->state = -1; } if ( this->state == -1 ) { this->baud = BAUD_ERR; } else { this->baud = baud; } return this->baud; break ; default: this->baud = BAUD_ERR; return this->baud; break; };//switch}enum CharSizeEnum SerialPort::setCharSize( enum CharSizeEnum charsize) { if ( ! this->state ) { this->charsize = CHAR_SIZE_ERR; return this->charsize; } struct termios options; if ( tcgetattr(this->fd, &options) != 0 ) { this->state = -1; this->charsize = CHAR_SIZE_ERR; return this->charsize; } switch ( charsize ) { case CHAR_SIZE_5: case CHAR_SIZE_6: case CHAR_SIZE_7: case CHAR_SIZE_8: if( charsize == CHAR_SIZE_8 ) { options.c_iflag &= ~ISTRIP ; // clear the ISTRIP flag. } else { options.c_iflag |= ISTRIP ; // set the ISTRIP flag. } options.c_cflag &= ~CSIZE; options.c_cflag |= charsize; if ( tcsetattr(this->fd, TCSANOW, &options) != 0 ){ this->state = -1; this->charsize = CHAR_SIZE_ERR; } else { this->charsize = charsize; } return this->charsize; break ; default: this->charsize = CHAR_SIZE_ERR; return this->charsize; break; }//switch}enum ParityEnum SerialPort::setParity( enum ParityEnum parity) { if ( ! this->state ) { this->parity = PARITY_ERR; return this->parity; } struct termios options ; if ( tcgetattr(this->fd, &options) != 0 ) { this->state = -1; this->parity = PARITY_ERR; return this->parity; } switch ( parity ) { case PARITY_EVEN: options.c_cflag |= PARENB ; options.c_cflag &= ~PARODD ; break ; case PARITY_ODD: options.c_cflag |= PARENB ; options.c_cflag |= PARODD ; break ; case PARITY_NONE: options.c_cflag &= ~PARENB ; break ; default: this->parity = PARITY_ERR; return this->parity; } if ( tcsetattr(this->fd, TCSANOW, &options) != 0 ){ this->state = -1; this->parity = PARITY_ERR; } else { this->parity = parity; } return this->parity;}int SerialPort::setStopBits(int stopbits) { if ( ! this->state ) { this->stopbits = -1; return this->parity; } struct termios options ; if ( tcgetattr(this->fd, &options) != 0 ) { this->state = -1; this->stopbits = -1; return this->parity; } switch ( stopbits ) { case 1: options.c_cflag &= ~CSTOPB ; break; case 2: options.c_cflag |= CSTOPB ; break; default: this->stopbits = -1; return this->stopbits; break; } if ( tcsetattr(this->fd, TCSANOW, &options) != 0 ){ this->state = -1; this->stopbits = -1; return this->stopbits; } this->stopbits = stopbits; return this->stopbits;}enum FlowControlEnum SerialPort::setFlowControl( enum FlowControlEnum flowctrl) { if ( ! this->state ) { this->flowctrl = FLOW_CONTROL_ERR; return this->flowctrl; } struct termios options ; if ( tcgetattr(this->fd, &options) != 0 ) { this->state = -1; this->flowctrl = FLOW_CONTROL_ERR; return this->flowctrl; } switch ( flowctrl ) { case FLOW_CONTROL_HARD: options.c_iflag &= ~ (IXON|IXOFF); options.c_cflag |= CRTSCTS; options.c_cc[VSTART] = _POSIX_VDISABLE; options.c_cc[VSTOP] = _POSIX_VDISABLE; break; case FLOW_CONTROL_SOFT: options.c_iflag |= IXON|IXOFF; options.c_cflag &= ~CRTSCTS; options.c_cc[VSTART] = CTRL_Q ; // 0x11 (021) ^q options.c_cc[VSTOP] = CTRL_S ; // 0x13 (023) ^s break; case FLOW_CONTROL_NONE: options.c_iflag &= ~(IXON|IXOFF); options.c_cflag &= ~CRTSCTS; break; default: this->flowctrl = FLOW_CONTROL_ERR; return this->flowctrl; break; } if ( tcsetattr(this->fd, TCSANOW, &options) != 0 ){ this->state = -1; this->flowctrl = FLOW_CONTROL_ERR; return this->flowctrl; } this->flowctrl = flowctrl; return this->flowctrl;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -