⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 基于MODBUS协议开发的一个测试程序,基于本人设计的一款温湿度仪表,内附说明书,已测试成功
💻 FRM
📖 第 1 页 / 共 3 页
字号:
         Width           =   1335
      End
   End
   Begin VB.CommandButton Command2 
      BackColor       =   &H00FFFFFF&
      Caption         =   "退 出"
      BeginProperty Font 
         Name            =   "仿宋_GB2312"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   4920
      Style           =   1  'Graphical
      TabIndex        =   1
      Top             =   6960
      Width           =   1215
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   1920
      Top             =   1200
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
   End
End
Attribute VB_Name = "FrmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
    

Private Sub Command1_Click()
T1.Enabled = True
End Sub

Private Sub Command2_Click()
T1.Enabled = False
Unload Me
End Sub


Private Sub Form_Load()
MsCom1
MsComerr
End Sub

Private Sub MSComm1_OnComm()
Dim i As Integer
Dim N As Integer
Dim T As Integer
Dim Y As Integer
Dim zH As Integer '高字节的中间变量
Dim zL As Integer '低字节的中间变量
Dim Z() As Byte
Dim X As Integer

        With MSComm1
           Select Case .CommEvent
                  Case comEvReceive
                  
                   .RThreshold = 0
                   Y = .InBufferCount
                  
                   For i = 1 To Y
                             Dim av As Variant
                             av = .Input
                             M1 = M1 + 1
                             Answerstr(M1) = av(0)
                                 
                             If M1 = 59 And Answerstr(58) = &HD And Answerstr(59) = &HA Then
                                '模块地址
                                 Label4.Caption = Chr(Answerstr(10)) + Chr(Answerstr(11))
                                '模块类型码
                                 Label1.Caption = Chr(Answerstr(8)) + Chr(Answerstr(9))
                                 '温度
                                   X = strHexToByteArray(Chr(Answerstr(12)) + Chr(Answerstr(13)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                   X = strHexToByteArray(Chr(Answerstr(14)) + Chr(Answerstr(15)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                   If Hex(zH) And Hex(8) = 1 Then
                                       Label6.Caption = Format(-((zH And &H7) * 256 + zL) * 0.0625, "0.0")
                                   Else
                                       Label6.Caption = Format(((zH And &H7) * 256 + zL) * 0.0625, "0.0")
                                   End If
                                   zH = 0
                                   zL = 0
                                 '湿度
                                   X = strHexToByteArray(Chr(Answerstr(16)) + Chr(Answerstr(17)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                   X = strHexToByteArray(Chr(Answerstr(18)) + Chr(Answerstr(19)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                   Label7.Caption = Format((zH * 256 + zL) / 10, "0.0")
                                   zH = 0
                                   zL = 0
                                   '继电器2的状态
                                   X = strHexToByteArray(Chr(Answerstr(20)) + Chr(Answerstr(21)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                   If zL = 20 Then
                                      Label30.Caption = "高报"
                    
                                   End If
                                   If zL = 21 Then
                                      Label30.Caption = "低报"
                                   
                                   End If
                                   If zL <> 20 And zL <> 21 Then
                                      Label30.Caption = ""
                                   End If
                                   zL = 0
                                   '继电器1的状态
                                   X = strHexToByteArray(Chr(Answerstr(22)) + Chr(Answerstr(23)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                   
                                   If zL = 20 Then
                                      Label29.Caption = "高报"
                                  
                                   End If
                                   If zL = 21 Then
                                      Label29.Caption = "低报"
                                  
                                   End If
                                   If zL <> 20 And zL <> 21 Then
                                      Label29.Caption = ""
                                   End If
                                   zL = 0
                                    '温度上限
                                   X = strHexToByteArray(Chr(Answerstr(24)) + Chr(Answerstr(25)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                    X = strHexToByteArray(Chr(Answerstr(26)) + Chr(Answerstr(27)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                    If Hex(zH) And Hex(8) = 1 Then
                                       Label9.Caption = Format(-((zH And &H7) * 256 + zL) * 0.0625 - 30, "0.0")
                                   Else
                                       Label9.Caption = Format(((zH And &H7) * 256 + zL) * 0.0625 - 30, "0.0")
                                   End If
                                   zH = 0
                                   zL = 0
                                   '温度下限
                                   X = strHexToByteArray(Chr(Answerstr(28)) + Chr(Answerstr(29)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                    X = strHexToByteArray(Chr(Answerstr(30)) + Chr(Answerstr(31)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                    If Hex(zH) And Hex(8) = 1 Then
                                       Label12.Caption = Format(-((zH And &H7) * 256 + zL) * 0.0625 - 30, "0.0")
                                   Else
                                       Label12.Caption = Format(((zH And &H7) * 256 + zL) * 0.0625 - 30, "0.0")
                                   End If
                                   zH = 0
                                   zL = 0
                                    '温度回差
                                   X = strHexToByteArray(Chr(Answerstr(32)) + Chr(Answerstr(33)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                    X = strHexToByteArray(Chr(Answerstr(34)) + Chr(Answerstr(35)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                    If Hex(zH) And Hex(8) = 1 Then
                                       Label13.Caption = Format(-((zH And &H7) * 256 + zL) * 0.0625, "0.0")
                                   Else
                                       Label13.Caption = Format(((zH And &H7) * 256 + zL) * 0.0625, "0.0")
                                   End If
                                   zH = 0
                                   zL = 0
                                    '湿度上限
                                   X = strHexToByteArray(Chr(Answerstr(36)) + Chr(Answerstr(37)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                   X = strHexToByteArray(Chr(Answerstr(38)) + Chr(Answerstr(39)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                   Label16.Caption = Format((zH * 256 + zL) / 10, "0.0")
                                   zH = 0
                                   zL = 0
                                     '湿度下限
                                   X = strHexToByteArray(Chr(Answerstr(40)) + Chr(Answerstr(41)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                   X = strHexToByteArray(Chr(Answerstr(42)) + Chr(Answerstr(43)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                   Label17.Caption = Format((zH * 256 + zL) / 10, "0.0")
                                   zH = 0
                                   zL = 0
                                    '湿度回差
                                   X = strHexToByteArray(Chr(Answerstr(44)) + Chr(Answerstr(45)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                   X = strHexToByteArray(Chr(Answerstr(46)) + Chr(Answerstr(47)), Z())
                                   If X > 0 Then
                                      zL = Z(0)
                                   End If
                                   Label19.Caption = Format((zH * 256 + zL) / 10, "0.0")
                                   zH = 0
                                   zL = 0
                                   '继电器选择
                                    X = strHexToByteArray(Chr(Answerstr(48)) + Chr(Answerstr(49)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                   If zH = 0 Then
                                      Label22.Caption = "温度上下限"
                                   End If
                                   If zH = 1 Then
                                      Label22.Caption = "温湿度上限"
                                   End If
                                   If zH = 2 Then
                                      Label22.Caption = "湿度上下限"
                                   End If
                                   If zH = 3 Then
                                      Label22.Caption = "温湿度下限"
                                   End If
                                   If zH = 4 Then
                                      Label22.Caption = "温度上限 湿度下限"
                                   End If
                                   If zH = 5 Then
                                      Label22.Caption = "温度下限 湿度上限"
                                   End If
                                   If zH = 6 Then
                                      Label22.Caption = "温度超限 湿度超限"
                                   End If
                                   zH = 0
                                   zL = 0
                                    '蜂鸣器状态
                                    X = strHexToByteArray(Chr(Answerstr(50)) + Chr(Answerstr(51)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                   If zH = 0 Then
                                      Label24.Caption = "打开"
                                   End If
                                   If zH = 255 Then
                                      Label24.Caption = "关闭"
                                   End If
                                    zH = 0
                                     '波特率
                                    X = strHexToByteArray(Chr(Answerstr(52)) + Chr(Answerstr(53)), Z())
                                   If X > 0 Then
                                      zH = Z(0)
                                   End If
                                   If zH = 6 Then
                                      Label25.Caption = "9600"
                                   End If
                                   If zH = 7 Then
                                      Label25.Caption = "19200"
                                   End If
                                    If zH = 8 Then
                                      Label25.Caption = "38400"
                                   End If
                                    zH = 0
              
                             End If
                             
                   Next i
               
                .RThreshold = 1
              End Select
        End With
End Sub

Private Sub T1_Timer()
'发送8230C模块读数据命令
Dim i As Integer
Dim d1() As Byte
ReDim d1(16) As Byte

M1 = 0
For i = 1 To 500
   Answerstr(i) = 0
Next i


'“:”的ASCII码
d1(0) = Asc(":")
'FF
d1(1) = Asc("F")
d1(2) = Asc("F")
'04
d1(3) = Asc("0")
d1(4) = Asc("4")
'00
d1(5) = Asc("0")
d1(6) = Asc("0")
'80
d1(7) = Asc("8")
d1(8) = Asc("0")
'00
d1(9) = Asc("0")
d1(10) = Asc("0")
'0C
d1(11) = Asc("0")
d1(12) = Asc("C")
'LRC效验码低两位
'71
d1(13) = Asc("7")
d1(14) = Asc("1")
'结束符
d1(15) = &HD
d1(16) = &HA

FrmMain.MSComm1.Output = d1
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -