📄 modbustooldlg.cpp
字号:
// ModBusToolDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ModBusTool.h"
#include "ModBusToolDlg.h"
#include <assert.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CModBusToolDlg dialog
CModBusToolDlg::CModBusToolDlg(CWnd* pParent /*=NULL*/)
: CDialog(CModBusToolDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CModBusToolDlg)
m_EditData = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_SizeFlag = FALSE;
lpNewComThread = NULL;
byPort = 1;
dwBaud = 9600;
uiTerm = 1;
uiInterval = 200;
blMode = TRUE;
blNotCrc = FALSE;
intRX = intTX = 0;
dwSetValue = 0;
}
void CModBusToolDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CModBusToolDlg)
DDX_Text(pDX, IDC_EDIT1, m_EditData);
DDV_MinMaxDWord(pDX, m_EditData, 0, 150000);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CModBusToolDlg, CDialog)
//{{AFX_MSG_MAP(CModBusToolDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_ENTER, OnEnter)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_WM_SIZE()
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_CBN_SELCHANGE(IDC_COMBO_COMMAND, OnSelchangeComboCommand)
ON_CBN_SELCHANGE(IDC_COMBO_COMPORT, OnSelchangeComboComport)
ON_CBN_SELCHANGE(IDC_COMBO_BAUD, OnSelchangeComboBaud)
ON_CBN_SELCHANGE(IDC_COMBO_MACHINE, OnSelchangeComboMachine)
ON_CBN_SELCHANGE(IDC_COMBO_INTERVAL, OnSelchangeComboInterval)
ON_BN_CLICKED(IDC_CHECK_MODE, OnCheckMode)
ON_BN_CLICKED(IDC_CHECK_NOTCRC, OnCheckNotcrc)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_CHECK_AUTORECEIVE, OnCheckAutoreceive)
ON_BN_CLICKED(IDC_BUTSETDATATO, OnButsetdatato)
ON_BN_CLICKED(IDC_BUT_COMPORT, OnButComport)
ON_BN_CLICKED(IDC_BUT_CONVENT, OnButConvent)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModBusToolDlg message handlers
BOOL CModBusToolDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
//-----------------------------------------------------
//---------draw enter button and exit button bitmap
m_bmpButton[0].SubclassDlgItem( IDC_ENTER,this );
m_bmpButton[0].SetIcon( IDI_ICON_ENTER );
m_bmpButton[0].SetFlat( TRUE );
m_bmpButton[1].SubclassDlgItem( IDC_EXIT,this );
m_bmpButton[1].SetIcon( IDI_ICON_EXIT1 );
m_bmpButton[1].SetFlat( TRUE );
m_bmpButton[2].SubclassDlgItem( IDC_CLEAR,this );
m_bmpButton[2].SetIcon( IDI_ICON_EXIT );
m_bmpButton[2].SetFlat( TRUE );
m_Button[0] = (CButton *)GetDlgItem( IDC_ENTER );
m_Button[1] = (CButton *)GetDlgItem( IDC_EXIT );
m_Button[2] = (CButton *)GetDlgItem( IDC_CLEAR );
m_CheckCrc = (CButton *)GetDlgItem( IDC_CHECK_CRC );
m_CheckCrc->SetCheck( TRUE );
m_CheckNotCrc = (CButton *)GetDlgItem( IDC_CHECK_NOTCRC );
m_CheckNotCrc->SetCheck( TRUE );
m_CheckMode = (CButton *)GetDlgItem( IDC_CHECK_MODE );
m_CheckMode->SetCheck( TRUE );
m_CheckAutoReceive = (CButton *)GetDlgItem( IDC_CHECK_AUTORECEIVE );
m_butSetDataTo = (CButton *)GetDlgItem( IDC_BUTSETDATATO );
m_butPort = (CButton *)GetDlgItem( IDC_BUT_COMPORT );
m_editData = (CEdit *)GetDlgItem( IDC_EDIT1 );
m_Status = (CStatic *)GetDlgItem( IDC_STATIC_STATUS );
m_Edit[0] = (CEdit *)GetDlgItem( IDC_EDIT_SEND );
m_Edit[1] = (CEdit *)GetDlgItem( IDC_EDIT_RECEIVE );
//m_Edit[1] = (CRichEditCtrl*)GetDlgItem( IDC_RICHEDIT1 );
m_Line = (CStatic *)GetDlgItem( IDC_STATIC_LINE );
m_SizeFlag = TRUE;
SetWindowText(TITLE);
Initailize_Control();
Change_Communication_Param();
//-----------------------------------------------------
return TRUE;
}
void CModBusToolDlg::OnPaint()
{
CPaintDC dc(this);
if (IsIconic())
{
// device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
Initailize_Window( &dc );
CDialog::OnPaint();
}
}
HCURSOR CModBusToolDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CModBusToolDlg::OnOK()
{
}
void CModBusToolDlg::OnCancel()
{
}
void CModBusToolDlg::OnClose()
{
EndDialog( FALSE );
}
BOOL CModBusToolDlg::Send_TextData( CString pszstring )
{
CString pszTemp1;
char *szbuff = NULL ;
szbuff = new char[ 1024 ];
memset( szbuff,0,sizeof(szbuff) );
strcpy( szbuff,(LPCTSTR)pszstring );
int len = pszstring.GetLength();
if ( lpNewComThread != NULL )
{
if ( lpNewComThread->SendData_Command( (unsigned char*)szbuff,len ) )
{
intTX += len;
delete []szbuff;
if ( lpNewComThread->ReceiveData_Command() )
{
CString pszTemp = lpNewComThread->Get_ReceiveValue();
m_Edit[1]->GetWindowText( pszTemp1 );
if ( pszTemp1 != "" )
pszTemp1 += "\r\n";
pszTemp1 += pszTemp;
m_Edit[1]->SetWindowText( pszTemp1 );
Change_Work_Status();
}
else
return ( FALSE );
}
else
{
delete []szbuff;
return ( FALSE );
}
}
return ( TRUE );
}
void CModBusToolDlg::OnEnter()
{
CString pszText,pszTemp,pszTemp1;
int len = 0;
unsigned short hchar = 0,lchar = 0;
m_Edit[0]->GetWindowText( pszText );
if ( pszText == "" )
{
MessageBox( "请输入或选择要发送的内容。",TITLE,MB_ICONINFORMATION );
m_Edit[0]->SetFocus();
return;
}
if ( !blMode )
{
Send_TextData( pszText );
return;
}
len = Str2Hex( pszText,(unsigned char*)sendByte );
if ( lpNewComThread != NULL )
{
//-----mask crc codeing------------------------------------------
if ( m_CheckCrc->GetCheck() )
{
unsigned short crcvalue = Get_CRC_Value( sendByte, len );
if ( crcvalue >0 )
{
sendByte[ len++ ] = (BYTE)(crcvalue / 256);
sendByte[ len++ ] = (BYTE)(crcvalue % 256);
CString pszA,pszB;
intTX += len;
for ( int n=0; n<len; n++ )
{
if ( sendByte[n] <= 0x0F )
pszA.Format( "0%X ",sendByte[n] );
else
pszA.Format( "%X ",sendByte[n] );
pszB += pszA;
}
pszB.MakeUpper();
m_Edit[0]->SetWindowText( pszB );
Change_Work_Status();
}
//}
}
else
{
/*if ( len != 8 )
{
MessageBox( "输入的数据位数有误(16个十六进制数)。请更正......",TITLE,MB_ICONINFORMATION );
m_Edit[0]->SetFocus();
return ;
}*/
}
BOOL bltrue;
if ( lpNewComThread->SendData_Command( (unsigned char*)sendByte,len ) )
{
if ( lpNewComThread->ReceiveData_Command() )
{
m_Edit[1]->GetWindowText( pszTemp );
if ( pszTemp != "" )
pszTemp += "\r\n";
pszTemp1 += lpNewComThread->Get_ReceiveValue();
if ( pszTemp1.GetLength() > 60 )
blNotCrc = TRUE;
if ( !blNotCrc )
bltrue = Get_Last_CRC_Data( pszTemp1 );
pszText = "";
for ( int n=0; n<pszTemp1.GetLength(); n+=2 )
{
pszText += pszTemp1.GetAt( n );
pszText += pszTemp1.GetAt( n+1 );
pszText += " ";
}
pszTemp += pszText;
if ( !blNotCrc )
{
if ( bltrue )
pszTemp += " [CRC校验正确。]";
else
pszTemp += " [CRC校验错误。]";
}
pszTemp.MakeUpper();
int tlen = m_Edit[1]->GetWindowTextLength();
m_Edit[1]->SetSel( tlen,tlen );
UINT uint = m_Edit[1]->GetLineCount();
m_Edit[1]->LineLength( uint+1 );
m_Edit[1]->SetWindowText( pszTemp );
} //ReplaceSel
else
MessageBox( "读缓冲区数据发生错误。",TITLE,MB_ICONINFORMATION );
}
else
MessageBox( "数据发送失败,请重试......",TITLE,MB_ICONINFORMATION );
}
}
BOOL CModBusToolDlg::Get_Last_CRC_Data( CString pszData )
{
if ( pszData == "" )
return ( FALSE );
BYTE nByte[100];
::Make_Info( nByte,pszData );
DWORD newcrc = ::Get_CRC_Value( nByte,(pszData.GetLength()-4)/2 );
CString pszcrc = pszData.Right( 4 );
DWORD h,l,lchar,hchar,oldcrc;
h = l = hchar = lchar = oldcrc = 0;
char *lastcrc = 0;
lastcrc = pszcrc.GetBuffer(4);
h = *(lastcrc++);
h = h > 0x39 ? h-0x37 : h-0x30;
l = *(lastcrc++);
l = l > 0x39 ? l-0x37 : l-0x30;
hchar = h * 16 + l;
h = *(lastcrc++);
h = h > 0x39 ? h-0x37 : h-0x30;
l = *(lastcrc++);
l = l > 0x39 ? l-0x37 : l-0x30;
lchar = h * 16 + l;
oldcrc = hchar * 256 + lchar;
if ( oldcrc == newcrc )
return ( TRUE );
else
return ( FALSE );
}
CString CModBusToolDlg::Get_CRC_Data()
{
CString str;
return str;
}
int CModBusToolDlg::Str2Hex(CString str, unsigned char* data)
{
//----HH 2006-2-14-------------------------------------------
//将字符串整理成16进制格式,用 data 参数送出,并返回数据长度
// HL HL HL HL HL HL HL HL HL HL HL HL HL
//-----------------------------------------------------------
int t,t1;
int rlen = 0,len = str.GetLength(); //get total lenght
for ( int i=0; i<len; )
{
char l, h = str[ i ];
if(h==' ')
{
i++;
continue;
}
i++;
if( i >= len )
break;
l = str[ i ];
t = HexChar( h );
t1 = HexChar( l );
if ( (t==16) || (t1==16) )
break;
else
t = t * 16 + t1;
i++;
data[rlen] = (unsigned char)t;
rlen++;
}
return rlen;
}
char CModBusToolDlg::HexChar(char c)
{
if ( (c>='0') && (c<='9') )
return c-0x30;
else if ( (c>='A') && (c<='F') )
return c-'A'+10;
else if ( (c>='a') && (c<='f') )
return c-'a'+10;
else
return 0x10;
}
void CModBusToolDlg::Receive_Data()
{
}
void CModBusToolDlg::OnExit()
{
OnClose();
}
void CModBusToolDlg::Initailize_Window( CDC *pDc )
{
//--------Draw left back bitmap----------------
//--------HH 2006-02-05
m_Picture = (CStatic *)GetDlgItem( IDC_PICTURE );
CRect rect;
CBitmap cbmp;
BITMAP bmp;
CDC pMemDc;
m_Picture->GetClientRect( &rect );
pMemDc.CreateCompatibleDC( pDc );
cbmp.LoadBitmap( IDB_BITMAP1 );
cbmp.GetObject( sizeof(bmp),&bmp );
pMemDc.SelectObject( &cbmp );
pDc->StretchBlt( 2,3,rect.Width(),rect.Height(),&pMemDc,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY );
ReleaseDC( pDc);
ReleaseDC( &pMemDc );
cbmp.DeleteObject();
}
void CModBusToolDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if ( m_SizeFlag )
{
CRect rect,rect1;
GetClientRect( &rect );
m_Picture->MoveWindow( 2,3,105,rect.bottom-24,FALSE);
m_Button[0]->MoveWindow( rect.right-100,rect.top+10,90,22 );
m_Button[1]->MoveWindow( rect.right-100,rect.top+33,90,22 );
m_Button[2]->MoveWindow( rect.right-100,rect.top+59,90,22 );
m_Status->MoveWindow( 1,rect.Height()-20,rect.Width()-1,20 );
m_Edit[0]->GetWindowRect( &rect1 );
ScreenToClient( rect1 );
m_Edit[0]->MoveWindow(rect1.left,rect1.top,rect.Width()-rect1.left-5,rect1.Height() );
m_Edit[1]->GetWindowRect( &rect1 );
ScreenToClient( rect1 );
m_Edit[1]->MoveWindow(rect1.left,rect1.top,rect.Width()-rect1.left-5,rect.Height()-235 );
m_Line->GetWindowRect( &rect1 );
ScreenToClient( rect1 );
m_Line->MoveWindow(rect1.left,rect1.top,rect.Width()-rect1.left-5,2 );
m_Command->GetWindowRect( &rect1 );
ScreenToClient( rect1 );
m_Command->MoveWindow(rect1.left,rect1.top,rect.Width()-rect1.left-10,20 );
Invalidate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -