📄 modtstsm.frm
字号:
VERSION 5.00
Object = "{633262C2-113D-11D3-82A8-0080C802724C}#6.1#0"; "MODCOMSE.OCX"
Begin VB.Form ModComSimple
Caption = "ModCom Test Application Screen 1"
ClientHeight = 7545
ClientLeft = 2010
ClientTop = 1380
ClientWidth = 8970
LinkTopic = "Form1"
ScaleHeight = 7545
ScaleWidth = 8970
Begin ModBusOCXCntrlSmplEX.ModCom ModCom1
Left = 510
Top = 3480
_ExtentX = 847
_ExtentY = 873
BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
RTSEnable = 0 'False
ComHandshaking = 0
ScanWriteIOTimerInterval= 250
End
Begin VB.CommandButton Command1
Caption = "Modem Functions"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 465
Left = 4650
TabIndex = 30
Top = 6330
Width = 1575
End
Begin VB.CommandButton ModSlv
Caption = "Modbus Slave Functions"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 6450
TabIndex = 29
Top = 6330
Width = 1575
End
Begin VB.Frame Frame2
Caption = "Port Parameters"
Height = 2535
Left = 120
TabIndex = 1
Top = 300
Width = 2115
Begin VB.TextBox Text10
Height = 315
Index = 1
Left = 840
TabIndex = 19
Text = "9600,e,8,1"
Top = 960
Width = 915
End
Begin VB.CommandButton Command13
Caption = "Close Port"
Height = 465
Left = 1080
TabIndex = 17
Top = 1680
Width = 945
End
Begin VB.CommandButton Command8
Caption = "open port"
Height = 465
Left = 60
TabIndex = 3
Top = 1680
Width = 930
End
Begin VB.TextBox Text10
Height = 375
Index = 0
Left = 840
TabIndex = 2
Text = "1"
Top = 420
Width = 315
End
Begin VB.Label Label12
Alignment = 1 'Right Justify
Caption = "Port Settings"
Height = 435
Index = 1
Left = 120
TabIndex = 20
Top = 900
Width = 555
End
Begin VB.Label Label12
Alignment = 1 'Right Justify
Caption = "Port Num"
Height = 435
Index = 0
Left = 240
TabIndex = 18
Top = 360
Width = 435
End
End
Begin VB.Frame Frame1
Caption = "Read / Write PLC"
Height = 5475
Left = 2640
TabIndex = 0
Top = 240
Width = 5805
Begin VB.Frame Frame3
Caption = "Write to PLC"
Height = 2535
Left = 3720
TabIndex = 23
Top = 360
Width = 1995
Begin VB.TextBox Text1
Height = 345
Left = 720
TabIndex = 27
Text = "40010"
Top = 1260
Width = 1095
End
Begin VB.CommandButton Command3
BackColor = &H0000FFFF&
Caption = "Write Device"
Height = 630
Left = 720
Style = 1 'Graphical
TabIndex = 25
Top = 420
Width = 1080
End
Begin VB.TextBox Text4
Height = 330
Left = 780
TabIndex = 24
Text = "0"
Top = 1800
Width = 795
End
Begin VB.Label Label8
Caption = "Address"
Height = 345
Left = 120
TabIndex = 28
Top = 1320
Width = 615
End
Begin VB.Label Label7
Caption = "Value to write"
Height = 555
Left = 180
TabIndex = 26
Top = 1800
Width = 495
End
End
Begin VB.TextBox Text2
Height = 345
Left = 2220
TabIndex = 14
Text = "4"
Top = 840
Width = 495
End
Begin VB.TextBox Text16
Height = 285
Left = 2160
TabIndex = 13
Top = 1410
Width = 645
End
Begin VB.TextBox Text17
Height = 285
Left = 2160
TabIndex = 12
Top = 1800
Width = 645
End
Begin VB.TextBox Text18
Height = 285
Left = 2160
TabIndex = 11
Top = 2220
Width = 645
End
Begin VB.TextBox Text19
Height = 285
Left = 2160
TabIndex = 10
Top = 2640
Width = 645
End
Begin VB.TextBox Text5
Height = 345
Left = 2220
TabIndex = 5
Text = "40010"
Top = 450
Width = 1275
End
Begin VB.CommandButton CmdRead
BackColor = &H0000FF00&
Caption = "Read Device"
Height = 705
Left = 180
Style = 1 'Graphical
TabIndex = 4
Top = 480
Width = 945
End
Begin VB.Label Label6
Caption = "Value 4"
Height = 225
Index = 2
Left = 1440
TabIndex = 22
Top = 2700
Width = 645
End
Begin VB.Label Label6
Caption = "Value 3"
Height = 225
Index = 1
Left = 1440
TabIndex = 21
Top = 2280
Width = 645
End
Begin VB.Label Label6
Caption = "Value 2"
Height = 225
Index = 0
Left = 1440
TabIndex = 16
Top = 1860
Width = 645
End
Begin VB.Label Label5
Caption = "Quantity"
Height = 315
Left = 1380
TabIndex = 15
Top = 900
Width = 615
End
Begin VB.Label Label4
Caption = "Value 1"
Height = 255
Left = 1440
TabIndex = 9
Top = 1440
Width = 645
End
Begin VB.Label Label3
Caption = "Address"
Height = 345
Left = 1410
TabIndex = 8
Top = 510
Width = 615
End
Begin VB.Label Label1
Caption = "Label1"
Height = 315
Left = 3150
TabIndex = 7
Top = 3210
Width = 2160
End
Begin VB.Label Label2
Caption = "Label2"
Height = 465
Left = 3150
TabIndex = 6
Top = 3750
Width = 1920
End
End
End
Attribute VB_Name = "ModComSimple"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'Use this form as template for ModBus communications
'
'upon startup port is opened with form paramters, parameters can be set by
'setting ModCom properties in design time or runtime
Dim starttme As Double
Private Sub CmdRead_Click()
ReadPLCData
'ReadPLCAlternate
End Sub
Private Sub Command1_Click()
ModComTest.Show
End Sub
Private Sub Command13_Click()
'open comm port
ModCom1.PortOpen = False
End Sub
Private Sub Command2_Click()
If ModCom1.PortOpen = True Then
ModCom1.PortOpen = False
End If
End
End Sub
Private Sub Command3_Click()
'write data to PLC
Dim address As Variant
'Dim valu As Long 'value to write to PLC, to write multiple registers: make this an array
'Dim valu(10) As Long 'value to write to PLC, to write multiple registers: make this an array
Dim valu As Long
Dim resp As Integer
Dim Writesize As Integer
address = Text1
'valu(1) = Text4
'valu(2) = 2576
valu = Text4
Writesize = Text2
resp = ModCom1.WritePLC(address, valu, Writesize)
If resp <> True Then
Label1 = "Bad resp"
Else
End If
'Text1 = resp
End Sub
Private Sub Command8_Click()
'use this to set port
ModCom1.CommPort = Text10(0)
ModCom1.Settings = Text10(1)
ModCom1.PortOpen = True
End Sub
Private Sub Form_Load()
'use this to open ort upon form startup
ModCom1.CommPort = Text10(0)
ModCom1.Settings = Text10(1)
ModCom1.PortOpen = True
Dim b As Long
Dim lpFrequency As LARGE_INTEGER 'Long
b = QueryPerformanceFrequency(lpFrequency)
HSfreqValue = cnvLARGE_INTEGER(lpFrequency)
End Sub
Private Sub ModCom1_CommComplete(MData() As Long, MAdd As Long, quan As Integer)
'if ReadPLCNW is called and don't wait for response
'resp will trigger event ModCom1_CommComplete
'and place data in text3
' Text3 = MData(Madd)
Label2 = "Comm Complete"
End Sub
Private Sub ModCom1_CommCompleteEx(MData() As Variant, MAdd As Long, quan As Integer, ErrorStr As String, RespStatus As Integer)
Label2 = ErrorStr
End Sub
Private Sub ModCom1_CommEventModem(CommEvent As Integer, MData() As Variant, Connected As Boolean)
Dim ModemData As String
Dim ModemEvent As Integer
Dim MConnected As Boolean
ModemEvent = CommEvent
ModemData = MData(0)
ModComTest.Text1 = ModemData
MConnected = Connected
ModComTest.Text24 = "CDHolding(Carrier detect) has changed on comm port"
ModComTest.Text23 = ModCom1.CDHolding
End Sub
Private Sub ModSlv_Click()
ModSlvTst.Show
End Sub
Public Sub ReadPLCData()
Dim ReadValue(1 To 10) As Variant 'returned data from read in array
Dim resp As Integer
Dim address As Variant
Dim readsize As Integer
Dim iotype As Integer
Label1 = ""
Label2 = ""
'iotype = 2 '5=signed integer, 2 = float
' nhsc = QueryPerformanceCounter(lp2PerformanceCount)
' P1CValue = cnvLARGE_INTEGER(lp2PerformanceCount)
readsize = Text2 'Optional- Number registers to read, defaults to 1
address = Text5 'PLC reg address
'if CommBusy then control is processing, wait till complete
' before continuing
Do While ModCom1.CommBusy = True
DoEvents
Loop
'querry read plc data, on response from PLC, data willl
'be transfered to controls buffer
resp = ModCom1.ReadDevice(address, readsize)
'resp = ModCom1.ReadDevice(address, readsize, iotype)
'then wait for response,
Do While ModCom1.CommBusy = True
DoEvents
Loop
'see if there are any errors
If ModCom1.ReadErrors = "" Then
'finally get transfered data from controls buffer
Text16 = ModCom1.ReadData(1)
Text17 = ModCom1.ReadData(2)
Text18 = ModCom1.ReadData(3)
Text19 = ModCom1.ReadData(4)
Else
Label2 = ModCom1.ReadErrors '"bad read"
End If
' nhsc = QueryPerformanceCounter(lp2PerformanceCount)
' P2CValue = cnvLARGE_INTEGER(lp2PerformanceCount)
' dint1 = (P2CValue - P1CValue) / HSfreqValue
End Sub
Public Sub ReadPLCAlternate()
Dim ReadValue(1 To 10) As Variant 'returned data from read in array
Dim resp As Integer
Dim address As Variant 'Long
Dim readsize As Integer
Dim iotype As Integer
iotype = 2
readsize = Text2 'Optional- Number registers to read, defaults to 1
address = Text5 'PLC reg address
'ReadValue(1) = 9
'read plc and wait for response
resp = ModCom1.ReadPLC(address, ReadValue(), readsize, iotype)
Text16 = ReadValue(1)
Text17 = ReadValue(2)
Text18 = ReadValue(3)
Text19 = ReadValue(4)
If resp <> True Then
Label1 = "Bad resp"
Else
Label1 = "Good resp"
End If
End Sub
Private Sub Timer1_Timer()
Static cnt As Long
cnt = cnt + 1
Text3 = cnt
ReadPLCData
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -