基于MODBUS現(xiàn)場總線技術(shù)的電力儀表遠程通訊軟件開發(fā)
4. 人機界面:
本文引用地址:http://www.ex-cimer.com/article/201612/331178.htm
5.程序代碼:
API 函數(shù)聲名
Private Declare Function GetTickCount Lib "kernel32" () As Long
點擊結(jié)束程序按鈕
Private Sub CmdEnd_Click()
End
End Sub
點擊讀取電功率按鈕
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ù)到達串口
Tick = GetTickCount()
Do
If (GetTickCount() - Tick) / 1000# > 10 Then
MsgBox "時間過久,請檢查多功能電力儀表及傳輸狀態(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 "地址錯誤!", vbCritical + vbOKOnly, "系統(tǒng)信息"
Exit Sub
End If
Buf = MSComm1.Input
If Buf(0) <> 4 Then
MsgBox "長度錯誤!", 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é)論
本實例是作者在實驗室做過多次實驗,達到了預(yù)定的應(yīng)用效果,目前在江西某廠取得了實際的應(yīng)用。
該控制系統(tǒng)的最大優(yōu)點是:
1,從硬件上看,僅僅使用一個轉(zhuǎn)換模塊就可以實現(xiàn)全廠電能遠程監(jiān)控和集中管理的功能。大大提高了效率,且十分經(jīng)濟實惠。
2,從軟件上看,直接使用串口的控制技術(shù),而不必需要寫額外的驅(qū)動程序。采用VB 開發(fā)應(yīng)用軟件十分方便,編程工作量小。
評論