测试环境:操作系统 win7
WINCC版本:V7.2
辅助工具:串口调试工具
ASPD虚拟串口工具
测试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