int ComPort::init(unsigned int comport,int boud_rate){ this->num = comport; fstream of_file; //cout<<"create stringstream"<<endl; stringstream ss; ss<<"/dev/ttyS"<<(num-1); string s = ss.str(); cout<< "open com for write and read: " <<s<<endl; BaudRateType s_BaudRateType; switch(boud_rate) { case 9600: s_BaudRateType=BAUD9600; break; case 115200: s_BaudRateType=BAUD115200; break; default: s_BaudRateType=BAUD9600; break; } m_QextSerialPort.setName(s.data()); m_QextSerialPort.setBaudRate(s_BaudRateType); m_QextSerialPort.setDataBits(DATA_8); m_QextSerialPort.setStopBits(STOP_1); m_QextSerialPort.setTimeout(2,500); m_QextSerialPort.open(); return 0;}int ComPort::write(string packet, int wait_milisecs){ if(num==-1) return 1; if(!m_QextSerialPort.isOpen()) { cout<< "port not open"<<endl; return 2; } cout<< "write[ "; for(int i=0;i<packet.size();i++) cout<<setw(2)<<hex<<setfill('0')<<(int)packet[i]<<" "; cout<< "]"<<endl; int c_write = m_QextSerialPort.writeBlock(packet.data(),packet.size()); cout<<dec<<"writen in com queue: "<< c_write<<" bytes"<<endl; return 0;}int ComPort::read( string &packet, int size, unsigned char stared_char, unsigned char stoped_char){ if(num==-1) return 1; if(!m_QextSerialPort.isOpen()) { cout<< "port not open"<<endl; return 2; } cout<< "clear packet string"<<endl; packet.clear(); cout<<"waiting chars in port queue..."<<endl; int i_ret = m_QextSerialPort.bytesWaiting(); if(i_ret==-1) { cout<<"waiting error."<<endl; return 3; } unsigned char buf; while(1) { int c_readed = m_QextSerialPort.readBlock((char*)&buf,1); if(c_readed==0) { cout<< "readed from com stream: VOID"<<endl; break; } if(c_readed>0) { cout<<"(r)>>"<<hex<<(unsigned int)buf<<endl; packet.push_back(buf); } else{ cout<< "read failed"<<endl; return 2; } if(buf==stoped_char) { cout<<"stoped byte was readed. break."<<endl; break; } cout<<"waiting chars in port queue..."<<endl; int i_ret = m_QextSerialPort.bytesWaiting(); if(i_ret==-1) { cout<<"waiting break."<<endl; break; } } cout<<dec<<"readed from com queue: "<< packet.size()<<" bytes"<<endl; cout<< "readed["; for(int i=0;i<packet.size();i++) cout<<setw(2)<<hex<<setfill('0')<<(int)(unsigned char)packet[i]<<" "; cout<< "]"<<endl; return 0;}