Anda di halaman 1dari 3

VB-6 Code for MODBUS_RTU_protocol by Mr.

Adeeb Raza Using CADEL Demand Analyzer Digital Meter CD-XXX with RS485 port for PC Interfacing. http://www.areptone.com/vb6/areptone_modbus_rtu.pdf http://www.areptone.com/cadel/MODBUS_RTU_protocol_v8_CD_701.pdf
Following code has written in a Form using VB6 Option Explicit Public m_svr1 As Object Dim status As Integer Dim A, B, C, D, E, X, VT, AT, WT, VR, VY, VB, AR, AY, AB, PF Private Sub Form_Load() Call Command3_Click End Sub Private Sub Command1_Click() m_svr1.Register(0) = Text1

' Store the value in ' Modbus Poll register array index 0 ' Physical not the same register used in read status = m_svr1.PresetMultipleRegisters(1, 10, 1) 'Write 1 value (m_svr1.Register(0)) to address 2 (40003) slave ID 1 End Sub Private Sub Command2_Click() ' Use this command to show Modbus Poll status = m_svr1.ShowWindow End Sub Private Sub Command3_Click() Set m_svr1 = CreateObject("mbpoll.Document") A = Text2 B = Text3 C = Text4 D = Text5 E = Text6 '******************************************* 'A = 1 'ID A= Slave ID 1 'B = 3 'Function B= Read 3 Holding Register 'C = 0 'Address C= From addr. 0 (40001) 'D = 16 'Number of Register D= Number of 16 Datas 'E = 1000 'Time ms E= Each Scan 1000ms '******************************************* status = m_svr1.CreateRequest(A, B, C, D, E) 'These 16 values are stored in m_svr1.Register(0) to 15 'status = m_svr1.ShowWindow ' Use this command to show Modbus 'Values are always store from index 0 no matter if we read from e.g. 'addr. 10 (40011) Label3(21).Caption = A Label3(22).Caption = B Label3(23).Caption = C Label3(24).Caption = D Label3(25).Caption = E End Sub

Private Sub Timer1_Timer() Label8.Caption = Format(Date, "dd - mm - yyyy") 'Date Label9.Caption = Format(Time, "HH:MM:SS") 'Time Label10.Caption = Format(Time, "SS") 'Seconds If Label10.Caption = 60 Then X = 1 'Minute X=X+1 Label11.Caption = X 'Minute Counter If X >= 540 Then Label7.Caption = "9 Minute DEMO TIME OVER" 'Demo Time Reminder If X >= 560 Then Call Command4_Click 'Quit at 10 Minute Result = m_svr1.ReadResult Label1(0) = m_svr1.Register(0) 'Reading of Register No.30000 = Input Register Label1(1) = m_svr1.Register(1) 'Cadel-0XXXX bits 1 to 16 Reading of Register No.30001 Label1(2) = m_svr1.Register(2) 'Cadel-0XXXX bits 17 to 32 Reading of Register No.30002 Label1(3) = m_svr1.Register(3) 'Cadel-CT AMPS Reading in Register No.30003 Label1(4) = m_svr1.Register(4) 'Cadel-PT VOLTS HI Reading in Register No.30004 Label1(5) = m_svr1.Register(5) 'Cadel-PT VOLTS LO Reading in Register No.30005 Label1(6) = m_svr1.Register(6) 'Cadel-KWH HI Reading in Register No.30006 Label1(7) = m_svr1.Register(7) 'Cadel-KWH LO Reading in Register No.30007 Label1(8) = m_svr1.Register(8) 'Cadel-POWER FACTOR Reading in Register No.30008 Label1(9) = m_svr1.Register(9) 'Cadel-ACTIVE POWER W Reading in Register No.30009 Label1(10) = m_svr1.Register(10) 'Cadel-APPARENT POWER VA Reading in Register No.30010 Label1(11) = m_svr1.Register(11) 'Cadel-REACTIVE POWER VAR Reading in Register No.30011 Label1(12) = m_svr1.Register(12) 'Cadel-CURRENT IR Reading in Register No.30012 Label1(13) = m_svr1.Register(13) 'Cadel-CURRENT IY Reading in Register No.30013 Label1(14) = m_svr1.Register(14) 'Cadel-CURRENT IB Reading in Register No.30014 Label1(15) = m_svr1.Register(15) 'Cadel-VOLTAGE V_RY Reading in Register No.30015 Label1(16) = m_svr1.Register(16) 'Cadel-VOLTAGE V_YB Reading in Register No.30016 Label1(17) = m_svr1.Register(17) 'Cadel-VOLTAGE V_BR Reading in Register No.30017 Label1(18) = m_svr1.Register(18) 'Cadel-KVAH HI Reading in Register No.30018 Label1(19) = m_svr1.Register(19) 'Cadel-KVAH LO Reading in Register No.30019 Label1(20) = m_svr1.Register(20) 'Cadel-FREQUENCY FR Reading in Register No.30020 Label1(21) = m_svr1.Register(21) 'Cadel-KVARH+ HI (LGPF) Reading in Register No.30021 Label1(22) = m_svr1.Register(22) 'Cadel-KVARH+ LO Reading in Register No.30022 Label1(23) = m_svr1.Register(23) 'Cadel-KVARH- HI (LDPF) Reading in Register No.30023 Label1(24) = m_svr1.Register(24) 'Cadel-KVARH- LO Reading in Register No.30024 Label1(25) = m_svr1.Register(25) 'Cadel-VOLTAGE VRN Reading in Register No.30025 Label1(26) = m_svr1.Register(26) 'Cadel-VOLTAGE VYN Reading in Register No.30026 Label1(27) = m_svr1.Register(27) 'Cadel-VOLTAGE VBN Reading in Register No.30027 Label1(28) = m_svr1.Register(28) 'Cadel-ACTIVE KW_R Reading in Register No.30028 Label1(29) = m_svr1.Register(29) 'Cadel-ACTIVE KW_Y Reading in Register No.30029 Label1(30) = m_svr1.Register(30) 'Cadel-ACTIVE KW_B Reading in Register No.30030 Label1(31) = m_svr1.Register(31) 'Cadel-APPARENT VA_R Reading in Register No.30031 'Label1(32) = m_svr1.Register(32) 'Cadel-APPARENT VA_Y Reading in Register No.30032 'Label1(33) = m_svr1.Register(33) 'Cadel-APPARENT VA_B Reading in Register No.30033 'Label1(34) = m_svr1.Register(34) 'Cadel-REACTIVE VAR_R Reading in Register No.30034 'Label1(35) = m_svr1.Register(35) 'Cadel-REACTIVE VAR_Y Reading in Register No.30035 'Label1(36) = m_svr1.Register(36) 'Cadel-REACTIVE VAR_B Reading in Register No.30036 'Label1(37) = m_svr1.Register(37) 'Cadel-FUTURE USE in Register No.30037 Call CADEL Call ERROR End Sub Sub ERROR() 'Show Selected Function On Screen Cadel Meter Readings Check Errors

If Label3(22).Caption = 3 Then Label5.Caption = "Function 3 --> Reading Holding Register " If Label3(22).Caption = 4 Then Label5.Caption = "Function 4 --> Reading Input Register " 'Show Error on Screen If Result.Caption = 0 Then Label4.Caption = "OK" If Result.Caption = 1 Then Label4.Caption = "Error (1) Time Out" If Result.Caption = 6 Then Label4.Caption = "Error (6) 10 Minute Demo Time Expaired" If Result.Caption = 10 Then Label4.Caption = "Error (10) Not Connected" If Result.Caption = 130 Then Label4.Caption = "Error (130) Set No.Of Reg Max <=19" End Sub Sub CADEL() Label15.Caption = m_svr1.Register(2) 'CR Ratio Label16.Caption = (m_svr1.Register(3) * 65536) + (m_svr1.Register(4)) 'PT Ratio Label20.Caption = (m_svr1.Register(14) * 11000) / 10000 'R phase Volt Label21.Caption = (m_svr1.Register(15) * 11000) / 10000 'Y phase Volt Label22.Caption = (m_svr1.Register(16) * 11000) / 10000 'B phase Volt Label26.Caption = (m_svr1.Register(11) * 300) / 10000 'R phase Amp Label27.Caption = (m_svr1.Register(12) * 300) / 10000 'Y phase Amp Label28.Caption = (m_svr1.Register(13) * 300) / 10000 'B phase Amp Label30.Caption = (m_svr1.Register(7) / 1000) 'Power Factor Label32.Caption = (m_svr1.Register(5) * 65536) + (m_svr1.Register(6)) 'KWH Label34.Caption = (m_svr1.Register(19) / 10) 'Frequency 50 Hz Followings Readings will Show on Screen VR = (m_svr1.Register(14) * 11000) / 10000 VY = (m_svr1.Register(15) * 11000) / 10000 VB = (m_svr1.Register(16) * 11000) / 10000 VT = (VR + VY + VB) / 3 Label38.Caption = VT AR = m_svr1.Register(11) * 300 / 10000 AY = m_svr1.Register(12) * 300 / 10000 AB = m_svr1.Register(13) * 300 / 10000 AT = (AR + AY + AB) / 3 Label40.Caption = AT PF = (m_svr1.Register(7) / 1000) WT = (1.732 * VT * AT * PF) Label36.Caption = WT / 1000 End Sub Private Sub Command4_Click() End End Sub THE END Click here to Download VB-6 Program http://www.areptone.com/vb6/test765.zip & read first read me file for help

'R phase Volt 'Y phase Volt 'R phase Volt 'Evarage V of R Y B Phase 'Show Evarage V of R Y B Phase 'R phase Amp 'Y phase Amp 'B phase Amp 'Evarage A of R Y B Phase 'Show Evarage A of R Y B Phase 'Power Factor 'Watt

Anda mungkin juga menyukai