TwinCAT PLC和上位机HMI通讯初探
来源:互联网 发布:网络支付审理风险 编辑:程序博客网 时间:2024/05/17 09:38
在PLC的程序中,添加如下变量
intCount AT %Q*: DINT; 可读
intVarIn AT %I* : DINT:=0; 可写
PLCVar : DINT:=0; 可读可写
上位机HMI程序(参考InfoSys中的例子)
1.声明变量:
//===========================
long nErr, nPort;
AmsAddr Addr;
PAmsAddr pAddr;
ULONG lHdlVarR,lHdlVarW;
int nData;
CString strTmp;
ULONG hNotification;
AdsNotificationAttrib adsNotificationAttrib;
//===========================
2.初始化变量和属性
//========================================================
pAddr = &Addr;
nData = 0;
// Open communication port for local PLC (Run-time system 1)
nPort = AdsPortOpen();
nErr = AdsGetLocalAddress(pAddr); //获取本地的AmsNetId 和 Port
if (nErr)
{
MessageBox("Error: AdsGetLocalAddress: ");
return 0;
}
pAddr->port = AMSPORT_R0_PLC_RTS1; //Port为第一个PLC任务
// Specify attributes of the notification
adsNotificationAttrib.cbLength = 16; //读取数据长度
adsNotificationAttrib.nTransMode = ADSTRANS_SERVERONCHA; //当变化是响应回调函数
adsNotificationAttrib.nMaxDelay = 1000000; // 100ms
adsNotificationAttrib.nCycleTime = 1000000; // 100ms
3.获取变量的句柄
// Fetch handle for an <szVar> PLC variable
char szVarR[]= {"MAIN.intCount"}; //读
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0,
sizeof(lHdlVarR), &lHdlVarR, sizeof(szVarR), szVarR);
char szVarW[]= {"MAIN.intVarIn"}; //写
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0,
sizeof(lHdlVarW), &lHdlVarW, sizeof(szVarW), szVarW);
4.通过上述的句柄就可以实现对PLC中的数据进行读写 或 设置“事件驱动”的方式获取数据
4.1读数据
nErr = AdsSyncReadReq( pAddr, ADSIGRP_SYM_VALBYHND, lHdlVarR, sizeof(nData), &nData );
if (nErr)
MessageBox("Fehler: AdsSyncReadReq: ");
else
strTmp.Format("%d", nData) ;
m_strSysData="Fehler: AdsSyncReadReq: " + strTmp;
4.2写数据
UpdateData();
nData=atoi(m_strWriteData);
nErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVarW, sizeof(nData), &nData);
if (nErr)
MessageBox("Error: AdsSyncWriteReq: ");
4.3启动“事件驱动”
1)声明回调函数
void __stdcall SymbolChanged(AmsAddr*, AdsNotificationHeader*, unsigned long);
2)启动“事件驱动”
nErr = AdsSyncAddDeviceNotificationReq(pAddr, ADSIGRP_SYM_VALBYHND,
lHdlVarR, &adsNotificationAttrib,
SymbolChanged, NULL, &hNotification);
if (nErr)
{
MessageBox("Error: AdsSyncAddDeviceNotificationReq: ");
return;
}
//================以下为回调函数部分========================
void __stdcall SymbolChanged(AmsAddr* pAddr, AdsNotificationHeader* pNotification, ULONG hUser)
{
CString strDataFromNotify,strTmp;
CEx02Dlg *dlg=(CEx02Dlg *)AfxGetMainWnd();
strDataFromNotify=""; strTmp="";
// Output value of the variable
strTmp.Format("Value:%d/n", *(int *)pNotification->data); //data中的数据为地址
strDataFromNotify+=strTmp;
strTmp.Format("Notification:%d/n",pNotification->hNotification);
strDataFromNotify+=strTmp;
strTmp.Format("SampleSize:%d/n",pNotification->cbSampleSize);
strDataFromNotify+=strTmp;
strTmp.Format("hUser:%d/n",hUser);
strDataFromNotify+=strTmp;
dlg->m_strNtyData=strDataFromNotify;
dlg->SetDlgItemText(IDC_STATIC_Data2,strDataFromNotify); //更新CEX02类中的窗体
}
4.4关闭“事件驱动”
nErr = AdsSyncDelDeviceNotificationReq(pAddr, hNotification);
if (nErr)
{
MessageBox("Error: AdsSyncDelDeviceNotificationReq: ");
return;
}
5.关闭端口
nErr = AdsPortClose();
if (nErr)
MessageBox("Error: AdsPortClose: ");
以上是对int型变量进行读写的简单实验。
- TwinCAT PLC和上位机HMI通讯初探
- 基于VC++和MFC的上位机与PLC的通讯系统
- MX Component 上位机与三菱PLC通讯
- 初探PLC远程通讯技术
- S7 PLC与 上位机之间可以采用TCP/IP,Socket通讯吗?
- 基于OPC 技术的上位机与S7-200 系列PLC通讯的实现
- 上位机与PLC通讯及OPC数据采集实践一
- TwinCAT PLC 使用
- PLC上位机监控系统(摘要)
- C#上位机开发连接三菱PLC
- 上位机+三菱PLC+TCP+SLMP
- 上位机与三菱PLC通信
- 倍福TwinCAT3上位机与PLC通信测试(ADS通信) 包含C#和C++代码
- PLC上位软件开发
- 话自动控制系统中容易被遗忘的PLC与上位之间的通讯工具
- 各种PLC通讯介质和协议介绍
- 西门子S7-200PLC上位机通信 计算机 工控
- 上位机操作三菱PLC指令规范 转载
- 参与esri用户大会感想
- 如何获得汉字的unicode编码
- 找在一个表中存在而另一个表中不存在的记录
- ffmpeg视频格式转换分析
- 想起两句古话
- TwinCAT PLC和上位机HMI通讯初探
- 苍鹰-GISRen漫话GIS
- 圣经摘抄
- volatile关键字的说明以及测试
- FastReport心得(一)
- sql编程中求两个日期之间相差天数准确写法
- 控制PHP的输出:缓存并压缩动态页面
- 在 SQL Server 中使用 Transact-SQL如何遍历一个结果集
- WebSphere MQ字符集问题