打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

废话不多说,后面会放出完整程序和代码

工具下载: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();}


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
《C语言程序设计》第三版课后答案
再说STM32中使用Flash模拟EEPROM (amoBBS 阿莫电子论坛)
Modbus TCP/IP协议的C++语言实现(转帖) | 花花侠的IT世界
Modbus从机(服务器)通讯设计
程序员面试攻略 5.6面试例题:整数/字符串之间的转换
Modbus通讯协议学习
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服