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

📄 form1.frm

📁 VB通过PPI协议访问S7-200 PLC的源程序,
💻 FRM
📖 第 1 页 / 共 2 页
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1 
   BackColor       =   &H00FFFFC0&
   Caption         =   "S7-200监控"
   ClientHeight    =   5700
   ClientLeft      =   1860
   ClientTop       =   1035
   ClientWidth     =   11475
   LinkTopic       =   "Form1"
   ScaleHeight     =   5700
   ScaleWidth      =   11475
   Begin VB.CommandButton about 
      Caption         =   "关于"
      Height          =   495
      Left            =   9960
      TabIndex        =   25
      Top             =   3120
      Width           =   1215
   End
   Begin VB.Timer Timer2 
      Enabled         =   0   'False
      Interval        =   1000
      Left            =   4800
      Top             =   2040
   End
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   500
      Left            =   4800
      Top             =   1440
   End
   Begin VB.CommandButton UnForce 
      Caption         =   "全部取消强制"
      Enabled         =   0   'False
      Height          =   495
      Left            =   9960
      TabIndex        =   6
      Top             =   2040
      Width           =   1215
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   3240
      Top             =   240
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
   End
   Begin VB.Frame Frame1 
      BackColor       =   &H00FFFFC0&
      Height          =   855
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   4815
      Begin VB.CheckBox Chk_COM 
         BackColor       =   &H00FFFFC0&
         Height          =   615
         Left            =   1200
         TabIndex        =   2
         Top             =   120
         Width           =   615
      End
      Begin VB.CommandButton OpenCom 
         Caption         =   "打开串口"
         Height          =   495
         Left            =   2640
         TabIndex        =   1
         Top             =   240
         Width           =   1335
      End
   End
   Begin VB.Frame Frame2 
      BackColor       =   &H00FFFFC0&
      Height          =   855
      Left            =   5400
      TabIndex        =   3
      Top             =   120
      Width           =   5895
      Begin VB.CommandButton PLC_Stop 
         Caption         =   "停止"
         Enabled         =   0   'False
         Height          =   495
         Left            =   4560
         TabIndex        =   5
         Top             =   240
         Width           =   1215
      End
      Begin VB.CommandButton PLC_Run 
         Caption         =   "运行"
         Enabled         =   0   'False
         Height          =   495
         Left            =   2760
         TabIndex        =   4
         Top             =   240
         Width           =   1215
      End
      Begin VB.Label Label2 
         BackColor       =   &H00FFFFC0&
         BackStyle       =   0  'Transparent
         Height          =   375
         Left            =   1155
         TabIndex        =   24
         Top             =   390
         Width           =   855
      End
      Begin VB.Shape Shape1 
         FillColor       =   &H00C0E0FF&
         FillStyle       =   0  'Solid
         Height          =   495
         Left            =   600
         Shape           =   4  'Rounded Rectangle
         Top             =   240
         Width           =   1455
      End
   End
   Begin VB.Frame Frame3 
      BackColor       =   &H00FFFFC0&
      Height          =   4455
      Left            =   120
      TabIndex        =   7
      Top             =   1080
      Width           =   11175
      Begin VB.Frame Frame4 
         BackColor       =   &H00FFFFC0&
         Caption         =   "强制"
         Height          =   1300
         Left            =   5280
         TabIndex        =   14
         Top             =   2910
         Width           =   4455
         Begin VB.OptionButton Option1 
            BackColor       =   &H00FFFFC0&
            Caption         =   "Q"
            Height          =   255
            Left            =   480
            TabIndex        =   21
            Top             =   360
            Value           =   -1  'True
            Width           =   615
         End
         Begin VB.OptionButton Option2 
            BackColor       =   &H00FFFFC0&
            Caption         =   "I"
            Height          =   255
            Left            =   480
            TabIndex        =   20
            Top             =   840
            Width           =   615
         End
         Begin VB.TextBox Text1 
            Height          =   300
            Left            =   1920
            TabIndex        =   19
            Text            =   "0"
            Top             =   360
            Width           =   975
         End
         Begin VB.ComboBox Combo1 
            Height          =   300
            ItemData        =   "Form1.frx":0000
            Left            =   2880
            List            =   "Form1.frx":001C
            TabIndex        =   18
            Text            =   "0"
            Top             =   360
            Width           =   1215
         End
         Begin VB.CommandButton ReSet_Bit 
            Caption         =   "复位"
            Enabled         =   0   'False
            Height          =   300
            Left            =   2160
            TabIndex        =   17
            Top             =   840
            Width           =   615
         End
         Begin VB.CommandButton Set_Bit 
            Caption         =   "置位"
            Enabled         =   0   'False
            Height          =   300
            Left            =   1320
            TabIndex        =   16
            Top             =   840
            Width           =   615
         End
         Begin VB.CommandButton UN_F 
            Caption         =   "取消强制"
            Enabled         =   0   'False
            Height          =   300
            Left            =   3120
            TabIndex        =   15
            Top             =   840
            Width           =   975
         End
         Begin VB.Label Label1 
            BackColor       =   &H00FFFFC0&
            Caption         =   "地址"
            Height          =   255
            Left            =   1440
            TabIndex        =   22
            Top             =   420
            Width           =   375
         End
      End
      Begin VB.CommandButton Read_I 
         Caption         =   "读取状态"
         Enabled         =   0   'False
         Height          =   495
         Left            =   9840
         TabIndex        =   12
         Top             =   360
         Width           =   1215
      End
      Begin PLC监控程序.IO_Label IO_Label4 
         Height          =   1455
         Left            =   5280
         TabIndex        =   8
         Top             =   240
         Width           =   4695
         _ExtentX        =   8281
         _ExtentY        =   2566
         caption         =   "Q0"
         value           =   0
         BackColor       =   16777152
      End
      Begin PLC监控程序.IO_Label IO_Label3 
         Height          =   1335
         Left            =   360
         TabIndex        =   9
         Top             =   2880
         Width           =   4455
         _ExtentX        =   7858
         _ExtentY        =   2355
         caption         =   "I2"
         value           =   0
         BackColor       =   16777152
      End
      Begin PLC监控程序.IO_Label IO_Label2 
         Height          =   1335
         Left            =   360
         TabIndex        =   10
         Top             =   1560
         Width           =   4455
         _ExtentX        =   7858
         _ExtentY        =   2355
         caption         =   "I1"
         value           =   0
         BackColor       =   16777152
      End
      Begin PLC监控程序.IO_Label IO_Label1 
         Height          =   1455
         Left            =   360
         TabIndex        =   11
         Top             =   240
         Width           =   4455
         _ExtentX        =   7858
         _ExtentY        =   2566
         caption         =   "I0"
         value           =   0
         BackColor       =   16777152
      End
      Begin PLC监控程序.IO_Label IO_Label5 
         Height          =   1335
         Left            =   5280
         TabIndex        =   23
         Top             =   1560
         Width           =   4575
         _ExtentX        =   8070
         _ExtentY        =   2355
         caption         =   "Q1"
         value           =   0
         BackColor       =   16777152
      End
      Begin VB.Label Val_I 
         Height          =   255
         Index           =   1
         Left            =   1080
         TabIndex        =   13
         Top             =   1080
         Width           =   375
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

'========================================================
'= S7-200 PLC VB监控程序                                =
'=                                                      =
'=                                                      =
'= 欢迎交流                                             =
'= QQ:443129651  大海                                   =
'= Email:youhm1@163.com                                 =
'========================================================


Private Declare Sub Sleep Lib "kernel32" (ByVal dwseconds As Long)

Dim PLC_Cmd As String                   '指令识别字符串

Dim Val_Ok(5) As Byte                   '确认指令数据

Dim ReadyOk As Boolean                  '空闲




Private Sub Form_Load()
  
  MSComm1.CommPort = 1                  '初始化串口
  
  MSComm1.Settings = "9600,e,8,1"
    
  MSComm1.InputLen = 0

  MSComm1.InputMode = comInputModeBinary
  
  Val_Ok(0) = &H10
  Val_Ok(1) = &H2
  Val_Ok(2) = &H0
  Val_Ok(3) = &H5C
  Val_Ok(4) = &H5E
  Val_Ok(5) = &H16

  
End Sub

Private Sub about_Click()

  Form2.Show

End Sub

Private Sub OpenCom_Click()              '打开串口
  
  On Error GoTo err
  
  If Not MSComm1.PortOpen Then
  
     MSComm1.PortOpen = True
     
     Chk_COM.Value = 1
     
     OpenCom.Caption = "关闭串口"
     
     PLC_Run.Enabled = True
     
     PLC_Stop.Enabled = True
     
     Read_I.Enabled = True
     
     UnForce.Enabled = True
     
     UN_F.Enabled = True
     
     Set_Bit.Enabled = True
     
     ReSet_Bit.Enabled = True
     
     ReadyOk = True
     
  Else
  
     MSComm1.PortOpen = False
     
     Chk_COM.Value = 0
     
     OpenCom.Caption = "打开串口"
     
     PLC_Run.Enabled = False
     
     PLC_Stop.Enabled = False
     
     Read_I.Enabled = False
     
     UnForce.Enabled = False
     
     UN_F.Enabled = False
     
     Set_Bit.Enabled = False
     
     ReSet_Bit.Enabled = False
     
     ReadyOk = False
     
  End If
  
  Exit Sub
  
err:

  MsgBox "串口打开失败", vbInformation, "打开串口"
  
End Sub

Private Sub MSComm1_OnComm()          '串口事件处理

  Dim Rcv_Data() As Byte
  
  Select Case MSComm1.CommEvent
  
  Case comEvReceive                   '串口接收事件
  
  Rcv_Data = MSComm1.Input            '取出串口接收缓冲器的数据。
  
  If Rcv_Data(UBound(Rcv_Data)) = &H16 Then ReadyOk = True: Timer2.Enabled = False
  
  If UBound(Rcv_Data) = 0 Then                 'PLC返回一个字节
     
     If Rcv_Data(0) = &HE5 And PLC_Cmd <> "UnForce" Then        'PLC返回E5则发送确认指令
        
        MSComm1.Output = Val_Ok

        If PLC_Cmd = "Run" Or PLC_Cmd = "Stop" Then
        
           MSComm1.RThreshold = 21
           
        End If
        
        If PLC_Cmd = "Rbyte" Then
        
           MSComm1.RThreshold = 37
           
        End If
        
     End If
     
  Else
     

⌨️ 快捷键说明

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