① 使用个人电脑EXCEL能够控制PLC吗
可以,Excel+VBA脚本,实现串口通讯,然后控制PLC。
EXCEL-VBA串口通讯实例
软件:EXCEL;硬件:艾默生EC10 PLC;功能:通过EXCEL中的按钮控制PLC的输出继电器Y0;
界面:RUN(运行通讯)、STOP(停止通讯)、Y0(Y0 ON/OFF控制)。
主程序:
OptionExplicit
PublicDeclareSubSleepLib"kernel32"(ByValdwMillisecondsAsLong)
"user32"(ByValhwndAsLong,ByValnIDEventAsLong,ByValuElapseAsLong,ByVallpTimerFuncAsLong)AsLong
"user32"(ByValhwndAsLong,ByValnIDEventAsLong)AsLong
Publiccom1AsNewMSCommLib.MSComm
Publicy0SttAsBoolean
Publicy0_onAsBoolean
PublictmrFlagAsBoolean
PublictmrAsLong
Subrunn()
OnErrorGoToed
com1.Settings="9600,e,8,1"
Ifcom1.PortOpen=FalseThen
com1.PortOpen=True
EndIf
tmr=SetTimer(0,0,500,AddressOfontimer)
ExitSub
ed:
MsgBox"串口打开错误!"
EndSub
Substopp()
Ifcom1.PortOpen=TrueThen
com1.PortOpen=False
KillTimer0,tmr
EndIf
EndSub
PublicFunctionontimer()
Dima(7)AsByte
DimaddAsLong
OnErrorGoToed
IftmrFlag=FalseThen
tmrFlag=True
Ify0_on=TrueThen
y0_on=False
Ify0Stt=TrueThen
a(0)=&H1
a(1)=&H5
a(2)=&H0
a(3)=&H0
a(4)=&HFF
a(5)=&H0
a(6)=&H8C
a(7)=&H3A
com1.Output=a
add=0
Do
DoEvents
Sleep10
add=add+1
Ifadd>=100Then
ExitDo
EndIf
LoopUntilcom1.InBufferCount>=8
Else
a(0)=&H1
a(1)=&H5
a(2)=&H0
a(3)=&H0
a(4)=&H0
a(5)=&H0
a(6)=&HCD
a(7)=&HCA
com1.Output=a
add=0
Do
DoEvents
Sleep10
add=add+1
Ifadd>=100Then
ExitDo
EndIf
LoopUntilcom1.InBufferCount>=8
EndIf
EndIf
EndIf
tmrFlag=False
ExitFunction
ed:MsgBox"串口错误!"
tmrFlag=False
EndFunction
界面程序:
PrivateSubcmd1_Click()
y0_on=True
y0Stt=Noty0Stt
EndSub
PrivateSubcmdRun_Click()
runn
CheckBox1.Value=1
EndSub
PrivateSubcmdStop_Click()
stopp
CheckBox1.Value=0
EndSub