📄 serialport.cpp
字号:
#include "serialport.h"TSerialPort::TSerialPort(string ACommPort){ CommPort=ACommPort; hComm = -1; ClearCounters(); options.c_cflag=0; //Setting character size to 8 bit options.c_cflag &= ~CSIZE; //options.c_cflag |= CS8; //Setting parity to noparity options.c_cflag &= ~PARENB; //Set one stop bit options.c_cflag &= ~CSTOP; cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_lflag = 0; options.c_iflag = 0; options.c_oflag = 0; options.c_cc[VINTR]=0; options.c_cc[VQUIT]=0; options.c_cc[VERASE]=0; options.c_cc[VKILL]=0; options.c_cc[VEOF]=0; options.c_cc[VEOL]=0; options.c_cc[VEOL2]=0; options.c_cc[VMIN]=0; options.c_cc[VSTART]=0; options.c_cc[VSTOP]=0; options.c_cc[VTIME]=0;};TSerialPort::~TSerialPort(void){ ClosePort();};int TSerialPort::OpenPort(void){ if (hComm!=-1){ errno = 2; return errno; }; //hComm = open(CommPort.c_str() , O_RDWR | O_NOCTTY | O_NDELAY); hComm = open(CommPort.c_str() , O_RDWR); if (hComm == -1){ // Could not open the port. return errno; }; fcntl(hComm, F_SETOWN, getpid()); fcntl(hComm, F_SETFL, O_SYNC); tcsetattr(hComm, TCSANOW, &options); return 0;};int TSerialPort::ClosePort(void){ if (hComm!=-1){ if (close(hComm)!=0){ return errno; }; hComm=-1; errno=0; return 0; }else{ errno=9; return errno; };};int TSerialPort::SetCommPort(string ACommPort){ if (hComm==-1){ CommPort=ACommPort; return 0; }else{ errno=1; return errno; };}; int TSerialPort::Write(void *Buf, int DataSizeRequest, int *DataSizeWrite){ int R; if (hComm!=-1){ TransmittedBR+=DataSizeRequest; R=write(hComm, Buf, DataSizeRequest); fdatasync(hComm); if (R<0){ //Nastala chyba return errno; }else{ //Vse v pohode; TransmittedB+=R; if (DataSizeWrite!=NULL){ *DataSizeWrite=R; }; if (R<DataSizeRequest) { errno=13; return errno; }; errno=0; return 0; }; }else{ if (DataSizeWrite!=NULL){ *DataSizeWrite=0; }; errno=9; return errno; };};int TSerialPort::SetBaudRate(int NewBaudRate){ cfsetispeed(&options, NewBaudRate); cfsetospeed(&options, NewBaudRate); if (hComm!=-1){ if (tcsetattr(hComm, TCSANOW, &options)!=0){ return errno; }; }; errno=0; return 0; };int TSerialPort::PurgeBuffers(void){ if (hComm!=0){ if (tcflush(hComm, TCIFLUSH)<0){ return errno; }; }; return 0;};int TSerialPort::Read(void *Buf, int DataSizeRequest, int *DataSizeRead){ int R; if (hComm!=-1){ ReceivedBR+=DataSizeRequest; R=read(hComm, Buf, DataSizeRequest); if (R<0){ //Nastala chyba return errno; }else{ //Vse v pohode; ReceivedB+=R; if (DataSizeRead!=NULL){ *DataSizeRead=R; }; if (R<DataSizeRequest) { errno=13; return errno; }; errno=0; return 0; }; }else{ errno=9; return errno; }; return 0;};bool TSerialPort::Opened(void) //Otestovani, zda je port otevreny{ return (hComm!=-1);};int TSerialPort::SetTimeOut(int AVTIME, int AVMIN){ options.c_cc[VMIN]=AVMIN; options.c_cc[VTIME]=AVTIME; if (hComm!=-1){ if (tcsetattr(hComm, TCSANOW, &options)!=0){ return errno; }; }; errno=0; return 0; };void TSerialPort::ClearCounters(void){ TransmittedB=0; TransmittedBR=0; ReceivedB=0; ReceivedBR=0;};unsigned long long TSerialPort::TransmittedBytes(void){ return TransmittedB;};unsigned long long TSerialPort::TransmittedBytesRequest(void){ return TransmittedBR;};unsigned long long TSerialPort::ReceivedBytes(void){ return ReceivedB;};unsigned long long TSerialPort::ReceivedBytesRequest(void){ return ReceivedBR;};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -