新闻中心

EEPW首页>嵌入式系统>设计应用> 基于MODBUS现场总线技术的电力仪表远程通讯软件开发

基于MODBUS现场总线技术的电力仪表远程通讯软件开发

作者: 时间:2016-12-19 来源:网络 收藏

4. 人机界面:

本文引用地址://m.amcfsurvey.com/article/201612/331178.htm
5.程序代码:
API 函数声名
Private Declare Function GetTickCount Lib "kernel32" () As Long
点击结束程序按钮
Private Sub CmdEnd_Click()
End
End Sub
点击读取电功率按钮
Private Sub CmdRead_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
CmdRead.Caption = "停止读数"
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
重新定义数据输入缓冲区, 发送命令 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; 等待多功能电力仪表响应的数据到达串口
Tick = GetTickCount()
Do
If (GetTickCount() - Tick) / 1000# > 10 Then
MsgBox "时间过久,请检查多功能电力仪表及传输状态!", vbCritical + vbOKOnly, "系统信息"
Exit Sub
End If
DoEvents
Loop Until MSComm1.InBuFFerCount >= 9
从输入缓冲区读数据
Buf = MSComm1.Input
If Buf(0) <> 1 Then
MsgBox "地址错误!", vbCritical + vbOKOnly, "系统信息"
Exit Sub
End If
Buf = MSComm1.Input
If Buf(0) <> 4 Then
MsgBox "长度错误!", vbCritical + vbOKOnly, "系统信息"
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
自定义功率换算函数
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
四、结论
本实例是作者在实验室做过多次实验,达到了预定的应用效果,目前在江西某厂取得了实际的应用。
该控制系统的最大优点是:
1,从硬件上看,仅仅使用一个转换模块就可以实现全厂电能远程监控和集中管理的功能。大大提高了效率,且十分经济实惠。
2,从软件上看,直接使用串口的控制技术,而不必需要写额外的驱动程序。采用VB 开发应用软件十分方便,编程工作量小。

上一页 1 2 下一页

评论


技术专区

关闭