目的:通過mscomm控件實現(xiàn)wincc串口通訊(c腳本和vb腳本兩種方式)
測試環(huán)境:操作系統(tǒng) win7
wincc版本:v7.2
輔助工具:串口調(diào)試工具
aspd虛擬串口工具
測試wincc組態(tài)畫面:
測試實驗過程畫面:
1、通過vb實現(xiàn)串口通訊
畫面對象“打開畫面”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按鈕”鼠標(biāo)左鍵按下事件:
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腳本實現(xiàn)串口通訊
畫面對象“打開畫面”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按鈕”鼠標(biāo)左鍵按下事件:
#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(p