Professional Documents
Culture Documents
VC的Modbus TCP协议模型通信测试软件的实现
VC的Modbus TCP协议模型通信测试软件的实现
,
( 730050)
Modbus/TCP Modbus Modbus/TCP
Modbus/TCP winsock
Modbus/TCPwinsock/
1.
:Modbus/TCPEthernet/I P
Foundation Fieldbus HSEProfinetModbus/TCP
Modbus/TCP
TCP/IP
I/O PLC 1Modbus/TCP
Schneider Modbus TCP/IP
Modbus /RTU ASCII HTTP
XML Internet Modbus/TCP
I/O I/O
E-mail Modbus/TCP
Modbus/TCP
Modbus/TCP
2Modbus/TCP
1.1
Modbus
2 Modbus/TCP
2.2 Modbus/TCP
2 Modbus 256
(1)MBAP Header(Modbus 7 )
Transaction identifier, 2 Modbus
Modbus 1Protocol Indentifier, 2
Modbus Length, 2 Unit Identifier Data
Unit Identifier 1 Modbus
2Function code( 1 ) 16
PLC 04
06 16
3Data( 148)
3 Winsock Modbus/TCP
Modbus/TCP/
Visual C++MFCVisual C++
WinsockCAsyncSocketCSocketCAsyncSocketWinsock
CSocketSocketFileCSocket
Modbus/TCPMFCWindow Sockets
StdAfx.h#include <afxsock.h>MFC Winsock
InitInstance()Winsock
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
/CSocket
CClientsocket
ModbusTCP-502Modbus/TCP
3
1
VC
void CModbusDlg::OnClick()//
{ CClientsocket m_ClientSocket;//
bind(m_ClientSocket,sockadd,len);//502
listen(m_ClientSocket,5);//
while(m_connect)//m_connect
{
SOCKET c=accept(m_ClientSocket,sockadd,len);
AfxBeginThread(ClientThreadPorc,(LPVOID)c,
THREAD_PRIORITY_ABOVE_NORMAL);//
WaitForSingleObject(m_hEvent,INFINITE);//
}
closesocket(m_ClientSocket);
}
//
UINT ClientThreadProc(LPVOID lpParam)
{
SOCKET m_socket=(SOCKET)lpParam;
SetEvent(m_hEvent);//
int kk=1;
BYTE* curRecord,waitRecord;
BYTE* curK=recvbuf=(BYTE*)malloc(4096);
While(1)//Modbus
{
kk=recv(m_sock,curK,256,0);
curK+=kk;
if(kk==0)//TCP
{
Closesocket(m_socket);
return 0;
}
}
Modbus
}
WaitForSingleObject(m_hEvent,INFINITE)
m_hEventCreateEvent
SetEvent(m_hEvent)
2
4
5
CString m_port = 502;//
CString m_IP = _T();//IP
CString m_con = _T();//
CListBox m_list1;//
4
//
void CModubusDlg::OnRead()
//Modbus
}
//
void CModubusDlg::OnDelete()
{
if(m_list1.GetCount()==0)//
return;
else if(m_list1.GetCurSel()<0||m_list1.GetCurSel()>m_list1.GetCount())
m_list1.SetCurSel(0);
m_pRecordset->Delete(adAffectCurrent);//
m_pRecordset->Update();
int nCurSel=m_list1.GetCurSel();//
m_list1.DeleteString(nCurSel);
if(nCurSel==0&&(m_list1.GetCount()!=0))//
m_list1.SetCurSel(nCurSel);
else if(m_list1.GetCount()!=0)//
m_list1.SetCurSel(nCurSel-1);
//Modbus
}
5 Modbus/TCP
4
Modbus/TCP
Modbus/TCPVC
Modbus/TCP
Mobbus/TCP
[]
[1] ..2003(10)
[2] D E Comer,Douglas E TCP/IP /
M 1999
[3]Anthony Jones. Window M1999
[4] Winsock2 [ M ] 2003
[5]MODBUS.ORGMODBUS Application Protocol Specification.
Author introduction
HAO Xiao-hong, male, was born in GanSu Province in February 1960 . He is a Professor of Electrical
Engineering at lanzhou University of technology and also is the master director of Chinese
artificial intelligence academic association. The mainly studying direction involve the control
of the complicated system and the intelligence control theories and application.
Zu Shou-Yuanmale, was born in YangZhou
University of technology . The mainly studying direction is the power electronic and electric
drive,field-bus.
730050
E-mail: zhangy790608@mail2.lut.cn thomasff79@163.com
TP393.04
A()