通过MSComm控件进行WINCC串口通讯一、通过C脚本实现串口通讯画面对象“打开画面”C事件脚本#include“apdefap.h”ViodOnOpenPicture(char*IpszPictureName,char*IpszObjectName,char*IpszPropertyName){#defineGetObjectGetObject__object*pdl=NULL;__object*pic=NULL;__object*obj=NULL;IntI,j;Pdl=__object_create(“PDLRuntime”);If(pdl){Printf(“portopengetpdlok”);}pic=pdl-GetPicture(“串口通讯c版”);if(pic){printf(“portopengetpicok”);}obj=pic-GetObject(“COM”);if(obj){printf(“portopengetobjok”);}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);}Mxcomm控件OnComm对象事件#include“apdefap.h”VoidOnComm(char*IpszPictureName,char*IpszobjectName){#defineGetObjectGetObject__object*pdl=NULL;__object*pic=NULL;__object*obj=NULL;char*data=“”;pdl=__object_create(“PDLRuntime”);if(pdl){Printf(“portopengetpdlok”);}pic=pdl-GetPicture(“串口通讯C版);if(pic){printf(“portopengetpicok”);}obj=pic-GetObject(“COM”);if(obj){printf(“portopengetobjok”);}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”VoidOnLBufferDown(char*IpszPictureName,char*IpszObjectName,char*IpszPropertyName,UINTnFlags,intx,inty){#defineGetObjectGetObject__object*pdl=NULL;__object*pic=NULL;__object*obj=NULL;char*data=“”;pdl=__object_create(“PDLRuntime”);if(pdl){printf(“portopengetpdlok”);}pic=pdl-GetPicture(“串口通讯C版”);if(pic){Printf(“portopengetpicok);}obj=pic-GetObject(“COM”);if(obj){printf(“portopengetobjok);}If(strcmp(GetTagChar(“Buffer1”),“”)){obj-Output=GetTagChar(“Buffer1”);}__object_delete(obj);__object_delete(pic);__object_delete(pdl);}二、通过VB实现串口通讯画面对象“打开画面”VB事件脚本SubOnOpen()DimobjMSComm,tagConnectionSetobjMSComm=hmiRuntime.Screens.ScreenITems(“Com”)SettagConnection=HMIRuntime.Tags(“tagconnection1”)IfobjMSComm.PortOpen=FalseThen‘AssigncomportnumberobjMSComm.CommPort=4‘Value:9600Baud,N-NoParity,8-Databit,1-StopbitobjMSComm.Settings=“9600,N’8’1”objMSComm.RThreshold=1objMSComm.SThreshold=0objMSComm.InBufferCount=0objMSComm.InputLen=0objMSComm.PortOpen=TuretagConnection.Write(Ture)HMIRuntime.Trace(“4#Portopen.”&vbCrLf)ElseHMIRuntime.Trace(“4#Portisalreadyopened.”&vbCrLf)EndIfEndSubMSComm控件OnComm对象事件SubOnOpen()DimobjMSComm,tagConnectionSetobjMSComm=HMIRuntime.Screens.ScreenItems(“COM”)SettagConnection=HMIRuntime.Tags(“tagConnection1”)IfobjMSComm.PortOpen=FalseThen‘AssigncomportnumberobjMSComm.CommPort=4‘Valus:9600Baud,N-NoParity,8-Databit,1-StopbitobjMSComm.Settings=“9600,N’8’1”objMSComm.RThreshold=1objMSComm.SThreshold=0objMSComm.InBufferCount=0objMSComm.InputLen=0objMSComm.PortOpen=TuretagConnection.Write(Ture)HMIRuntime.Trace(“Portopen.”&vbCrLf)ElseHMIRuntime.Trace(“Portisalreadyopened.”&vbCrLf)EndIfEndSubSEND按钮鼠标左键按下事件;SubOnLButtonDown(ByValItem,ByValFlags,ByValx,ByValy)DimobjMSCommDimstrTemp,bufferHMIRuntime.Trace(“Communication!”&vbCrLf)SetobjMSComm=HMIRtime.Screens.ScreensItems(“COM”)Setbuffer=HMIRuntime.Tags(“Buffer1”)StrTemp=buffer.ReadIfobjMSComm.PortOpen=TrueThenIfstrTemp“”thenobjMSComm.Output=strTempEndIfEndIfEndSub