Hi I;m entry programmer in company.
I;m making Modbus program. The problem is that how proram runs speedly.
When I compile my codes, the program runs slowly.
My modbus program is that first crc check first, second error check.
For these problem, I;m making packet check problem..
I don;t know how to make function in my codes.
Plz, give me and advice or hint ..
I cannot make packet check function my codes.
\\
void CSerialDlg::OnEnter()
{
CString pszText, pszTemp, pszTemp1;
int len = 0;
unsigned short hchar = 0,lchar = 0;
u16 mAddress = 0;
if(m_RqFlag==0)
{
memset(sendByte, 0, 100);
}
if ( lpComThread != NULL )
{
len = 0;
sendByte[ len++ ] = (BYTE)(mModeBus_PACKET.pDevice_ID);
sendByte[ len++ ] = (BYTE)(mModeBus_PACKET.pCommand);
sendByte[ len++ ] = (BYTE)(mModeBus_PACKET.pAddress.pValue16/256);
sendByte[ len++ ] = (BYTE)(mModeBus_PACKET.pAddress.pValue16%256);
sendByte[ len++ ] = (BYTE)(mModeBus_PACKET.pQuantity.pValue16/256);
sendByte[ len++ ] = (BYTE)(mModeBus_PACKET.pQuantity.pValue16%256);
unsigned short crcvalue = Get_CRC_Value( sendByte,len );
if ( crcvalue >0 )
{
sendByte[ len++ ] = (BYTE)(crcvalue / 256);
sendByte[ len++ ] = (BYTE)(crcvalue % 256);
}
else
{
AfxMessageBox(TEXT("CRC Error"));
return;
}
}
else
{
}
BOOL bltrue;
unsigned i;
OnClear();
if ( lpComThread->SendData_Command( (unsigned char*)sendByte,len ) )
{
if ( lpComThread->ReceiveData_Command() )
{
m_SendMsg.GetWindowText( pszTemp );
if ( pszTemp != "" ){ pszTemp += "\r\n";}
pszTemp1 += lpComThread->Get_ReceiveValue();
if ( pszTemp1.GetLength() > 60 ){ blNotCrc = TRUE;}
if ( !blNotCrc ) { bltrue = Get_Last_CRC_Data( pszTemp1 ); }
pszText = "";
CString str = "";
mModeBus_PACKET.mState = ModeBus_ID;
for (unsigned int n=0; n<lpComThread->Get_ReceiveValue_Data_Length(); n++)
{
mModeBus_PACKET.mRxData = lpComThread->Get_ReceiveValue_Data( n );
switch(mModeBus_PACKET.mState)
{
case ModeBus_ID:
if(mModeBus_PACKET.pDevice_ID == mModeBus_PACKET.mRxData)
{
mModeBus_PACKET.mState = ModeBus_COMM;
}
else
{
mModeBus_PACKET.mState = ModeBus_IDLE;
}
break;
case ModeBus_COMM:
if(mModeBus_PACKET.pCommand == mModeBus_PACKET.mRxData)
{
mModeBus_PACKET.mDataLength = 0;
switch(mModeBus_PACKET.pCommand)
{
case 0x03:
mModeBus_PACKET.mState = ModeBus_DATALENGTH;
break;
case 0x06:
case 0x10:
mModeBus_PACKET.mState = ModeBus_ADDRESS;
break;
default:
mModeBus_PACKET.mState = ModeBus_IDLE;
AfxMessageBox(TEXT("Error"));
break;
}
}
else if(mModeBus_PACKET.pCommand == (mModeBus_PACKET.mRxData | 0x80) )
{
mModeBus_PACKET.mState = ModeBus_ERR;
}
else
{
mModeBus_PACKET.mState = ModeBus_IDLE;
}
break;
case ModeBus_ADDRESS:
mModeBus_PACKET.mAddress.pValue08[1 - (mModeBus_PACKET.mDataLength++)] = mModeBus_PACKET.mRxData;
if(mModeBus_PACKET.mDataLength > 1)
{
mModeBus_PACKET.mDataLength = 0;
if(mModeBus_PACKET.pAddress.pValue16 == mModeBus_PACKET.mAddress.pValue16)
{
switch(mModeBus_PACKET.pCommand)
{
case 0x06:
mModeBus_PACKET.mState = ModeBus_DATA;
break;
case 0x10:
mModeBus_PACKET.mState = ModeBus_QUANTITY;
break;
default:
mModeBus_PACKET.mState = ModeBus_IDLE;
break;
}
}
}
break;
case ModeBus_QUANTITY:
mModeBus_PACKET.mQuantity.pValue08[1 - (mModeBus_PACKET.mDataLength++)] = mModeBus_PACKET.mRxData;
if(mModeBus_PACKET.mDataLength > 1)
{
mModeBus_PACKET.mDataLength = 0;
if(mModeBus_PACKET.pQuantity.pValue16 == mModeBus_PACKET.mQuantity.pValue16)
{
mModeBus_PACKET.mState = ModeBus_CRC;
}
}
break;
case ModeBus_DATALENGTH:
mModeBus_PACKET.pDataLength = mModeBus_PACKET.mRxData;
mModeBus_PACKET.mDataLength = 0;
mModeBus_PACKET.mState = ModeBus_DATA;
break;
case ModeBus_DATA:
mAddress = mModeBus_PACKET.pAddress.pValue16 + mModeBus_PACKET.mDataLength/2;
i = mModeBus_PACKET.mDataLength;
i %= 2;
if(i == 0)
{
mModeBus_PACKET.pData[mAddress].pValue08[1] = mModeBus_PACKET.mRxData;
}
else
{
mModeBus_PACKET.pData[mAddress].pValue08[0] = mModeBus_PACKET.mRxData;
}
int mAddress_Check;
mAddress_Check = mModeBus_PACKET.mDataLength;
mAddress_Check = mModeBus_PACKET.mDataLength;
mAddress_Check = mAddress_Check % 4;
if(mAddress_Check == 3)
{
long int display_Value1, dispaly_Value2;
unsigned int display_Address;
display_Address = mAddress;
display_Address -= 1;
display_Value1 = mModeBus_PACKET.pData[mAddress-1].pValue16;
dispaly_Value2 = mModeBus_PACKET.pData[mAddress].pValue16;
display_Value1 = (display_Value1 << 16) & 0xFFFF0000;
display_Value1 = display_Value1 + dispaly_Value2;
switch(display_Address)
{
case 100:
case 142:
str.Format("ModeBUS Data[ %d ~ %d ] = 0x%x\r\n",display_Address,mAddress ,display_Value1);
break;
default:
str.Format("ModeBUS Data[ %d ~ %d ] = %d\r\n",display_Address,mAddress ,display_Value1);
break;
}
pszText += str;
}
mModeBus_PACKET.mDataLength++;
if(mModeBus_PACKET.mDataLength >= mModeBus_PACKET.pDataLength)
{
mModeBus_PACKET.mDataLength = 0;
mModeBus_PACKET.mState = ModeBus_CRC;
}
break;
case ModeBus_ERR:
mModeBus_PACKET.pErr = mModeBus_PACKET.mRxData;
mModeBus_PACKET.mState = ModeBus_CRC;
str.Format("Mode Bus Communiction Err = 0x%x\r\n",mModeBus_PACKET.pErr);
pszText += str;
break;
case ModeBus_CRC:
mModeBus_PACKET.pCrc.pValue08[1 - (mModeBus_PACKET.mDataLength++)] = mModeBus_PACKET.mRxData;
if(mModeBus_PACKET.mDataLength > 1)
{
if(mModeBus_PACKET.mCRC.pValue16 == mModeBus_PACKET.pCrc.pValue16)
{
mModeBus_PACKET.pPacket_Sucess = TRUE;
}
mModeBus_PACKET.mState = ModeBus_IDLE;
}
case ModeBus_IDLE:
break;
}
}
pszText += " ";
pszTemp += pszText;
if ( !blNotCrc )
{
if ( bltrue ) pszTemp += " [CRC Correct Parity ]";
else pszTemp += " [CRC Check Error。]";
}
pszTemp.MakeUpper();
int tlen = m_SendMsg.GetWindowTextLength();
m_SendMsg.SetSel( tlen,tlen );
UINT uint = m_SendMsg.GetLineCount();
m_SendMsg.LineLength( uint+1 );
m_SendMsg.SetWindowText( pszTemp );
}
else
{
MessageBox( "Error reading buffer data ",TITLE,MB_ICONINFORMATION );
}
}
else
{
MessageBox( "Send data failed, please try again......",TITLE,MB_ICONINFORMATION );
}
}