📄 frmmainshow.frm
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form frmMainShow
Caption = "Form1"
ClientHeight = 6450
ClientLeft = 60
ClientTop = 345
ClientWidth = 8145
LinkTopic = "Form1"
ScaleHeight = 6450
ScaleWidth = 8145
StartUpPosition = 3 '窗口缺省
Begin VB.Timer Timer1
Enabled = 0 'False
Interval = 100
Left = 480
Top = 4080
End
Begin VB.CommandButton Command3
Caption = "STOP"
Height = 375
Left = 3840
TabIndex = 3
Top = 5760
Width = 1335
End
Begin VB.CommandButton Command2
Caption = "END"
Height = 375
Left = 6240
TabIndex = 2
Top = 5760
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "START"
Height = 375
Left = 1800
TabIndex = 1
Top = 5760
Width = 1455
End
Begin VB.TextBox Text1
Height = 975
Left = 480
MultiLine = -1 'True
TabIndex = 0
Text = "frmMainShow.frx":0000
Top = 1680
Width = 6975
End
Begin VB.Timer Timer4
Left = 480
Top = 3120
End
Begin MSCommLib.MSComm MSComm1
Left = 3720
Top = 4320
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
InputMode = 1
End
Begin VB.Label Label1
BackStyle = 0 'Transparent
Caption = "采用SNPX协议实现GE90-30PLC与上位机通信:19200,O,8,1;SNPID:ABCDEF"
BeginProperty Font
Name = "宋体"
Size = 15.75
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00C00000&
Height = 735
Left = 600
TabIndex = 4
Top = 480
Width = 6855
End
End
Attribute VB_Name = "frmMainShow"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim TimeOut4Flag As Boolean
Dim FirstTime As Integer
Dim ReadNumber As Integer
Dim RealStrings(30) As String
Dim CurCount As Integer
Dim Outflag As Boolean
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Command3_Click()
Timer1.Enabled = False
End Sub
Private Sub Form_Load()
ReadNumber = 30
Call InitComPort
End Sub
Public Sub InitComPort()
frmMainShow.MSComm1.CommPort = 1
frmMainShow.MSComm1.Settings = "19200,O,8,1"
frmMainShow.MSComm1.RThreshold = 23
frmMainShow.MSComm1.InputLen = 0
frmMainShow.MSComm1.InBufferCount = 0
frmMainShow.MSComm1.OutBufferCount = 0
frmMainShow.MSComm1.InputMode = comInputModeBinary
frmMainShow.MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim aa() As Byte
Dim bb() As Byte
Dim j As Integer
Dim GetString As String
Select Case MSComm1.CommEvent
Case comEvReceive
aa = MSComm1.Input
Call Byte2Str(aa, GetString)
Text1.Text = GetString
End Select
End Sub
Public Sub ReadInputStatus()
Dim OutString As String
Dim SendDataByte() As Byte
'OutString = "1B58" & "4142434445460000" & "0000000000000000" & "1700000000B2" 'attach 必需先发一次,建立连接后在发以下命令
OutString = "1B58" & "4142434445460000" & "0108000004000000" & "17000000001A"
Call Str2Byte(OutString, SendDataByte)
frmMainShow.MSComm1.Output = SendDataByte
End Sub
Private Sub Timer1_Timer()
Call ReadInputStatus
End Sub
Private Sub Timer4_Timer()
TimeOut4Flag = True
End Sub
Public Sub Str2Byte(ByVal strData As String, ByRef ByteData() As Byte)
Dim i, j, k As Byte
Dim tempstr As String
Dim Length As Long
Length = Len(strData)
ReDim ByteData(1 To Length / 2)
i = 2
j = 1
While strData <> ""
tempstr = Left(strData, 2) '默认为高4位在前,例如:"7E"=7*16+E=126
k = AscB(Left(tempstr, 1))
If k >= 48 And k <= 57 Then
k = k - 48
ElseIf k >= 65 And k <= 70 Then
k = k - 65 + 10
End If
ByteData(j) = k * 16
tempstr = Right(tempstr, 1)
k = AscB(Left(tempstr, 1))
If k >= 48 And k <= 57 Then
k = k - 48
ElseIf k >= 65 And k <= 70 Then
k = k - 65 + 10
End If
ByteData(j) = ByteData(j) + k
strData = Right(strData, Length - i)
i = i + 2
j = j + 1
Wend
End Sub
Public Sub Byte2Str(ByRef ByteData() As Byte, ByRef strData As String)
Dim i As Long
Dim hex_in
strData = ""
For i = LBound(ByteData) To UBound(ByteData)
hex_in = Hex(ByteData(i)) '例如:126=7E->"7E"
If ByteData(i) < 16 Then
strData = strData & "0" '0--F 的值转化为字符时在前头补'0'
End If
strData = strData & CStr(hex_in)
Next i
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -