① 使用個人電腦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