📄 fbycom.cpp
字号:
if(fby_IsOpened)
{
Serial.Close();
fby_IsOpened=FALSE;
}
//单机模式
if( SingleMachine == 1)
{
if(!Serial.Open(fby_ComID,fby_Baud,0)) //首次打开端口设置成永为1
{
return FBY_FAIL; //不能打开串行口
}
}
else
//多机模式
{
if(!Serial.Open(fby_ComID,fby_Baud,3)) //首次打开端口设置成永为d8位为mark
{
return FBY_FAIL; //不能打开串行口
}
}
fby_IsOpened=TRUE;
return FBY_SUCCESS;
}
/*
判断命令并发送命令读取数据函数
*/
int SendCmd(int Command)
{
if(!fby_IsOpened)
return ERR_NOT_OPENED; //串口没有打开
memset( ReadData ,0 ,1007 );//初始化接收缓存
ClearComData();
SendData[0] = (unsigned char)fby_Address;//仪器地址
SendData[1] = 0x02;
CString tempstr;
int read_num;
switch( Command )
{
case 1://取状态
SendData[2] = GET_STATUS;
read_num = 7;
break;
case 2://取测量值
SendData[2] = GET_TEST_DATA;
read_num = 10;
break;
case 3://取稳态值
SendData[2] = GET_SAFE_DATA;
read_num = 8;
break;
case 4://取加速值
SendData[2] = GET_SPEEDUP_DATA;
read_num = 37;
break;
case 5://请求线性修正
SendData[2] = REVISE;
read_num = 4;
break;
case 6://请求转换到测量屏
SendData[2] = CHANGE_TO_TEST_PAGE;
read_num = 4;
break;
case 7://请求转换到稳态屏
SendData[2] = CHANGE_TO_SAFE_PAGE;
read_num = 4;
break;
case 8://请求转换到加速屏
SendData[2] = CHANGE_TO_SPEEDUP_PAGE;
read_num = 4;
break;
case 9://请求开始或停止稳态测量
SendData[2] = BEGIN_STOP_SAFE_TEST;
read_num = 4;
break;
case 10://请求进行一次加速触发(手动方式)
SendData[2] = SPEEDUP_TOUCH_OFF;
read_num = 4;
break;
case 11://请求清除加速数据
SendData[2] = CLEAR_SPEEDUP_DATA;
read_num = 4;
break;
case 12://请求切换加速方式(手动方式与自动方式间切换)
SendData[2] = CHANGE_SPEEDUP_MODE;
read_num = 4;
break;
case 13://请求取最后一次加速主要数据
SendData[2] = GET_LAST_MAIN_SPEEDUP_DATA;
read_num = 8;
break;
case 14://请求取最近一次加速曲线数据
SendData[2] = GET_LAST_SPEEDUP_LINE;
read_num = 1007;
break;
}
//如果是多机
if( SingleMachine == 0 )
SendData[3] = 256 - ( SendData [1] + SendData [2] ) % 256;
//如果是单机
else
SendData[3] = 256 - ( SingleMachineCode[0] +
SingleMachineCode[1] +
SingleMachineCode[2] +
SendData [0] +
SendData [1] +
SendData [2] ) % 256;
if(Read(read_num))
return FBY_SUCCESS;
return ERR_CMD_TIMEOUT;
}
int _stdcall FBY_GetStatus(unsigned char *Data_Array,int *ReturnDataNum)//取状态函数
{
if( !fby_IsOpened )
return ERR_NOT_OPENED;
int ReturnCode = 0;
ReturnCode = SendCmd( 1 );
if( ReturnCode == FBY_SUCCESS )//取状态成功
{
// CString s;
// s.Format("%d %d %d %d %d %d %d",ReadData[0],ReadData[1],ReadData[2],ReadData[3],ReadData[4],ReadData[5],ReadData[6]);
// AfxMessageBox(s);
for(int ii=0;ii<ReadNum;ii++)
Data_Array[ii] = ReadData[ii];
*ReturnDataNum = ReadNum;
ReadNum = 0;
return FBY_SUCCESS;
}
else
return ReturnCode;
}
int _stdcall FBY_SendCmd(int iCommand)
{
if( !fby_IsOpened )
return ERR_NOT_OPENED;
switch(iCommand)
{
case 1://取状态
case 2://取测量值
case 3://取稳态值
case 4://取加速值
return ERR_REF_ERR; //参数错误
break;
case 5://请求进行线性校正
case 6://请求转换到测量屏
case 7://请求转换到稳态屏
case 8://请求转换到加速屏
case 9://请求开始或停止稳态测量
case 10://请求进行一次加速触发(手动方式)
case 11://请求清除加速数据
case 12://请求切换加速方式(自动方式与手动方式间转换)
return (SendCmd( iCommand ));
case 13://请求取最近一次加速主要数据
case 14://请求取最近一次加速曲线数据
return ERR_REF_ERR; //参数错误
break;
default:
return ERR_REF_ERR;
break;
}
}
int _stdcall FBY_GetTestResult(unsigned char *Data_Array,int *ReturnDataNum)//转速 //取测量值
{
if( !fby_IsOpened )
return ERR_NOT_OPENED;
int ReturnCode = SendCmd( 2 );
if( ReturnCode == FBY_SUCCESS )//取状态成功
{
for(int ii=0;ii<ReadNum;ii++)
Data_Array[ii] = ReadData[ii];
*ReturnDataNum = ReadNum;
ReadNum = 0;
return FBY_SUCCESS;
}
else
return ReturnCode;
}
//设置通讯模式,单机模式还是多机模式
int _stdcall FBY_SetCommMode( int singlemachine ) //设置通讯的模式(单机模式还是多机模式)
{
if( singlemachine < 0 || singlemachine > 1)
return ERR_REF_ERR; //参数错误
if( SingleMachine == singlemachine ) //如果设置和原来的相等 则直接返回
return FBY_SUCCESS;
Serial.Close();
fby_IsOpened = FALSE;
SingleMachine = singlemachine ;
if(fby_IsOpened)
{
Serial.Close();
fby_IsOpened=FALSE;
}
if( SingleMachine == 1)
{
if(!Serial.Open(fby_ComID,fby_Baud,0)) //首次打开端口设置成永为1
{
return FBY_FAIL; //不能打开串行口
}
}
else
{
if(!Serial.Open(fby_ComID,fby_Baud,3)) //首次打开端口设置成永为1
{
return FBY_FAIL; //不能打开串行口
}
}
fby_IsOpened=TRUE;
return FBY_SUCCESS;
}
int _stdcall FBY_GetSafeResult(unsigned char *Data_Array,int *ReturnDataNum) //取稳态值
{
if( !fby_IsOpened )
return ERR_NOT_OPENED;
int ReturnCode = SendCmd( 3 );
if( ReturnCode == FBY_SUCCESS )//取状态成功
{
for(int ii=0;ii<ReadNum;ii++)
Data_Array[ii] = ReadData[ii];
*ReturnDataNum = ReadNum;
ReadNum = 0;
return FBY_SUCCESS;
}
else
return ReturnCode;
}
int _stdcall FBY_GetSpeedUpResult(unsigned char *Data_Array,int *ReturnDataNum) //16组数据按照时间由近至远顺序排列 //取加速值
{
if( !fby_IsOpened )
return ERR_NOT_OPENED;
CString s;
int ReturnCode = SendCmd( 4 );
if( ReturnCode == FBY_SUCCESS )//取状态成功
{
for(int ii=0;ii<ReadNum;ii++)
Data_Array[ii] = ReadData[ii];
*ReturnDataNum = ReadNum;
ReadNum = 0;
return FBY_SUCCESS;
}
else
return ReturnCode;
}
int _stdcall FBY_GetLastMainSpeedUpResult(unsigned char *Data_Array,int *ReturnDataNum) //转速 //取最近一次加速主要数据
{
if( !fby_IsOpened )
return ERR_NOT_OPENED;
int ReturnCode = SendCmd( 13 );
if( ReturnCode == FBY_SUCCESS )//取状态成功
{
for(int ii=0;ii<ReadNum;ii++)
Data_Array[ii] = ReadData[ii];
*ReturnDataNum = ReadNum;
ReadNum = 0;
return FBY_SUCCESS;
}
else
return ReturnCode;
}
int _stdcall FBY_GetLastSpeedUpLineData(unsigned char *Data_Array,int *ReturnDataNum) //500个点的曲线的K值 //取加速值
{
if( !fby_IsOpened )
return ERR_NOT_OPENED;
ReadNum = 0;
int ReturnCode = SendCmd( 14 );
if( ReturnCode == FBY_SUCCESS )//取状态成功
{
for(int ii=0;ii<ReadNum;ii++)
Data_Array[ii] = ReadData[ii];
*ReturnDataNum = ReadNum;
memset(ReadData,0,1007);
ReadNum = 0;
return FBY_SUCCESS;
}
else
return ReturnCode;
}
int CFBYComApp::ExitInstance()
{
Serial.Close();
fby_IsOpened=FALSE;
return CWinApp::ExitInstance();
}
void _stdcall FBY_Close()
{
Serial.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -