Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

\\


// SerialDlg.cpp :   I want to add packet function code in these part

void CSerialDlg::OnEnter()
{
	CString pszText, pszTemp, pszTemp1;
	int len = 0;
	unsigned short hchar = 0,lchar = 0;
	u16	mAddress = 0;
		// m_RqFlag==0 이면 데이터의 추가 입력이 의미가 없으므로 청소한다.
	if(m_RqFlag==0)
	{
		memset(sendByte, 0, 100);
			//	KillTimer(11);
	}
	
	
	
		
	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;
//				str.Format("Total Recive Data Length = %d\r\n",lpComThread->Get_ReceiveValue_Data_Length());
//				pszText += str;
			
				for (unsigned int n=0; n<lpComThread->Get_ReceiveValue_Data_Length(); n++)			
				{
					mModeBus_PACKET.mRxData	= lpComThread->Get_ReceiveValue_Data( n );
//					str.Format("mRxData ID = 0x%x\r\n",mModeBus_PACKET.mRxData);
//					pszText += str;
					switch(mModeBus_PACKET.mState)
					{
					case	ModeBus_ID:
						if(mModeBus_PACKET.pDevice_ID == mModeBus_PACKET.mRxData)
						{
							mModeBus_PACKET.mState = ModeBus_COMM;
//							str.Format("Device ID = %d\r\n",mModeBus_PACKET.pDevice_ID);
//							pszText += str;
						}
						else
						{
							mModeBus_PACKET.mState = ModeBus_IDLE;
						}
						break;
					case	ModeBus_COMM:
						if(mModeBus_PACKET.pCommand == mModeBus_PACKET.mRxData)
						{
//							str.Format("Device Command = %d\r\n",mModeBus_PACKET.mRxData);
//							pszText += str;
							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;
//							str.Format("ModeBUS Data[Address = %d ]  =  0x%x\r\n",mAddress,mModeBus_PACKET.pData[mAddress].pValue16);
//							pszText += str;
						}
						else
						{
							mModeBus_PACKET.pData[mAddress].pValue08[0] = mModeBus_PACKET.mRxData;
						}
//						str.Format("mDataLength  =  %d, mRxData  =  0x%x\r\n",mModeBus_PACKET.mDataLength,mModeBus_PACKET.mRxData);
//						pszText += str;
						int	mAddress_Check;
						mAddress_Check = mModeBus_PACKET.mDataLength;
//						mAddress_Check = mAddress_Check % 2;
//						if( (mAddress_Check==0) && (i == 1) )
						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 );
		
		   }  //ReplaceSel
		   else
		   {
				MessageBox( "Error reading buffer data ",TITLE,MB_ICONINFORMATION );
			}
	}
	else
	{
			MessageBox( "Send data failed, please try again......",TITLE,MB_ICONINFORMATION );
	}
}
Posted
Updated 18-May-11 20:40pm
v5
Comments
Nithin Sundar 19-May-11 2:29am    
Please do not post such a huge code block. Only post the snippet where you think it is going wrong.

And never post your email unless you like spam.
Sun-Mi Kang 19-May-11 2:41am    
OK
Sergey Alexandrovich Kryukov 19-May-11 12:30pm    
Can you explain more exactly where is your problem?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900