QQ登录

只需一步,快速开始

通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)

[ 复制链接 ]
本帖最后由 baikhgmv 于 2016-9-6 12:41 编辑

通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)

测试环境:操作系统  win7

          WINCC版本:V7.2

辅助工具:串口调试工具

               ASPD虚拟串口工具

测试WINCC组态画面: 测试实验过程画面:

通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)

通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)

通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)

通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)

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



百度云附件:MSComm控件.zip   


回复

使用道具 举报

大神点评(1)

baikhgmv 楼主 2016-9-6 12:39:37 显示全部楼层
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);
}
回复

使用道具 举报

快速回复 返回列表 客服中心 搜索