Linux libserial库使用2-----定时读取串口数据

源码地址:
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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值