源码地址:
https://download.csdn.net/download/zlyadvocate/12562897
void GasmeterManager::run()
{
std::string accrbuff;
std::string accrstr;
int ret;
vector<string> vec;
std::string::size_type sz; // alias of size_t
size_t xdpos,m_pos;
/*优化方案:此处将串口每一句作为一个节点保存到链表中,另一个线程去以此取节点解析*/
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); //允许退出线程
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); //设置立即取消
while(1){
//std::cout << "GPSDataManager run() wait"<<std::endl;
ret = Gasmeterserialport.GetGpsStringData(accrbuff);
//std::cout<<"***"<<accrbuff<<"***"<<std::endl;
if(((m_pos = accrbuff.find("#")) != std::string::npos)&&((xdpos = accrbuff.find("*XD")) != std::string::npos))
{
//std::string class provides a member function string::erase() to remove some characters from a given position i.e.
//string& erase (size_t pos = 0, size_t len = npos);
// accrbuff.erase(tpos);
// accrbuff.erase(m_pos);
accrstr=accrbuff.substr(xdpos+4,m_pos-8);//
unsigned short checksum=0,s_checksum=0;
char m_unsigned[1024];
memcpy(m_unsigned,accrstr.c_str(),accrstr.length());
for (int i = 0; i <accrstr.length() ; i++)
checksum=m_unsigned[i]+checksum;
// cout <<"checksum:"<<checksum <<endl;
boost::split(vec, accrbuff,boost::is_any_of(","), boost::token_compress_on);
// cout<<"vect size is:"<<vec.size()<<endl;
if(vec.size()==8)
{
s_checksum=std::stoi (vec[7],&sz);
cout <<"s_checksum:"<<s_checksum<<"checksum"<<checksum<<endl;
if(s_checksum==checksum)
{
//_hours=std::stoi (vec[1],&sz);
// gasmeterID=std::stoi (vec[2],&sz);
oillervel=std::stoi (vec[3],&sz);
realtimedata=std::stoi (vec[4],&sz);
// signalquatity=std::stoi (vec[5],&sz);
oiltemp=std::stoi (vec[6],&sz);
gasmeter_event(oillervel,realtimedata,oiltemp);
}
//
}
}
// cout <<"gasmeterID is "<<gasmeterID<<endl;
// cout <<_hours<<gasmeterID<<oillervel<<realtimedata<<signalquatity<<oiltemp<<endl;
usleep(2000*1000);
}
}
主处理流程每2秒读取一次数据并进行解析。
源码地址:
https://download.csdn.net/download/zlyadvocate/12562897