当前位置: 首页>>代码示例>>C++>>正文


C++ Serial::openCommPort方法代码示例

本文整理汇总了C++中Serial::openCommPort方法的典型用法代码示例。如果您正苦于以下问题:C++ Serial::openCommPort方法的具体用法?C++ Serial::openCommPort怎么用?C++ Serial::openCommPort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Serial的用法示例。


在下文中一共展示了Serial::openCommPort方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc, char * argv[])
{
   char newline = '\n';
   char cmd[2] = {'V', 'V'};
   char blockread[] = { '5', 'R' , 0x00, 0x00, 0x00, 0x00 };
   char block[257];
   unsigned char sum;

   int tmp, i, j ;
   char tmpc;
   Serial serialPort;

   if(argc != 3)
   {
      cerr<< "Usage: " << argv[0] << " <Serial device> <string to send>" << endl;
      return false;
   }

   std::string pname(argv[1]);
   std::string command(argv[2]);

   if(serialPort.setPort(pname))
      cerr << "setPort OK" << endl;
   else
      cerr << "setPort NOK" << endl;

   if(serialPort.openCommPort())
      cerr << "openCommPort OK " << endl;
   else
      cerr << "openCommPort NOK: " << pname << endl;

   serialPort.setSpeedAndDataBits(921600,8,'n',1);

   serialPort.setTimeouts(5,50,50,50,50);

   if( serialPort.applySettings())
      cerr << "applySettings OK" << endl;
   else
      cerr << "applySettings NOK" << endl;

   if (!serialPort.sendBytes(( char *) command.c_str(), command.length()))
   {
      cerr << "Error with sending command" << endl;
      return false;
   }
   else
      cerr << "sendBytes OK" << endl;

   while(serialPort.getByte(&tmpc))
      cout << "returned byte is: " << hex << (int) tmpc << endl;

   for( i = 0; i < 256; i++)
   {
      cerr << "reading block: " << i ;
      blockread[3] = i;

      for(sum = j = 0; j < 5; j++)
         sum += blockread[j];

      blockread[5] = sum;

      if(!serialPort.sendBytes(blockread, 6))
         cerr << " send commands for blockread failed" << endl;

      else if(!serialPort.getBytes(block, 257))
         cerr << " read data failed" << endl;

      for(sum = j = 0; j < 256; j++)
         sum += block[j];

      if( sum != block[256] )
         cerr << "checksum invalid " << endl;
      else
         cerr << " OK " << endl;

   }
   return true;
}
开发者ID:keith-daigle,项目名称:moates,代码行数:78,代码来源:SerialDriver.cpp


注:本文中的Serial::openCommPort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。