好的代码,发送包大于200后分成几次发送
CP5200API int CALLING_CONV CP5200_RS232_SendInstantMessage1( BYTE nCardID, BYTE byPlayTimes , int x , int y , int cx , int cy , int nFontSize , byte byColorAlign , int nEffect , BYTE nSpeed , BYTE byStayTime ,const char* pText )
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
int nRet = 0;
int nDataLen = (int)(strlen(pText)) + 1 + 22 ;
BYTE *pData = (BYTE*)malloc(nDataLen);
if ( pData != NULL )
{
nDataLen = CP5200_MakeInstantMessageData1( pData , nDataLen , byPlayTimes , x,y,cx,cy,nFontSize , byColorAlign,nEffect,nSpeed,byStayTime,pText);
if ( nDataLen > 0)
{
int nOpen = 0;
if(CP5200_RS232_IsOpened())
nOpen = 2;
else if(CP5200_RS232_Open())
nOpen = 1;
if ( nOpen )
{
HOBJECT hObj= CP5200_CommData_Create(COMMDATA_TYPE_RS232, nCardID, GetIDCode());
int nOffset = 0;
int nRemainLen = nDataLen;
long lDataOffset = 0;
while(nRemainLen>0) //将制作出来的数据封包发送,200个字节一包
{
BYTE bySendBuf[512] = {0};
int nLen = 0;
BYTE byLastPacket = 0;
if ( nRemainLen > 200 )
{
nLen = 200;
nRemainLen = nRemainLen-200;
byLastPacket = 0;
}
else
{
nLen = nRemainLen;
nRemainLen = 0;
byLastPacket = 1;
}
int nSendLen = CP5200_MakeSendInstantMessageData(hObj, bySendBuf, sizeof(bySendBuf) , pData + nOffset , nLen , byLastPacket , lDataOffset );
gRs232.WriteData(bySendBuf, nSendLen);
BYTE byRec[512] = {0};
int nRecLen = gRs232.ReceiveData(byRec, sizeof(byRec));
if(nRecLen>0)
{
BYTE byBuf[12] = {0};
CP5200_ParseSendInstantMessageRet(hObj, byRec, nRecLen, byBuf, sizeof(byBuf));
if( byBuf[0] != 0) //成功
{
lDataOffset = byBuf[1] + (byBuf[2]<<8) + (byBuf[3]<<16) + (byBuf[4]<<24);
if ( byLastPacket == 1)
nRet = 1;
}
}
nOffset += nLen;
Sleep(10); //每发送一包休眠10毫秒
}
if(nOpen == 1)
{
gRs232.Close();
}
CP5200_CommData_Destroy(hObj);
}
}
free(pData);
}
return nRet;
}