基于MODBUS現(xiàn)場(chǎng)總線技術(shù)的電力儀表遠(yuǎn)程通訊軟件開發(fā)
4. 人機(jī)界面:
本文引用地址:http://m.butianyuan.cn/article/201612/331178.htm
5.程序代碼:
API 函數(shù)聲名
Private Declare Function GetTickCount Lib "kernel32" () As Long
點(diǎn)擊結(jié)束程序按鈕
Private Sub CmdEnd_Click()
End
End Sub
點(diǎn)擊讀取電功率按鈕
Private Sub CmdRead_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
CmdRead.Caption = "停止讀數(shù)"
Else
CmdRead.Caption = "讀取電功率"
&nb
sp;End If
End Sub
程序初始化
Private Sub Form_Load()
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Dim C4%, C5%, C6%, C7%
Dim Tick&
Dim Data As Double
Dim Buf() As Byte
Dim ByteOut(7) As Byte
MSComm1.InputLen = 0
Buf = MSComm1.Input
重新定義數(shù)據(jù)輸入緩沖區(qū), 發(fā)送命令 4
MSComm1.InputLen = 1
ByteOut(0) = &H1
ByteOut(1) = &H4
ByteOut(2) = &H0
ByteOut(3) = &H5C
ByteOut(4) = &H0
ByteOut(5) = &H4
ByteOut(6) = &H31
ByteOut(7) = &HDB
MSComm1.Output = ByteOut(0) & ByteOut(1) & ByteOut(2) & ByteOut(3) & ByteOut(4) & ByteOut(5) &
ByteOut(6) & ByteOut(7)
&
nbsp; 等待多功能電力儀表響應(yīng)的數(shù)據(jù)到達(dá)串口
Tick = GetTickCount()
Do
If (GetTickCount() - Tick) / 1000# > 10 Then
MsgBox "時(shí)間過久,請(qǐng)檢查多功能電力儀表及傳輸狀態(tài)!", vbCritical + vbOKOnly, "系統(tǒng)信息"
Exit Sub
End If
DoEvents
Loop Until MSComm1.InBuFFerCount >= 9
從輸入緩沖區(qū)讀數(shù)據(jù)
Buf = MSComm1.Input
If Buf(0) <> 1 Then
MsgBox "地址錯(cuò)誤!", vbCritical + vbOKOnly, "系統(tǒng)信息"
Exit Sub
End If
Buf = MSComm1.Input
If Buf(0) <> 4 Then
MsgBox "長(zhǎng)度錯(cuò)誤!", vbCritical + vbOKOnly, "系統(tǒng)信息"
Exit Sub
End If
Buf = MSComm1.Input
C4 = Buf(0)
Buf = MSComm1.Input
C5 = Buf(0)
Buf = MSComm1.Input
C6 = Buf(0)
Buf = MSComm1.Input
C7 = Buf(0)
Buf = MSComm1.Input
Buf = MSComm1.Input
Data = Power(C4, C5, C6, C7)
TxtPower.Text = CStr(Data)
End Sub
自定義功率換算函數(shù)
Private Function Power(C4%, C5%, C6%, C7%)
Dim PowerExp As String, Mantissa As String
Dim ReadValue#
Dim ValueSign&
If ((C4 And &H80) / 2 ︿ 7) = True Then
ValueSign = -1
Else
ValueSign = 1
End If
PowerExp = CStr((C4 And &H7F) * 2 + (C5 And &H80) / 2 ︿ 7)
Mantissa = CStr((&H80 or C5) * 2 ︿ 16 + C6 * 2 ︿ 8 + C7)
ReadValue = ValueSign * 2 ︿ (Val(PowerExp) - 126) * Val(Mantissa) / (256 * 65536)
Power = ReadValue
End Function
四、結(jié)論
本實(shí)例是作者在實(shí)驗(yàn)室做過多次實(shí)驗(yàn),達(dá)到了預(yù)定的應(yīng)用效果,目前在江西某廠取得了實(shí)際的應(yīng)用。
該控制系統(tǒng)的最大優(yōu)點(diǎn)是:
1,從硬件上看,僅僅使用一個(gè)轉(zhuǎn)換模塊就可以實(shí)現(xiàn)全廠電能遠(yuǎn)程監(jiān)控和集中管理的功能。大大提高了效率,且十分經(jīng)濟(jì)實(shí)惠。
2,從軟件上看,直接使用串口的控制技術(shù),而不必需要寫額外的驅(qū)動(dòng)程序。采用VB 開發(fā)應(yīng)用軟件十分方便,編程工作量小。
評(píng)論