📄 carditfa.cpp
字号:
*trunc = 0;
if((temp[12] & 0x20) == 0) // 分包
{
nPackLength = ((nFrameLength + DATABODY) <= size)?
(DATABODY):(size - nFrameLength);
nFrameLength += DATABODY;
if(nPackLength > 0)//nFrameLength 可能会 > size
{
memcpy(data, (temp + DATAHEAD), nPackLength);
data += nPackLength;
}
}else
{
//分割数据报的最后一包,或者完整一包
nPackLength = (temp[12] & 0x1F);
nPackLength = (nPackLength << 8) + temp[13];
nPackLength = (nPackLength < DATABODY)?nPackLength:DATABODY;
nFrameLength += nPackLength;
if((nFrameLength + nPackLength) > size)
nPackLength = size - nFrameLength;
if(nPackLength > 0)
{
memcpy(data, (temp + DATAHEAD), (nPackLength));
data += nPackLength;
}
break;
}
}
}
//更新其他参数
if( chanel != NULL )
*chanel = temp[4];//通道号
if( group != NULL )
*group = temp[5];//测试组号
if( abnormal != NULL )
*abnormal = temp[10];//异常帧标识
//时戳
if( pTime != NULL )
*pTime = ntohl( *((unsigned long *)(temp+6)) );
// if(nFrameLength >= 2)//去掉最后两个CRC校验字节
// nFrameLength -= 2;
return nFrameLength;
}
/************WriteCard()--写数据****************************
data: 要写入数据
Len : 写入数据字节数
type: 00 表示本帧的发送无需向PC确认,但是异步给出全部发送完的结果;
01 表示本帧发送完,需要返回一个ACK或者NAK帧同步确认通知PC;
10 表示在本连续发送过程中,每实际发送一次数据都异步返回发送成功响应包(在选择该参数时,发送间隔最小500ms,常用来测试延时),
11 表示本帧的发送完全无需做任何的确认。
***********************************************************/
BOOL WINAPI WriteCard
(unsigned char* Data, int DataLength,
UINT Times, UINT Interval, unsigned char Type,
unsigned char *pSendID, unsigned int * pSucNum, unsigned int * pFailNum,
unsigned int * pTimeStamp, unsigned char *pError)
{
if(!bBeRun)
return FALSE;
unsigned char temp[BUFDATASIZE];
int nSentLength = 0, nPackNum;
if(WaitForSingleObject(hMutexSend,-1) == WAIT_TIMEOUT)
return -4;
//----------组包头------------------------
temp[0] = 0x05;//Frame-Type : Data
temp[1] = FrameID;
//发送次数
*((ULONG *)(temp+4)) = htonl(Times);
//发送间隔
*((ULONG *)(temp+8)) = htonl(Interval);
//Ack
switch(Type)
{
case 0x00:
temp[12] &= 0x3F;//0011 1111
break;
case 0x11:
temp[12] |= 0xC0;//1100 0000
break;
case 0x01:
temp[12] &= 0x7F;//0111 1111
temp[12] |= 0x40;//0100 0000
break;
case 0x10://每发送一次都返回
if(Interval < 500)
return FALSE;
temp[12] |= 0x80;
temp[12] &= 0xBF;
break;
};
//----------组包---发送-------------------------------
nPackNum = (DataLength / DATABODY) + 1;
while(nPackNum > 1)//处理分包
{
temp[2] = (BUFDATASIZE - 4) >> 8;//总帧长
temp[3] = (BUFDATASIZE - 4) & 0xFF;
temp[12] &= 0xDF;//**0* ****//未完标志
temp[12] &= 0xE0;
temp[12] |= (0x1F & (nPackNum >> 8));//***@ @@@@//包数
temp[13] = nPackNum & 0xFF;
nPackNum --;
//nSentLength += DATABODY;
memcpy((temp+14),Data,DATABODY);
DataLength -= DATABODY;
if(sClient)
send(sClient,(char *) temp, BUFDATASIZE,0);
}
//处理最后一帧
temp[2] = (DATAHEAD - 4 + (DataLength)) >> 8;//总帧长
temp[3] = (DATAHEAD - 4 + (DataLength)) & 0xFF;
temp[12] |= 0x20;//**1* ****//未完标志
temp[12] &= 0xE0;
temp[12] |= (0x1F & (DataLength >> 8));
temp[13] = DataLength & 0xFF;
// nPackNum = 0;
memcpy((temp+14),Data,DataLength);
if(sClient)
send(sClient,(char *) temp, (DATAHEAD+DataLength),0);
//-----------等待返回值---------------------------
/*
00 表示本帧的发送无需向PC确认,但是异步给出全部发送完的结果;
01 表示本帧发送完,需要返回一个ACK或者NAK帧同步确认通知PC;
10 表示在本连续发送过程中,每实际发送一次数据都异步返回发送成功响应包(在选择该参数时,发送间隔最小500ms,常用来测试延时),
11 表示本帧的发送完全无需做任何的确认。*/
int trynum = 0;
if(Type == 0x01)
{
UINT uElapse = 200 + Interval*Times;
unsigned char cmdlen;
// hTimer = SetTimer(NULL,NULL,uElapse,(TIMERPROC)OnTimerOutOut);
do
{
cmdlen = pBufSynCmdRecv->GetFrom(temp,BUFCMDSIZE);
trynum ++;
}while((cmdlen == 0) && (trynum < MAXRETRYTIMES));
// KillTimer(NULL,hTimer);
if(cmdlen == 0)
{
ReleaseMutex(hMutexSend);
return FALSE;
}
if((temp[0] == 0x04) && (temp[1] == FrameID)
&& ((temp[4] == 0x20) || (temp[4] == 0x21)) )
{
if( pSucNum != NULL )
(*pSucNum) = ntohl(*((unsigned long *)(temp + 5)));
if( pFailNum != NULL )
(*pFailNum) = ntohl(*((unsigned long *)(temp + 9)));
if( pTimeStamp != NULL )
(*pTimeStamp) = ntohl(*((unsigned long *)(temp + 13)));
if( pError != NULL )
{
if(temp[4] == 0x20)//成功
*pError = 0;
else
*pError = temp[17];
}
}
}
//保存发送ID,以备异步响应
SendID = FrameID;
if( pSendID != NULL )
*pSendID = SendID;
FrameID ++;
ReleaseMutex(hMutexSend);
return TRUE;
}
//----------------------------------------------------------------------------------------------------
//--------参数设置系列--------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
/************sendconfig()--发送配置请求************************************************************
传入参数:CmdType 参数字, CmdLen 参数长度, CmdContent参数内容头指针, Error错误变量存放地址
返回值:-2,未连接 -1,参数设置无响应 0,设置失败 1,设置成功
Error(当设置失败时): 0 请求参数不合法, 1 参数设置失败
*************************************************************************/
int sendconfig(unsigned char CmdType, unsigned char CmdLen, unsigned char * CmdContent, unsigned char * Error)
{
if(!bBeRun)return -2;
unsigned char tempTx[BUFCMDSIZE];
int lenTx = CmdLen + 6;
if(WaitForSingleObject(hMutexSend,-1) == WAIT_TIMEOUT)
return -4;
//组包头
tempTx[0] = 0x02;
tempTx[1] = FrameID;
tempTx[2] = 0;
tempTx[3] = CmdLen + 2;//长度
tempTx[4] = CmdType;//0x03
tempTx[5] = CmdLen;
for(unsigned char i = 0; i < CmdLen; i ++)
{
tempTx[6+i] = *CmdContent;
CmdContent ++;
}
unsigned char tempRx[BUFCMDSIZE];
int lenRx;
int Rtn = -1;
unsigned char nRetry;
nRetry = MAXRETRYTIMES;
// hTimer = SetTimer(NULL,NULL,1000,(TIMERPROC)OnTimerOutOut);
while((Rtn < 0) && (nRetry > 0))
{
if(sClient)
send(sClient,(char *)tempTx,lenTx,0);
while(1)
{
lenRx = pBufSynCmdRecv->GetFrom(tempRx,BUFCMDSIZE);
if(lenRx > 0)
{
if((tempRx[0] = 0x04) && (tempRx[1] == FrameID))
{
if((tempRx[4] == 0x10) &&
(memcmp((tempRx+6),(tempTx+4),(lenTx-4)) == 0))//成功响应
Rtn = 1;//表示成功
else if(tempRx[4] == 0x11)
{
Rtn = 0;//表示失败
if(Error != NULL)
*Error = tempRx[6];//失败原因
}else
Rtn = -2;
FrameID ++;
break;
}
}else//超时引起的,所以重发
{
nRetry --;
break;
}
}
}
//::KillTimer(NULL,hTimer);
ReleaseMutex(hMutexSend);
return Rtn;
}
/************SetAppMode()--设置误码率测试或者模拟DCECTE******************
0x01 appmode: 要设置的模式 1:模拟 2:监测 3:误码
***********************************************************/
int WINAPI SetAppMode(unsigned char appmode,unsigned char* Error)
{
return (sendconfig(0x01, 0x01, &appmode, Error));
}
/************SetProtocol()--设置协议类型******************
0x02 protocol: 要写入的协议类型
***********************************************************/
int WINAPI SetProtocol(unsigned char protocol, unsigned char * Error)
{
return (sendconfig(0x02, 0x01, &protocol, Error));
}
/************SetInterface()--设置接口类型******************
0x03 interfacetype: 要写入的接口类型 返回值:-1,参数设置无响应 0,设置失败 1,设置成功
***********************************************************/
int WINAPI SetInterface(unsigned char interfacetype, unsigned char * Error)
{
return (sendconfig(0x03, 0x01, &interfacetype, Error));
}
/************SetSpeed()--设置波特率******************
0x04 speed:速率
***********************************************************/
int WINAPI SetSpeed(UINT speed, unsigned char * Error)
{
speed = htonl(speed);
return (sendconfig(0x04, 0x04, (unsigned char *)&speed, Error));
}
/*******************SetItfa()--设置接口状态*******************
0x05 设置参数向下
**************************************************************/
int WINAPI SetItfaState(unsigned char status,unsigned char * Error)
{
return (sendconfig(0x05, 0x01, &status, Error));
}
/************SetDteDce()--设置模拟DTE方式******************
0x11 DteDce:0 DTE, 1 DCE
*******************************************************/
int WINAPI SetDteDce(unsigned char DteDce, unsigned char * Error)
{
return (sendconfig(0x11, 0x01, &DteDce, Error));
}
/************SetMode()--设置运行模式****************
0x12 mode: 要写入的运行模式 0 正常, 1 回环, 2 反射
***********************************************************/
int WINAPI SetMode(unsigned char Mode, unsigned char * Error)
{
return (sendconfig(0x12, 0x01, &Mode, Error));
}
/************SetMonitor()--选择监测选择的时钟****************
0x21 clock: 0:TC提供DCE时钟, 1:TTC提供DTE时钟
***********************************************************/
int WINAPI SetMonitor(unsigned char clock, unsigned char * Error)
{
return (sendconfig(0x21, 0x01, &clock, Error));
}
/************SetLineSel()--选择监测线路****************
0x22 0:双向,1:监测DTE方向,2:监测DCE方向,3:都不监测
********************************************************/
int WINAPI SetLineSel(unsigned char Line, unsigned char *Error)
{
return (sendconfig(0x22, 0x01, &Line, Error));
}
/*********************** 与HDLC协议相关 *********************/
/************SetClock()--设置时钟类型******************
0x61 同步HDLC&BISYNC clock:时钟方式 0:默认,1:内时钟,2:外时钟
***********************************************************/
int WINAPI SetClock(unsigned char clock, unsigned char * Error)
{
return (sendconfig(0x61, 0x01, &clock, Error));
}
/*****SetCode()--设置编码类型*******************************
codetype: 要写入的编码方式
***********************************************************/
/************SetFLAGShare()--设置帧标志共享******
0x62 flag: 1: 共享使能,0: 共享废除
***********************************************************/
int WINAPI SetFLAGShare(unsigned char flag, unsigned char * Error)
{
return (sendconfig(0x62, 0x01, &flag, Error));
}
/************SetIntSignal()--设置帧标志共享******
0x63 signal: 0:帧间隔传送全‘1’,1:传送帧标志FLAG
***********************************************************/
int WINAPI SetIntSignal(unsigned char signal, unsigned char * Error)
{
return (sendconfig(0x63, 0x01, &signal, Error));
}
/************SetEncodeFormat()--设置码格式(电平方式)******
0x64 format: 0:正常,1:NRZI
***********************************************************/
int WINAPI SetEncodeFormat(unsigned char format, unsigned char * Error)
{
return (sendconfig(0x64, 0x01, &format, Error));
}
/************************************************************/
/*********************** 与UART、SYNC协议相关 *********************/
/************SetBitsLen()--设置数据位长******
0x71 bitslen:数据位长 6-8
***********************************************************/
int WINAPI SetBitsLen(unsigned char bitslen, unsigned char * Error)
{
return (sendconfig(0x71, 0x01, &bitslen, Error));
}
/************SetStopBits()--设置停止位长******
0x72 len:停在位长 1,2
***********************************************************/
int WINAPI SetStopBits(unsigned char len, unsigned char * Error)
{
return (sendconfig(0x72, 0x01, &len, Error));
}
/************SetParity()--设置校验位长******
0x73 type: 0-None,1-ODD,2-EVEN
***********************************************************/
int WINAPI SetParity(unsigned char type, unsigned char * Error)
{
return (sendconfig(0x73, 0x01, &type, Error));
}
/***************SetSync(BYTE sync1,BYTE sync2)--设置同步码****
0x81 sync1 :同步码1, sync2 :同步码2
**************************************************************/
int WINAPI SetSync(BYTE sync1,BYTE sync2, unsigned char *Error)
{
BYTE sync[2];
sync[0] = sync1;
sync[1] = sync2;
return (sendconfig(0x81, 0x02, sync, Error));
}
/******************SetMaxLen()--获取接口状态***************
0x91 最大接收长度
**************************************************************/
int WINAPI SetMaxLen(UINT length, unsigned char * Error)
{
length = htonl(length);
return (sendconfig(0x91, 0x04, (BYTE *)&length, Error));
}
//----------------------------------------------------------------------
//--------控制指令-----------------------------------------------------
//--------------------------------------------------------------------
void cmdcompose
(
unsigned char * tempTx,
unsigned char chFrameType,
unsigned char chFrameID,
unsigned char chCmdType,
unsigned char chCmdLen,
unsigned char * CmdContent
)
{
//组包头
tempTx[0] = chFrameType;
tempTx[1] = chFrameID;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -