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

📄 frmmbtcp.frm

📁 modbus协议用vb编写
💻 FRM
📖 第 1 页 / 共 4 页
字号:
      Width           =   495
   End
   Begin VB.Label lbCommand 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte7]"
      Height          =   255
      Index           =   7
      Left            =   4320
      TabIndex        =   22
      Top             =   5580
      Width           =   495
   End
   Begin VB.Label lbCommand 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte6]"
      Height          =   255
      Index           =   6
      Left            =   3720
      TabIndex        =   21
      Top             =   5580
      Width           =   495
   End
   Begin VB.Label lbCommand 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte5]"
      Height          =   255
      Index           =   5
      Left            =   3120
      TabIndex        =   20
      Top             =   5580
      Width           =   495
   End
   Begin VB.Label lbCommand 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte4]"
      Height          =   255
      Index           =   4
      Left            =   2520
      TabIndex        =   19
      Top             =   5580
      Width           =   495
   End
   Begin VB.Label lbCommand 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte3]"
      Height          =   255
      Index           =   3
      Left            =   1920
      TabIndex        =   18
      Top             =   5580
      Width           =   495
   End
   Begin VB.Label lbCommand 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte2]"
      Height          =   255
      Index           =   2
      Left            =   1320
      TabIndex        =   17
      Top             =   5580
      Width           =   495
   End
   Begin VB.Label lbCommand 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte1]"
      Height          =   255
      Index           =   1
      Left            =   720
      TabIndex        =   16
      Top             =   5580
      Width           =   495
   End
   Begin VB.Label lbResponse 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte0]"
      Height          =   255
      Index           =   0
      Left            =   5040
      TabIndex        =   11
      Top             =   5580
      Width           =   495
   End
   Begin VB.Label lbCommand 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte0]"
      Height          =   255
      Index           =   0
      Left            =   120
      TabIndex        =   9
      Top             =   5580
      Width           =   495
   End
   Begin VB.Label lbTCPHead 
      BackStyle       =   0  'Transparent
      Caption         =   "[Byte0]"
      Height          =   255
      Index           =   0
      Left            =   120
      TabIndex        =   10
      ToolTipText     =   " Transaction identifier - copied by server - usually 0 "
      Top             =   4980
      Width           =   495
   End
End
Attribute VB_Name = "frmMBTCP"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

'TCPModbus_Slave: Sends command to device with TCPModbus protocol

' [29/Nov/2001] by Kevin

' Version 1.1.0 [20,Mar,2003] by Kevin
'   Add extention function to display received data.
'
' Version 1.1.1 [15,Apr,2003] by Kevin
'   Add for DO, for DI, for AI, for AO to description of function code
'
' Version 1.1.2 [05,May,2003] by Kevin
'   Modify display for received data
'   Add polling mode to send command

' Version 1.1.3 [30,Jun,2003] by Kevin
'   Add data log function.
'     Creat a text file (DataLog.txt) after connection is created.
'     Close the text file after connection is disconnected.
'     All communication data is stored in the text file.
'
' Version 1.1.4 [12,Apr,2005] by Kevin
'   Re-arrange all items in the statistic frame.
'
'   Disable display command/response packages when run polling mode.
'      This can improve communication performance. That can estimate
'      the communication performance more exactly.
'

Option Explicit

Dim mlReceiveByte As Long, mlReceivePacket As Long
Dim mlSendByte As Long, mlSendPacket As Long
Dim mlDiffPacket As Long
Dim mbPolling As Boolean

Dim fso As New FileSystemObject

Dim bFileIsReady As Boolean



Private Sub cboFunction_Change()
    Dim i As Integer
    
    Select Case cboFunction.Text
        Case "FC1  Read multiple coils status (0xxxx)  for DO"
            'Call ShowTipText(1)
            txtExplanation.Text = "[Prefixed 6 bytes of Modbus/TCP protocol]" & vbCrLf & _
                                  "    Byte 0: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 1: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 2: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 3: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 4: Length field (upper byte)=0" & vbCrLf & _
                                  "                (since all messages are smaller than 256)" & vbCrLf & _
                                  "    Byte 5: Length field (lower byte)=number of bytes following" & vbCrLf & _
                                  vbCrLf & _
                                  "[Request]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=01" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Bit count" & vbCrLf & _
                                  vbCrLf & _
                                  "[Response]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=01" & vbCrLf & _
                                  "    Byte 2:       Byte count of response (B=(bit count + 7)/8)" & vbCrLf & _
                                  "    Byte 3-(B+2): Bit values (least significant is first coil!)"

        Case "FC2  Read multiple input discretes (1xxxx)  for DI"
            'Call ShowTipText(2)
            txtExplanation.Text = "[Prefixed 6 bytes of Modbus/TCP protocol]" & vbCrLf & _
                                  "    Byte 0: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 1: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 2: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 3: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 4: Length field (upper byte)=0" & vbCrLf & _
                                  "                (since all messages are smaller than 256)" & vbCrLf & _
                                  "    Byte 5: Length field (lower byte)=number of bytes following" & vbCrLf & _
                                  vbCrLf & _
                                  "[Request]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=02" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Bit count" & vbCrLf & _
                                  vbCrLf & _
                                  "[Response]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=02" & vbCrLf & _
                                  "    Byte 2:       Byte count of response (B=(bit count + 7)/8)" & vbCrLf & _
                                  "    Byte 3-(B+2): Bit values (least significant is first coil!)"

        Case "FC3  Read multiple registers (4xxxx)  for AO"
            'Call ShowTipText(3)
            txtExplanation.Text = "[Prefixed 6 bytes of Modbus/TCP protocol]" & vbCrLf & _
                                  "    Byte 0: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 1: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 2: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 3: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 4: Length field (upper byte)=0" & vbCrLf & _
                                  "                (since all messages are smaller than 256)" & vbCrLf & _
                                  "    Byte 5: Length field (lower byte)=number of bytes following" & vbCrLf & _
                                  vbCrLf & _
                                  "[Request]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=03" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Word count" & vbCrLf & _
                                  vbCrLf & _
                                  "[Response]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=03" & vbCrLf & _
                                  "    Byte 2:       Byte count of response (B=2 x word count)" & vbCrLf & _
                                  "    Byte 3-(B+2): Register values"

        Case "FC4  Read multiple input registers (3xxxx)  for AI"
            'Call ShowTipText(4)
            txtExplanation.Text = "[Prefixed 6 bytes of Modbus/TCP protocol]" & vbCrLf & _
                                  "    Byte 0: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 1: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 2: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 3: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 4: Length field (upper byte)=0" & vbCrLf & _
                                  "                (since all messages are smaller than 256)" & vbCrLf & _
                                  "    Byte 5: Length field (lower byte)=number of bytes following" & vbCrLf & _
                                  vbCrLf & _
                                  "[Request]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=04" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Word count" & vbCrLf & _
                                  vbCrLf & _
                                  "[Response]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=04" & vbCrLf & _
                                  "    Byte 2:       Byte count of response (B=2 x word count)" & vbCrLf & _
                                  "    Byte 3-(B+2): Register values"
                                  
        Case "FC5  Write single coil (0xxxx)  for DO"
            'Call ShowTipText(5)
            txtExplanation.Text = "[Prefixed 6 bytes of Modbus/TCP protocol]" & vbCrLf & _
                                  "    Byte 0: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 1: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 2: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 3: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 4: Length field (upper byte)=0" & vbCrLf & _
                                  "                (since all messages are smaller than 256)" & vbCrLf & _
                                  "    Byte 5: Length field (lower byte)=number of bytes following" & vbCrLf & _
                                  vbCrLf & _
                                  "[Request]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=05" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4:       =FF to trun ON coil, =00 to trun OFF coil" & vbCrLf & _
                                  "    Byte 5:       =00" & vbCrLf & _
                                  vbCrLf & _
                                  "[Response]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=05" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4:       =FF to trun ON coil, =00 to trun OFF coil (echoed)" & vbCrLf & _
                                  "    Byte 5:       =00" & vbCrLf

        Case "FC6  Write single register (4xxxx)  for AO"
            'Call ShowTipText(6)
            txtExplanation.Text = "[Prefixed 6 bytes of Modbus/TCP protocol]" & vbCrLf & _
                                  "    Byte 0: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 1: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 2: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 3: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 4: Length field (upper byte)=0" & vbCrLf & _
                                  "                (since all messages are smaller than 256)" & vbCrLf & _
                                  "    Byte 5: Length field (lower byte)=number of bytes following" & vbCrLf & _
                                  vbCrLf & _
                                  "[Request]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=06" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Register value" & vbCrLf & _
                                  vbCrLf & _
                                  "[Response]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=06" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Register value"

        Case "FC15 Force multiple coils (0xxxx)  for DO"
            'Call ShowTipText(15)
            txtExplanation.Text = "[Prefixed 6 bytes of Modbus/TCP protocol]" & vbCrLf & _
                                  "    Byte 0: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 1: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 2: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 3: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 4: Length field (upper byte)=0" & vbCrLf & _
                                  "                (since all messages are smaller than 256)" & vbCrLf & _
                                  "    Byte 5: Length field (lower byte)=number of bytes following" & vbCrLf & _
                                  vbCrLf & _
                                  "[Request]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=0F (hex)" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Bit count" & vbCrLf & _
                                  "    Byte 6:       Byte count (B=(bit count + 7)/8)" & vbCrLf & _
                                  "    Byte 7-(B+6): Data to be written (least significant is first coil!)" & vbCrLf & _
                                  vbCrLf & _
                                  "[Response]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=0F (hex)" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Bit count"

        Case "FC16 Write multiple registers (4xxxx)  for AO"
            'Call ShowTipText(16)
            txtExplanation.Text = "[Prefixed 6 bytes of Modbus/TCP protocol]" & vbCrLf & _
                                  "    Byte 0: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 1: Transaction identifier - copied by server - usually 0" & vbCrLf & _
                                  "    Byte 2: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 3: Protocol identifier=0" & vbCrLf & _
                                  "    Byte 4: Length field (upper byte)=0" & vbCrLf & _
                                  "                (since all messages are smaller than 256)" & vbCrLf & _
                                  "    Byte 5: Length field (lower byte)=number of bytes following" & vbCrLf & _
                                  vbCrLf & _
                                  "[Request]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=10 (hex)" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Word count" & vbCrLf & _
                                  "    Byte 6:       Byte count (B=2 x word count)" & vbCrLf & _
                                  "    Byte 7-(B+6): Register values" & vbCrLf & _
                                  vbCrLf & _
                                  "[Response]" & vbCrLf & _
                                  "    Byte 0:       Net ID (Station number)" & vbCrLf & _
                                  "    Byte 1:       FC=10 (hex)" & vbCrLf & _
                                  "    Byte 2-3:     Reference number" & vbCrLf & _
                                  "    Byte 4-5:     Word count"
    End Select
End Sub

Private Sub cboFunction_Click()
    Call cboFunction_Change
End Sub

Private Sub chkDataLog_Click()
    If chkDataLog.Value = vbChecked Then
        'for exporting reference address document
        fso.CreateTextFile "DataLog.txt", True
        Open "DataLog.txt" For Output As #1
        bFileIsReady = True
    Else
        bFileIsReady = False
        Close #1
    End If
End Sub

Private Sub cmdStartPolling_Click()
    lStartTime = GetTickCount
    mbPolling = True
    lTime_Max = 0
    lTime_Min = 1000
    txtTimeMax.Text = 0
    txtTimeMin.Text = 1000
    cmdStartPolling.Enabled = False
    cmdStopPolling.Enabled = True
    txtStartTime.Text = Now
    cmdSend_Click
End Sub

Private Sub cmdStartTimer_Click()
    If Winsock1.State = sckConnected Then
        Timer1.Enabled = True
        cmdStartTimer.Enabled = False
        cmdStopTimer.Enabled = True
        txtStartTime.Text = Time
        txtStopTime.Text = "Stop Time"
    End If
End Sub

Private Sub CmdClear_Click()

⌨️ 快捷键说明

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