No more nonsense, the whole program and code will be released later.
Tool Download: http://download.csdn.net/detail/stevenking55/5186848
Tool source code Download:
It's basically three functions:
1.OnButtonjoin () Establishing a connection
void Cclientdlg::onbuttonjoin ()
{
//Todo:add your control notification handler code here
UpdateData (True) ;
CString servername = m_server_addr; Get the server address
int port = atoi (M_port);//Get Port
if (pmysocket! = NULL)
{
//delete pmysocket;
Pmysocket = NULL;
}
Pmysocket = new Cmysocket (this); Create socket Object
if (!pmysocket->create ())//Create socket
{
delete pmysocket;
Pmysocket = NULL;
MessageBox ("Socket creation failed.");
return;
}
if (!pmysocket->connect (Servername,port))//Connect server
{
MessageBox ("Connection server failed!");
return;
}
flag = 1;
Success shows the connected
CString str;
Str. Format ("Modbus TCP/IP device%s port%s is connected. ", m_server_addr,m_port);
int getcount = M_send. GetCount ();
M_send. Insertstring (m_send. GetCount (), str);
}
2.ReceiveData () Accept function
void Cclientdlg::receivedata ()
{
unsigned char receivebuf[200];
Received data from
int receive_data_num = pmysocket->receive (receivebuf,200);
int i=0,j=0;
CString str,str1,temp;
while (I < receive_data_num)
{
temp. Format ("%02x", Receivebuf[i]);
if (receivebuf[i] = = 0xff)
j = i;
str = str + temp + ";
i++;
}
Add 16 binary data to the list box
//int getcount = m_receive. GetCount ();
M_receive. Insertstring (m_receive. GetCount (), str);
hexadecimal to decimal output
j = j + 3;
while (J < Receive_data_num)
{
int temp_num = (int) (RECEIVEBUF[J]<<8);
Temp_num = temp_num + (int) receivebuf[j+1];
Temp. Format ("%d", temp_num);
STR1 = str1 + temp + ";
j = j + 2;
}
int getcount1 = m_receive. GetCount ();
M_receive. Insertstring (m_receive. GetCount (), str1);
}
3.OnButtonsend () Send function
void Cclientdlg::onbuttonsend () {UpdateData (true);
Todo:add your control notification handler code here CString str,temp;
int i = 0;
unsigned char sendbuf[12] = {0};
Check for error if (Pmysocket = = NULL | | flag = = 0) {MessageBox ("Please make a connection first!");
Return } if (m_server_addr. IsEmpty () |m_port.
IsEmpty ()) {MessageBox ("Please check server address and port number!");
Return
}//Processing data M_DATA_ADDR = ((m_data_addr-1) & 0xFFFF);
M_data_addr_low = (unsigned char) ((M_DATA_ADDR) & 0X00FF);
M_data_addr_high = (unsigned char) (((M_DATA_ADDR) >>8) & 0X00FF);
M_data_num = (M_data_num & 0xFFFF);
M_data_num_low = (unsigned char) ((m_data_num) & 0X00FF);
M_data_num_high = (unsigned char) (((M_data_num) >>8) & 0X00FF); i = M_cmdword.
GetCurSel ();
switch (i) {case 0:sendbuf[7] = 0x01;
Break
Case 1:sendbuf[7] = 0x02;
Break
Case 2:sendbuf[7] = 0x03;
Break
Case 3:sendbuf[7] = 0x04;
Break
Case 4:sendbuf[7] = 0x05;
Break
Case 5:sendbuf[7] = 0x06; BReak;
Case 6:sendbuf[7] = 0x0f;
Break
Case 7:sendbuf[7] = 0x10;
Break
Case 8:sendbuf[7] = 0x14;
Break
Case 9:sendbuf[7] = 0x15;
Break
Case 10:sendbuf[7] = 0x16;
Break
Case 11:sendbuf[7] = 0x17;
Break
Case 12:sendbuf[7] = 0x2B;
Break Default:messagebox ("Command word error.
");
Break
}//Generate Modbus Format data sendbuf[0] = m_1;//transaction meta identifier, high byte in front, low byte at rear sendbuf[1] = m_2;
SENDBUF[2] = m_3;//protocol identifier, high byte in front, low byte at rear sendbuf[3] = M_4;
SENDBUF[4] = 0x00;//Subsequent byte length, high byte in front, low byte at rear sendbuf[5] = 0x06; SENDBUF[6] = m_7;//cell identifier//sendbuf[7] = 0x03;//m_cmdword;//command word sendbuf[8] = m_data_addr_high;//data start address, high byte in front, low byte behind sendb
UF[9] = M_data_addr_low;
SENDBUF[10] = m_data_num_high;//data length, high byte in front, low byte at rear sendbuf[11] = M_data_num_low;
int num = pmysocket->send (sendbuf,12);
Success shows the sent data i = 0; while (i <=) {temp.
Format ("%02x", Sendbuf[i]);
str = str + temp + ";
i++; } int getcount = M_send.
GetCount (); M_send. Insertstring (m_send.
GetCount (), str); M_receiVe.
SetWindowText (""); M_receive.
SetFocus (); }