通过MSComm控件进行WINCC串口通讯总结

来源:本站
导读:目前正在解读《通过MSComm控件进行WINCC串口通讯总结》的相关信息,《通过MSComm控件进行WINCC串口通讯总结》是由用户自行发布的知识型内容!下面请观看由(电工学习网 - www.9pbb.com)用户发布《通过MSComm控件进行WINCC串口通讯总结》的详细说明。
目的:通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)
测试环境:操作系统 win7
WINCC版本:V7.2
辅助工具:串口调试工具
ASPD虚拟串口工具
测试WINCC组态画面:

通过MSComm控件进行WINCC串口通讯总结
测试实验过程画面:

通过MSComm控件进行WINCC串口通讯总结
1、通过VB实现串口通讯
画面对象“打开画面”VB事件脚本:
Sub OnOpen()
Dim objMSComm, tagConnection
Set objMSComm = hmiRuntime.Screens("串口通讯VB版").ScreenItems("COM")
Set tagConnection = HMIRuntime.Tags("tagConnection1")
If objMSComm.PortOpen = False Then
' Assign com port number
objMSComm.Commport = 4
' Values: 9600 Baud, N - No Parity, 8 - Databit, 1 - Stopbit
objMSComm.Settings = "9600,N,8,1"
objMSComm.RThreshold = 1
objMSComm.SThreshold = 0
objMSComm.InBufferCount = 0
objMSComm.InputLen = 0
objMSComm.PortOpen = True
tagConnection.Write (True)
HMIRuntime.Trace("Port open." & vbCrLf)
Else
HMIRuntime.Trace("Port is already opened." & vbCrLf)
End If
End Sub
MSComm控件OnComm对象事件:
Sub OnOpen()
Dim objMSComm, tagConnection
Set objMSComm = HMIRuntime.Screens("串口通讯VB版").ScreenItems("COM")
Set tagConnection = HMIRuntime.Tags("tagConnection1")
If objMSComm.PortOpen = False Then
' Assign com port number
objMSComm.Commport = 4
' Values: 9600 Baud, N - No Parity, 8 - Databit, 1 - Stopbit
objMSComm.Settings = "9600,N,8,1"
objMSComm.RThreshold = 1
objMSComm.SThreshold = 0
objMSComm.InBufferCount = 0
objMSComm.InputLen = 0
objMSComm.PortOpen = True
tagConnection.Write (True)
HMIRuntime.Trace("Port open." & vbCrLf)
Else
HMIRuntime.Trace("Port is already opened." & vbCrLf)
End If
End Sub
“SEND按钮”鼠标左键按下事件:
Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)
Dim objMSComm
Dim strTemp,buffer
HMIRuntime.Trace("Communication!" & vbCrLf)
Set objMsComm = HMIRuntime.Screens("串口通讯VB版").ScreenItems("COM")
Set buffer = HMIRuntime.Tags("Buffer1")
strTemp = buffer.Read
If objMSComm.PortOpen = True Then
If strTemp<>"" then
objMSComm.Output=strTemp
End If
End If
End Sub
2、通过C脚本实现串口通讯
画面对象“打开画面”C事件脚本:
#include "apdefap.h"
void OnOpenPicture(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)
{
#define GetObject GetObject
__object *pdl=NULL;
__object *pic=NULL;
__object *obj=NULL;
int i,j;
pdl = __object_create("PDLRuntime");
if(pdl)
{
printf("portopen get pdl ok ");
}
pic=pdl->GetPicture("串口通讯C版");
if(pic)
{
printf("portopen get pic ok ");
}
obj=pic->GetObject("COM");
if(obj)
{
printf("portopen get obj ok ");
}
if(obj->PortOpen==0)
{
obj->Commport = 3;
obj->Settings = "9600,N,8,1";
obj->RThreshold = 1;
obj->SThreshold = 0;
obj->InBufferCount = 0;
obj->InputLen = 0;
obj->PortOpen = 1;
}


__object_delete(obj);
__object_delete(pic);
__object_delete(pdl);
}
MSComm控件OnComm对象事件:
#include "apdefap.h"
void OnComm(char* lpszPictureName, char* lpszObjectName )
{
#define GetObject GetObject
__object *pdl=NULL;
__object *pic=NULL;
__object *obj=NULL;
char *data="";
pdl = __object_create("PDLRuntime");
if(pdl)
{
printf("portopen get pdl ok ");
}
pic=pdl->GetPicture("串口通讯C版");
if(pic)
{
printf("portopen get pic ok ");
}
obj=pic->GetObject("COM");
if(obj)
{
printf("portopen get obj ok ");
}
SetTagChar("BufferTemp",obj->Input);
printf("BufferTemp:%s ",GetTagChar("BufferTemp"));


if(strcmp(GetTagChar("BufferTemp"),""))
{
SetTagChar("Buffer1",GetTagChar("BufferTemp"));
}
__object_delete(obj);
__object_delete(pic);
__object_delete(pdl);
}
“SEND按钮”鼠标左键按下事件:
#include "apdefap.h"
void OnLButtonDown(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName, UINT nFlags, int x, int y)
{
#define GetObject GetObject
__object *pdl=NULL;
__object *pic=NULL;
__object *obj=NULL;
char *data="";
pdl = __object_create("PDLRuntime");
if(pdl)
{
printf("portopen get pdl ok ");
}
pic=pdl->GetPicture("串口通讯C版");
if(pic)
{
printf("portopen get pic ok ");
}
obj=pic->GetObject("COM");
if(obj)
{
printf("portopen get obj ok ");
}
if(strcmp(GetTagChar("Buffer1"),""))
{
obj->Output=GetTagChar("Buffer1");
}
__object_delete(obj);
__object_delete(pic);
__object_delete(pdl);
}

MSComm控件.rar

提醒:《通过MSComm控件进行WINCC串口通讯总结》最后刷新时间 2023-07-10 03:47:28,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《通过MSComm控件进行WINCC串口通讯总结》该内容的真实性请自行鉴别。