⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 control.~cpp

📁 c++builder编写的机器人参数传递上位机软件.
💻 ~CPP
📖 第 1 页 / 共 3 页
字号:
//-----------------------------------------------
/*
 unsigned long lrc,BS;
  BS=4;
  DWORD nBytesRead,dwError,inack;
  COMSTAT cs;   //端口状态

  if (hComm==0) return;


//判断当前读取缓冲区中是否只有"REQU"响应字符
  ClearCommError(hComm,&dwError,&cs); //清除错误,检测状态
  if  (cs.cbInQue != 4)
    {
      ShowMessage("Read Buffer Error!There are more or less bytes in Read Buffer than REQU!");
    }
   else
    {
     ReadFile(hComm,(char *)&inack,BS,&nBytesRead,NULL);
     if (inack != 0x55514552) //"REQU"
        {
         ShowMessage("Ack Error!The bytes in Read Buffer is not REQU!");
        }
     else
//有"REQU"响应,可以写出数据
        {
         WriteFile(hComm,(char*)&parameter,BS,&lrc,NULL);
//清除接收缓存器内容
         PurgeComm(hComm,PURGE_RXCLEAR);
        }
     }
//-----------------------------------------------
//等待接受返回数据


//下位机将反馈42个参数,加上首地址两个不用字节共86个字节,然后再反馈"REQU"
//读取86个字节后,这些字节从读取缓冲区中消除,剩下"REQU"供发送时判断

    ClearCommError(hComm,&dwError,&cs);  //清除错误,检测状态

    while (cs.cbInQue < 86)   //接收86字节
     {
        ClearCommError(hComm,&dwError,&cs);
        ReadFlag = 0;   //  接收缓冲区不够86字节
        if ( ReadDelayFlag == 1 )
           {
            ShowMessage("Read Buffer Error!Bytes in Read Buffer less than parameters!");
            break;
           }
     }
    if ( ReadDelayFlag == 1)
       {
        ReadDelayFlag = 0;
        return;
       }
    ReadFlag = 1;   //   接收缓冲区够86字节
    ReadFile(hComm,(char *)accept_parameter,86,&nBytesRead,NULL);
//将数据读入参数数组中供显示修改,注意与发送要一致
//accept_parameter[0]无效
    for ( i = 0 ; i < 10 ; i++ )
        {
          parameterA[i] = accept_parameter[i+1];
          parameterB[i] = accept_parameter[i+11];
        }
    for ( i = 0 ; i < 16 ; i++ )
        {
          parameterMacro[i] =  accept_parameter[i+21];
        }
    for ( i = 0 ; i < 6 ; i++ )
        {
          parameterMicro[i] =  accept_parameter[i+37];
        }
//显示参数
//---------------
//行走电机(A)参数
//---------------
   Edit9->Text  = IntToStr(parameterA[0]); //电机A运行换相时间
   Edit10->Text = IntToStr(parameterA[1]); //电机A制动换相时间
   Edit11->Text = IntToStr(parameterA[2]); //电机A运行控制参数1
   Edit57->Text = IntToStr(parameterA[3]); //电机A制动控制参数1
//运行控制参数2
   high8bit = parameterA[4];
   low8bit  = parameterA[4];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit12->Text = IntToStr(high8bit);
   Edit13->Text = IntToStr(low8bit);
//制动控制参数2
   high8bit = parameterA[5];
   low8bit  = parameterA[5];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit18->Text = IntToStr(high8bit);
   Edit19->Text = IntToStr(low8bit);
//运行控制参数3
   high8bit = parameterA[6];
   low8bit  = parameterA[6];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit14->Text = IntToStr(high8bit);
   Edit15->Text = IntToStr(low8bit);
//制动控制参数3
   high8bit = parameterA[7];
   low8bit  = parameterA[7];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit20->Text = IntToStr(high8bit);
   Edit21->Text = IntToStr(low8bit);
//相位索引1
   Edit16->Text = IntToStr(parameterA[8]);
//相位索引2
   Edit17->Text = IntToStr(parameterA[9]);
//---------------
//转向电机(B)参数
//---------------
   Edit22->Text = IntToStr(parameterB[0]); //电机A运行换相时间
   Edit23->Text = IntToStr(parameterB[1]); //电机A制动换相时间
   Edit24->Text = IntToStr(parameterB[2]); //电机A运行控制参数
   Edit58->Text = IntToStr(parameterB[3]); //电机A制动控制参数
//运行控制参数2
   high8bit = parameterB[4];
   low8bit  = parameterB[4];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit25->Text = IntToStr(high8bit);
   Edit26->Text = IntToStr(low8bit);
//制动控制参数2
   high8bit = parameterB[5];
   low8bit  = parameterB[5];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit38->Text = IntToStr(high8bit);
   Edit39->Text = IntToStr(low8bit);
//运行控制参数3
   high8bit = parameterB[6];
   low8bit  = parameterB[6];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit34->Text = IntToStr(high8bit);
   Edit35->Text = IntToStr(low8bit);
//制动控制参数3
   high8bit = parameterB[7];
   low8bit  = parameterB[7];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit40->Text = IntToStr(high8bit);
   Edit41->Text = IntToStr(low8bit);
//相位索引1
   Edit36->Text = IntToStr(parameterB[8]);
//相位索引2
   Edit37->Text = IntToStr(parameterB[9]);
//------------
//宏观场景参数
//------------
   Edit27->Text = IntToStr(parameterMacro[0]); //目标X
   Edit28->Text = IntToStr(parameterMacro[1]); //目标Y
   Edit29->Text = IntToStr(parameterMacro[2]); //机器人最终角
   Edit30->Text = IntToStr(parameterMacro[3]); //区域半径0
   Edit31->Text = IntToStr(parameterMacro[4]); //区域半径1
   Edit32->Text = IntToStr(parameterMacro[5]); //区域半径2
   Edit33->Text = IntToStr(parameterMacro[6]); //区域半径3
   Edit42->Text = IntToStr(parameterMacro[7]); //区域0步进
   Edit43->Text = IntToStr(parameterMacro[8]); //区域1步进
   Edit44->Text = IntToStr(parameterMacro[9]); //区域2步进
   Edit45->Text = IntToStr(parameterMacro[10]); //区域0最大角度误差
   Edit46->Text = IntToStr(parameterMacro[11]); //区域1最大角度误差
   Edit47->Text = IntToStr(parameterMacro[12]); //区域2最大角度误差
   Edit48->Text = IntToStr(parameterMacro[13]); //X方向最大误差
   Edit49->Text = IntToStr(parameterMacro[14]); //Y方向最大误差
   Edit50->Text = IntToStr(parameterMacro[15]); //最大旋转角度
//------------
//微观场景参数
//------------
   Edit51->Text = IntToStr(parameterMicro[0]); //中心点X坐标
   Edit52->Text = IntToStr(parameterMicro[1]); //中心点Y坐标
   Edit53->Text = IntToStr(parameterMicro[2]); //平台X向最大位移
   Edit54->Text = IntToStr(parameterMicro[3]); //平台Y向最大位移
   Edit55->Text = IntToStr(parameterMicro[4]); //平台X向最大允许误差
   Edit56->Text = IntToStr(parameterMicro[5]); //平台Y向最大允许误差

//
*/
}
//---------------------------------------------------------------------------
//打开并初始化串口 (9600,8,0,1)
void __fastcall TForm1::Button48Click(TObject *Sender)
{
  char *ComNo;
  DCB dcb;
  String Temp,Temp1;
  int i;

  Temp="COM1";
  ComNo=Temp.c_str();

  hComm=CreateFile(ComNo,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0);
  //judge open?
  if (hComm==INVALID_HANDLE_VALUE)
  {
   MessageBox(0,"Error of Opening Comm port!","Comm Error",MB_OK);
   return;
   }
  GetCommState(hComm,&dcb);
  dcb.BaudRate=CBR_9600;
  dcb.ByteSize=8;
  dcb.Parity=NOPARITY;
  dcb.StopBits=ONESTOPBIT;
//set comm port
  if (!SetCommState(hComm,&dcb))
   {
    MessageBox(0,"Error of setting Comm port!","Set Error",MB_OK);
    CloseHandle (hComm);
    return;
   }
//清除缓冲区
   PurgeComm(hComm,PURGE_RXCLEAR);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button49Click(TObject *Sender)
{
  CloseHandle (hComm);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  unsigned long lrc,BS;
  BS=4;
  DWORD nBytesRead,dwError,inack;
  COMSTAT cs;   //端口状态
  //定义接受参数数据缓冲
  short int accept_parameter[43];
  int i;
  int high8bit,low8bit;

  if (hComm==0) return;

  ClearCommError(hComm,&dwError,&cs); // 清除错误,检测状态
  if (cs.cbInQue < 86)   //接收86字节
     {
       MessageBox(0,"Bytes in Read Buffer less than parameters!","Read Buffer Error",MB_OK);
       Timer1->Interval = 0;
       return;
     }
  else
     {
      ReadFile(hComm,(char *)accept_parameter,86,&nBytesRead,NULL);
     }

//将数据读入参数数组中供显示修改,注意与发送要一致
//accept_parameter[0]无效
    for ( i = 0 ; i < 10 ; i++ )
        {
          parameterA[i] = accept_parameter[i+1];
          parameterB[i] = accept_parameter[i+11];
        }
    for ( i = 0 ; i < 16 ; i++ )
        {
          parameterMacro[i] =  accept_parameter[i+21];
        }
    for ( i = 0 ; i < 6 ; i++ )
        {
          parameterMicro[i] =  accept_parameter[i+37];
        }
//显示参数
//---------------
//行走电机(A)参数
//---------------
   Edit9->Text  = IntToStr(parameterA[0]); //电机A运行换相时间
   Edit10->Text = IntToStr(parameterA[1]); //电机A制动换相时间
   Edit11->Text = IntToStr(parameterA[2]); //电机A运行控制参数1
   Edit57->Text = IntToStr(parameterA[3]); //电机A制动控制参数1
//运行控制参数2
   high8bit = parameterA[4];
   low8bit  = parameterA[4];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit12->Text = IntToStr(high8bit);
   Edit13->Text = IntToStr(low8bit);
//制动控制参数2
   high8bit = parameterA[5];
   low8bit  = parameterA[5];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit18->Text = IntToStr(high8bit);
   Edit19->Text = IntToStr(low8bit);
//运行控制参数3
   high8bit = parameterA[6];
   low8bit  = parameterA[6];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit14->Text = IntToStr(high8bit);
   Edit15->Text = IntToStr(low8bit);
//制动控制参数3
   high8bit = parameterA[7];
   low8bit  = parameterA[7];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit20->Text = IntToStr(high8bit);
   Edit21->Text = IntToStr(low8bit);
//相位索引1
   Edit16->Text = IntToStr(parameterA[8]);
//相位索引2
   Edit17->Text = IntToStr(parameterA[9]);
//---------------
//转向电机(B)参数
//---------------
   Edit22->Text = IntToStr(parameterB[0]); //电机A运行换相时间
   Edit23->Text = IntToStr(parameterB[1]); //电机A制动换相时间
   Edit24->Text = IntToStr(parameterB[2]); //电机A运行控制参数
   Edit58->Text = IntToStr(parameterB[3]); //电机A制动控制参数
//运行控制参数2
   high8bit = parameterB[4];
   low8bit  = parameterB[4];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit25->Text = IntToStr(high8bit);
   Edit26->Text = IntToStr(low8bit);
//制动控制参数2
   high8bit = parameterB[5];
   low8bit  = parameterB[5];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit38->Text = IntToStr(high8bit);
   Edit39->Text = IntToStr(low8bit);
//运行控制参数3
   high8bit = parameterB[6];
   low8bit  = parameterB[6];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit34->Text = IntToStr(high8bit);
   Edit35->Text = IntToStr(low8bit);
//制动控制参数3
   high8bit = parameterB[7];
   low8bit  = parameterB[7];
   low8bit  = low8bit  & 0x00ff;
   high8bit = high8bit & 0xff00;
   high8bit = high8bit >> 8;
   Edit40->Text = IntToStr(high8bit);
   Edit41->Text = IntToStr(low8bit);
//相位索引1
   Edit36->Text = IntToStr(parameterB[8]);
//相位索引2
   Edit37->Text = IntToStr(parameterB[9]);
//------------
//宏观场景参数
//------------
   Edit27->Text = IntToStr(parameterMacro[0]); //目标X
   Edit28->Text = IntToStr(parameterMacro[1]); //目标Y
   Edit29->Text = IntToStr(parameterMacro[2]); //机器人最终角
   Edit30->Text = IntToStr(parameterMacro[3]); //区域半径0
   Edit31->Text = IntToStr(parameterMacro[4]); //区域半径1
   Edit32->Text = IntToStr(parameterMacro[5]); //区域半径2
   Edit33->Text = IntToStr(parameterMacro[6]); //区域半径3
   Edit42->Text = IntToStr(parameterMacro[7]); //区域0步进
   Edit43->Text = IntToStr(parameterMacro[8]); //区域1步进
   Edit44->Text = IntToStr(parameterMacro[9]); //区域2步进
   Edit45->Text = IntToStr(parameterMacro[10]); //区域0最大角度误差
   Edit46->Text = IntToStr(parameterMacro[11]); //区域1最大角度误差
   Edit47->Text = IntToStr(parameterMacro[12]); //区域2最大角度误差
   Edit48->Text = IntToStr(parameterMacro[13]); //X方向最大误差
   Edit49->Text = IntToStr(parameterMacro[14]); //Y方向最大误差
   Edit50->Text = IntToStr(parameterMacro[15]); //最大旋转角度
//------------
//微观场景参数
//------------
   Edit51->Text = IntToStr(parameterMicro[0]); //中心点X坐标
   Edit52->Text = IntToStr(parameterMicro[1]); //中心点Y坐标
   Edit53->Text = IntToStr(parameterMicro[2]); //平台X向最大位移
   Edit54->Text = IntToStr(parameterMicro[3]); //平台Y向最大位移
   Edit55->Text = IntToStr(parameterMicro[4]); //平台X向最大允许误差
   Edit56->Text = IntToStr(parameterMicro[5]); //平台Y向最大允许误差

   Timer1->Interval = 0;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Timer1->Enabled = false;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -