学习VB与PLC通讯有什么意义?
答:可以大大节省企业的成本!
当控制系统要求上位机与PLC的通讯量很大时,触摸屏已经不能胜任了,这时候VB就派上用场了!
用VB在电脑上编制一套PLC通讯程序,一台电脑花费很少的费用就能搞定上位机与PLC的通讯。到时候一定可以得到公司领导的赏识,得到重用!
所以,学习的意义在这里也就得到了充分的体现!当你拥有其他人所不会的技术或者技能时,你的优越性就体现出来了。所谓技多不压身,平时还是要多多学习,好好提升自己!
好了,下面转入今天的正题。如何实现VB与PLC进行开发呢?
通过一个实际程序来帮助大家学习。
VB与PLC开发界面
01左中括号实现源码左中括号'定义变量Dim setadOut As String, DevDatOut As String'程序初始化Private Sub Form_Load() '列出PLC端口输入输出全部地址 For g = 0 To 7 ListInAddr.AddItem g ListOutAddr.AddItem g Next g For h = 10 To 17 ListInAddr.AddItem h ListOutAddr.AddItem h Next h ListInAddr.ListIndex = 0 ListOutAddr.ListIndex = 0 MSComm1.CommPort = 1 '通信口 MSComm1.Settings = "9600,E,7,1" '串口参数设置 MSComm1.Handshaking = 0 '握手信号 MSComm1.InputLen = 0 '设置和返回input每次读出的字节数,设为0时读出接收缓冲区中的内容 MSComm1.OutBufferCount = 0 '设置和返回发送缓冲区的字节数,设为0时清空发送缓冲区 MSComm1.InBufferCount = 0 '设置和返回接收缓冲区的字节数,设为0时清空接收缓冲区 MSComm1.PortOpen = True '打开串口 InAlarm.FillColor = QBColor(10) '输入信号指示灯,初始绿色 OutAlarm.FillColor = QBColor(10) '输出信号指示灯,初始绿色End Sub'回路测试Private Sub cmdtest_Click() Dim Tim As Single MSComm1.InBufferCount = 0 '清空接收缓冲区 MSComm1.OutBufferCount = 0'清空发送缓冲区 MSComm1.Output = Chr(5) Tim = Timer '返回一个 Single,代表从午夜开始到现在经过的秒数DoIf Timer > Tim + 1Then MsgBox "与PLC没有连接!": Exit SubLoopUntil MSComm1.InBufferCount = 1IfLeft$(MSComm1.Input, 1) = Chr(6) Then MsgBox "与PLC通讯正常!", , "与PLC通讯检测"Else MsgBox "与PLC通讯不正常!", 48, "与PLC通讯检测"EndIfEnd Sub'置位:置指定地址端口为ON,即打开指示灯Private Sub Cmdset_Click() Call diziq If CStr(Val(setadOut)) <> setadOut Then Exit Sub '数字区包括了字母 MSComm1.OutBufferCount = 0 MSComm1.InBufferCount = 0 DevDatOut = "7" + DevDatOutFG: MSComm1.Output = Chr(2) + DevDatOut + SumChk(DevDatOut) Tim = TimerDoIf Timer > Tim + 1Then: ExitDoLoopUntil MSComm1.InBufferCount = 1If MSComm1.Input = Chr(6) Then MSComm1.InBufferCount = 0ElseIf MsgBox("置位不成功", vbRetryCancel + vbCritical) = vbCancel ThenExit SubIf MsgBox("置位不成功", vbRetryCancel + vbCritical) = vbRetry ThenGoTo FGEndIf OutAlarm.FillColor = QBColor(12)End Sub'复位:置指定地址端口为OFF,即关闭指示灯Private Sub Cmdreset_Click() Call diziq If CStr(Val(setadOut)) <> setadOut Then Exit Sub '数字区包括了字母 MSComm1.OutBufferCount = 0 MSComm1.InBufferCount = 0 DevDatOut = "8" + DevDatOutFG: MSComm1.Output = Chr(2) + DevDatOut + SumChk(DevDatOut) Tim = TimerIf MSComm1.Input = Chr(6) Then MSComm1.InBufferCount = 0ElseIf MsgBox("复位不成功", vbRetryCancel + vbCritical) = vbCancel ThenExit SubIf MsgBox("复位不成功", vbRetryCancel + vbCritical) = vbRetry ThenGoTo FGEndIf OutAlarm.FillColor = QBColor(10)End Sub'周期检测输出端口状态Private Sub Timer1_Timer() Call In_forEnd SubPrivate Sub In_for() Dim awe, awe1, weishu Dim BN8, BN7, BN6, BN5, BN4, BN3, BN2, BN1 As Integer Dim devadd As String, setin As String, setad As String Dim weishu1 As String, setad1 As String setad = ListInAddr If CStr(Val(setad)) <> setad Then Exit Sub '数字区包括了字母If (setad Mod10) < 4Then'断定是低四位还是高四位 weishu = 0 Else weishu = 1 End If awe1 = setad Mod 10 If Oct(Val("&o" + setad)) <> setad Then '判断是不是八进制。Exit SubEndIf setad1 = Val(Str(setad \ 10)) devadd = "0" + "008" + Hex("&o" + setad1) + "02" + Chr(3) MSComm1.InBufferCount = 0 MSComm1.OutBufferCount = 0 MSComm1.Output = Chr(2) + devadd + SumChk(devadd) Tim = TimerDoIf Timer > Tim + 1Then: ExitDoLoopUntil MSComm1.InBufferCount = 20 setin = MSComm1.Input weishu1 = Val("&H" + Mid(setin, 2, 2)) awe = dec2bin(weishu1) BN8 = Mid(awe, 1, 1) BN7 = Mid(awe, 2, 1) BN6 = Mid(awe, 3, 1)SelectCase awe1Case0'awe1断定元件号的位数,如是0位或4位 If BN1 = 1 Then biaozi = True Else biaozi = False End If Case 1 If BN2 = 1 Then biaozi = True Else biaozi = False End If Case 2 If BN3 = 1 Then biaozi = True Else biaozi = False End If Case 3 If BN4 = 1 Then biaozi = True Else biaozi = False End If Case 4 'awe1断定元件号的位数,如是0位或4位If BN5 = 1Then biaozi = TrueElse biaozi = FalseEndIfCase5If BN6 = 1Then biaozi = TrueElse biaozi = FalseEndIfCase6If BN7 = 1Then biaozi = TrueElse biaozi = FalseEndIfCase7If BN8 = 1Then biaozi = TrueElse biaozi = FalseEndIfEndSelectIf biaozi = TrueThen InAlarm.FillColor = QBColor(12)Else InAlarm.FillColor = QBColor(10)EndIfEnd Sub'转换成二进制Private Function dec2bin(Dats$) As String Dim bin8, bin4, bin2, bin1, bin16, bin32, bin64, bin128 If Dats \ 128 >= 1 Then bin128 = 1 Else bin128 = 0 End If If (Dats Mod 128) \ 64 >= 1 Then bin64 = 1 Else bin64 = 0 End If If (Dats Mod 64) \ 32 >= 1 Then 'Mod用来对两个数作除法并且只返回余数 bin32 = 1Else bin32 = 0EndIfIf (Dats Mod32) \ 16 >= 1Then bin16 = 1Else bin16 = 0EndIfIf (Dats Mod16) \ 8 >= 1Then'\ 运算符用来对两个数作除法并返回一个整数 bin8 = 1 Else bin8 = 0 End If If (Dats Mod 8) \ 4 >= 1 Then 'Mod用来对两个数作除法并且只返回余数 bin4 = 1Else bin4 = 0EndIfIf (Dats Mod4) \ 2 >= 1Then bin2 = 1Else bin2 = 0EndIfIf Dats Mod2 = 0Then bin1 = 0Else bin1 = 1EndIf bin128 = CStr(bin128) 'CStr 函数将一数值转换为 String bin64 = CStr(bin64) bin32 = CStr(bin32) bin16 = CStr(bin16) bin8 = CStr(bin8) 'CStr 函数将一数值转换为 String bin4 = CStr(bin4) bin2 = CStr(bin2) bin1 = CStr(bin1) dec2bin = bin128 + bin64 + bin32 + bin16 + bin8 + bin4 + bin2 + bin1EndFunction'地址计算Public Sub diziq() Dim setaddr As String setadOut = ListInAddr.Text If setadOut = "" Then MsgBox ("请输入元件地址!") Exit Sub End If If CStr(Val(setadOut)) <> setadOut Then Exit Sub '数字区包括了字母IfOct(Val("&o" + setadOut)) <> setadOut Then'判断是不是八进制。 Exit Sub End If DevDatOut = ydizi(setadOut)End Sub'地址范围是0500__057F,方式是1032PrivateFunction ydizi(Dats$) AsString Dim devadd AsString Dim station1 devadd = Hex("&o" + setadOut) station1 = "00" + devadd devadd = Right(station1, 2) ydizi = devadd + "05" + Chr(3) 'Y的地址End FunctionPrivate Function SumChk(Dats$) As String Dim I& Dim CHK& For I = 1 To Len(Dats) CHK = CHK + Asc(Mid(Dats, I, 1)) Next I SumChk = Right(Hex$(CHK), 2)End Function'关闭串口退出程序Private Sub cmdquit_Click()Set PLCForm = NothingIf MSComm1.PortOpen = TrueThen MSComm1.PortOpen = FalseEndEnd SubPrivate Sub Form_Unload(CancelAsInteger)Set PLCForm = NothingIf MSComm1.PortOpen = TrueThen MSComm1.PortOpen = FalseEndEnd Sub
上面是一个基本的VB与PLC程序实现的过程。
实际运行一下,才能深有体会。更多VB与工控、串口通信开发的教程以及实际项目、源码,正在学习的朋友可以添加下图小编微信号获取。
|