📄 carditfa.cpp
字号:
tempTx[2] = 0;
tempTx[3] = chCmdLen + 2;//长度
tempTx[4] = chCmdType;//0x03
tempTx[5] = chCmdLen;
for(unsigned char i = 0; i < chCmdLen; i ++ )
{
tempTx[6+i] = *CmdContent;
CmdContent ++;
}
}
int cmdsend//发送打包好的指令,等待同ID的响应
(
unsigned char * Send, //待发送帧
unsigned char * Response //响应帧
)
{
int lenRx;
int Rtn = 0;
unsigned char nRetry;
nRetry = MAXRETRYTIMES;
// hTimer = SetTimer(NULL,NULL,30,(TIMERPROC)OnTimerOutOut);
while((Rtn <= 0) && (nRetry > 0))
{
if(sClient)
send(sClient,(char *)Send,(Send[3]+4),0);
while(1)//只有在收不到响应超时才重试
{
lenRx = pBufSynCmdRecv->GetFrom(Response,BUFCMDSIZE);
if(lenRx > 0)
{
if((Response[0] = 0x04) && (Response[1] == Send[1]))
{//如果收到上行应答指令且帧ID对应
Rtn = 1;
break;
}
}else//超时引起的,所以重发
{
nRetry --;
break;
}
}
}
//KillTimer(NULL,hTimer);
return Rtn;
}
/****************** CtlReset()--系统复位 帧类型0x03 ***********************
0x01
**************************************************************/
int WINAPI CtlReset(unsigned char Reset)
{
if(!bBeRun)return -2;
unsigned char tempTx[BUFCMDSIZE];
if(WaitForSingleObject(hMutexSend,-1) == WAIT_TIMEOUT)
return -4;
cmdcompose(tempTx,0x03,FrameID,0x01,0x01, &Reset);
if(sClient)
send(sClient,(char *)tempTx,tempTx[3]+4,0);
ReleaseMutex(hMutexSend);
return 1;
}
//Return Value: 1 Success; 0 Fail; -2 Not Connect;-1 No Response; -3 Abnormal Error
int sendctl(unsigned char CmdType, unsigned char CmdLen, unsigned char * CmdContent)
{
if(!bBeRun)return -2;
unsigned char tempTx[BUFCMDSIZE], tempRx[BUFCMDSIZE];
int Rtn;
if(WaitForSingleObject(hMutexSend,-1) == WAIT_TIMEOUT)
return -4;
cmdcompose(tempTx,0x03,FrameID,CmdType,CmdLen,CmdContent);
if(cmdsend(tempTx,tempRx) > 0)//收到对应的响应
{
FrameID ++;
if((tempRx[4] == 0x30) &&
(memcmp((tempRx+6),(tempTx+4),tempTx[3]) == 0))//成功响应
Rtn = 1;//表示成功
else if(tempRx[4] == 0x31)//失败响应
{
// if(Error != NULL)
// *Error = tempRx[6];//失败原因
Rtn = 0;//表示失败
}else
Rtn = -3;//异常出错,收到不正确应答
}else
Rtn = -1;//表示无响应
ReleaseMutex(hMutexSend);
return Rtn;
}
/****************** CtlSimula()--模拟控制 ***********************
0x10
0:保留
1:开始发送
2:停止发送
3:停止发送且清除发送缓冲区
4:开始接收
5:停止接收
6:停止接收且清除接收缓冲区
7:开始发送和接收
8:停止发送和接收
9:停止发送和接收且清除发送缓冲区与接收缓冲区
**************************************************************/
int WINAPI CtlSimula(unsigned char Code)//, unsigned char * Error)
{
int rtn = sendctl(0x10,1,&Code);
if((Code = 6) && (rtn > 0))//清除缓冲区
pBufDataRecv->Clear();
return rtn;
}
/****************** CtlMonitor()--监测控制 ***********************
0x20
0:保留
1:监测SCC1 DCE
2:停止监测SCC1 DCE
3:停止监测SCC1(DCE)且清除接收缓冲区
4:监测SCC2 DTE
5:停止监测SCC2 DTE
6:停止监测SCC2(DTE)且清除接收缓冲区
7:同时监测SCC1和SCC2
8:停止监测SCC1和SCC2
9:停止监测SCC1和SCC2,且清除两个通道的缓冲区
10:切换成截断数据上传
11:切换成全部数据上传
**************************************************************/
int WINAPI CtlMonitor(unsigned char Code)//, unsigned char * Error)
{
return (sendctl(0x20,1,&Code));
}
/****************** CtlBert()--误码测试控制 ***********************
0x30
0:保留
1:开始测试误码
2:关闭误码通路且清除缓冲
3:关闭误码通路不清除缓冲
**************************************************************/
int WINAPI CtlBert(unsigned char Code)//, unsigned char * Error)
{
return (sendctl(0x30,1,&Code));
}
/****************** CtlBertPat()--设置误码测试图案 ***********************
0x31
0:2e6-1
1:2e7-1
2:2e9-1
3:2e11-1
4:2e15-1
5:2e20-1;o.153
6:2e20-1;QRSS
7:2e23-1
8:all 1(未用)
9:all 0(未用)
0x0A:alternate
0x0B:double alternate
0x0C:3 in 24
0x0D:1 in 16
0x0E:1 in 8
0x0F:1 in 4
**************************************************************/
int WINAPI CtlBertPat(unsigned char Code)//, unsigned char * Error)
{
return (sendctl(0x31,1,&Code));
}
/****************** CtlBertEBI()--设置误码测试强制插入 ***********************
0x32
0: 无误码
1: 10e-1
2: 10e-2
3: 10e-3
4: 10e-4
5: 10e-5
6: 10e-6
7: 10e-7
其他(>10):减10后表示强制插入的误码数
**************************************************************/
int WINAPI CtlBertEBI(unsigned char Code)//, unsigned char * Error)
{
return (sendctl(0x32,1,&Code));
}
/****************** CtlE1Test()--E1测试控制 ***********************
0x70
0:保留
1:开始E1参数测试
2:停止E1测试且清除缓冲
3:停止E1测试而不清除缓冲
**************************************************************/
int WINAPI CtlE1Test(unsigned char Code)//, unsigned char * Error)
{
return (sendctl(0x70,1,&Code));
}
/********查询帧*******(MSI管理)*****************************************
返回值
1 设置成功;-1 无响应; -2 未连接; -3 异常出错 -4 缓冲区溢出; -5 发送冲突
***************************************************************/
int sendquery
(
unsigned char CmdType, //命令字
unsigned char CmdLen, //命令长度
unsigned char * CmdContent, //命令内容
unsigned char *Value, //返回值
unsigned char ValueLen //返回值缓冲区长度
)
{
if(!bBeRun)return -2;//Not Connected
unsigned char tempTx[BUFCMDSIZE], tempRx[BUFCMDSIZE];
int Rtn;
if(WaitForSingleObject(hMutexSend,-1) == WAIT_TIMEOUT)
return -5;
cmdcompose(tempTx,0x03,FrameID,CmdType,CmdLen,CmdContent);
if(cmdsend(tempTx,tempRx) > 0) //收到对应的响应
{
FrameID ++;
if(tempRx[4] == CmdType) //请求和响应指令字对应相同
{
Rtn = 1;
if(tempRx[5] > ValueLen)
Rtn = -4; //缓冲区溢出
else
memcpy(Value,tempRx+6,tempRx[5]);
}
else
Rtn = -3; //异常出错
}else
Rtn = -1; //表示无响应
ReleaseMutex(hMutexSend);
return Rtn;
}
/******************CtlGetBaud()--获取波特率***************
0x40
**************************************************************/
int WINAPI CtlGetBaud(UINT *baud)
{
UINT temp = 0;
int Rtn = sendquery(0x40,4,(unsigned char *)&temp,(unsigned char *)baud,4);
if(Rtn > 0)
*baud = ntohl(*baud);
return Rtn;
}
/******************CtlGetItfaState()--获取接口状态***************
0x41
**************************************************************/
int WINAPI CtlGetItfaState(unsigned char *stat)
{
unsigned char temp = 0;
return (sendquery(0x41,1,&temp,stat,1));
}
/******************CtlGetAbn()--获取异常情况统计表***************
0x81 暂时还未定上传的结构及其长度
**************************************************************/
int WINAPI CtlGetAbn(unsigned char *Abn)
{
unsigned char temp = 0;
return (sendquery(0x81,1,&temp,Abn,1));
}
/******************CtlQSend()--获取异常情况统计表***************
0x80 Return:-3 查询的帧ID不正确,-2 未连接,-1 无应答, 0 发送失败 1 发送成功
**************************************************************/
int WINAPI CtlQSend(unsigned char chSendID,
UINT * SucNum,
UINT * FailNum,
UINT * TimeStamp,
unsigned char * Error
)
{
//test
//return 1;
if(!bBeRun)return -2;
if(chSendID != SendID)
return -3;
unsigned char tempTx[BUFCMDSIZE], tempRx[BUFCMDSIZE];
int Rtn;
if(WaitForSingleObject(hMutexSend,-1) == WAIT_TIMEOUT)
return -5;
cmdcompose(tempTx,0x03,FrameID,0x80,1,&SendID);
if(cmdsend(tempTx,tempRx) > 0) //收到对应的响应
{
FrameID ++;
if((tempRx[4] == 0x20) && (tempRx[5] == 12)) //最近发送成功
{
Rtn = 1;
}
else if((tempRx[4] == 0x21) && (tempRx[5] == 13))//最近一次发送失败
{
Rtn = 0;
if(Error != NULL)
*Error = tempRx[18];
}else
Rtn = -3; //异常出错
if(Rtn > 0)
{
*SucNum = ntohl( *((ULONG *)(tempRx + 6)) );
*FailNum = ntohl( *((ULONG *)(tempRx + 10)) );
*TimeStamp = ntohl( *((ULONG *)(tempRx + 14)) );
}
}else
Rtn = -1; //表示无响应
ReleaseMutex(hMutexSend);
return Rtn;
}
//********** MSI 系列指令**************************
int sendmsi
(
unsigned char CmdType,
unsigned char CmdLen,
unsigned char * CmdContent,
unsigned char *Value,
unsigned char ValueLen
)
{
return (sendquery(CmdType, CmdLen, CmdContent, Value, ValueLen));
}
/******************CtlMsiAdd()--添加2m测试组***************
0x50
**************************************************************/
int WINAPI CtlMsiAdd(ULONG Assemble1, ULONG Assemble2, unsigned char * pGroupID)
{
int Rtn;
struct
{
unsigned char GroupID;
ULONG lnArgu[2];
} strE1;
strE1.lnArgu[0] = htonl(Assemble1);
strE1.lnArgu[1] = htonl(Assemble2);
Rtn = sendmsi(0x50, 8, (unsigned char *)strE1.lnArgu,(unsigned char *)&strE1,9);
*pGroupID = strE1.GroupID;
return Rtn;
}
/******************CtlMsiDel()--删除/清空测试组***************
0x51 当传入id为0时,清除全部测试组 返回值为1时成功,0时失败,-1无相应...
**************************************************************/
int WINAPI CtlMsiDel(BYTE GroupID)//, unsigned char * Value)
{
unsigned char Value;
int Rtn = sendmsi(0x51, 1, &GroupID,&Value,1);
if(Rtn > 0)
return Value;
else
return Rtn;
}
/******************CtlMsiQuery()--***************
0x52 查询特定的测试组或者全部测试组(when 0 transfered in)
**************************************************************/
int WINAPI CtlMsiQuery(BYTE GroupID, unsigned char * Value, unsigned char ValueLen)
{
return (sendmsi(0x52, 1, &GroupID,Value, ValueLen));
}
/******************CtlMsiSet()--设置2M参数***************
0x53 设置2M参数
bit0:纯2M传输("1"有效);
//bit1:0时隙传数据("1"有效)(保留,常设为"0")
bit2:16时隙传数据("1"有效)
bit3:主/从方式("1"为主,"0"为从)
//bit7:时隙控制("1"860,"0"PLD)(保留,常设为"0")
**************************************************************/
int WINAPI CtlMsiSet(BYTE CtlCode)
{
unsigned char Value;
int Rtn = sendmsi(0x53, 1, &CtlCode,&Value,1);
if(Rtn > 0)
return Value;
else
return Rtn;
}
/***************** 读取异步报告 *************/
/********************************************/
int WINAPI AsyReportGet(unsigned char * AsyReport, unsigned char BufLen)
{
if(!bBeRun)return -2;
return (pBufAsyCmdRecv->GetFrom(AsyReport, BufLen));
}
/***************** 停止读取异步报告 *************/
/********************************************/
int WINAPI AsyReportUnlock()
{
if(!bBeRun)return -2;
return (pBufAsyCmdRecv->Unlock());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -