您的位置:首页 > 理论基础 > 计算机网络

VC2010基于TCP/IP的Modbus传输实现

2013-03-19 20:05 423 查看
废话不多说,后面会放出完整程序和代码

工具下载:http://download.csdn.net/detail/stevenking55/5186848

工具源代码下载:

主要就是三个函数:

1.OnButtonjoin() 建立连接

void CClientDlg::OnButtonjoin()
{
// TODO: Add your control notification handler code here
UpdateData(true);
CString servername = m_server_addr; //获取服务器地址
int port = atoi(m_port); //获取端口

if(pMysocket != NULL)
{
//delete pMysocket;
pMysocket = NULL;
}
pMysocket = new CMysocket(this); //创建套接字对象
if (!pMysocket->Create()) //创建套接字
{
delete pMysocket;
pMysocket = NULL;
MessageBox("套接字创建失败.");
return;
}
if (!pMysocket->Connect(servername,port)) //连接服务器
{
MessageBox("连接服务器失败!");
return;
}
flag = 1;
//成功则显示已连接
CString str;
str.Format("Modbus TCP/IP 设备 %s 端口 %s 已连接!",m_server_addr,m_port);
int getcount = m_send.GetCount();
m_send.InsertString(m_send.GetCount(),str);

}
2.ReceiveData()接受函数

void CClientDlg::ReceiveData()
{
unsigned char ReceiveBuf[200];
//接收传来的数据
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++;
}
//将16进制数据添加到列表框中
//int getcount = m_receive.GetCount();
m_receive.InsertString(m_receive.GetCount(),str);

//十六进制转十进制输出
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() 发送函数

void CClientDlg::OnButtonsend()
{
UpdateData(true);
// TODO: Add your control notification handler code here
CString str,temp;
int i = 0;
unsigned char SendBuf[12] = {0};
//检查错误
if( pMysocket == NULL || flag == 0)
{
MessageBox("请先建立连接!");
return;
}
if (m_server_addr.IsEmpty()|m_port.IsEmpty())
{
MessageBox("请检查服务器地址及端口号!");
return;
}

//处理数据
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("命令字错误!");
break;
}

//生成Modbus格式数据
SendBuf[0] = m_1;//事务元标识符,高字节在前,低字节在后
SendBuf[1] = m_2;
SendBuf[2] = m_3;//协议标识符,高字节在前,低字节在后
SendBuf[3] = m_4;
SendBuf[4] = 0x00;//后续字节长度,高字节在前,低字节在后
SendBuf[5] = 0x06;
SendBuf[6] = m_7;//单元标识符
//SendBuf[7] = 0x03;//m_cmdword;//命令字
SendBuf[8] = m_data_addr_high;//数据起始地址,高字节在前,低字节在后
SendBuf[9] = m_data_addr_low;
SendBuf[10] = m_data_num_high;//数据长度,高字节在前,低字节在后
SendBuf[11] = m_data_num_low;

int num = pMysocket->Send(SendBuf,12);

//成功则显示发送的数据
i = 0;
while (i <= 11)
{
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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: