?? frmmain.frm
字號:
EndProperty
Height = 600
Left = 4860
TabIndex = 0
Top = 2340
Width = 1590
End
Begin MSCommLib.MSComm MSComm1
Left = 45
Top = 945
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
Begin VB.Image imgDisable
Height = 735
Left = 3420
Picture = "frmMain.frx":0000
Stretch = -1 'True
Top = 360
Visible = 0 'False
Width = 780
End
Begin VB.Label lblMsg
BackColor = &H00C0FFFF&
BorderStyle = 1 'Fixed Single
Caption = "Label2"
BeginProperty Font
Name = "宋體"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 420
Left = 315
TabIndex = 7
Top = 4995
Width = 5595
End
Begin VB.Label Label1
Caption = "站號"
BeginProperty Font
Name = "宋體"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 330
Index = 6
Left = 2025
TabIndex = 5
Top = 120
Width = 780
End
Begin VB.Image imgOFF
Height = 735
Left = 3870
Picture = "frmMain.frx":1992
Stretch = -1 'True
Top = 0
Visible = 0 'False
Width = 780
End
Begin VB.Image imgON
Height = 735
Left = 4500
Picture = "frmMain.frx":36CA
Stretch = -1 'True
Top = 270
Visible = 0 'False
Width = 780
End
Begin VB.Label Label1
Caption = "通信端口"
BeginProperty Font
Name = "宋體"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 330
Index = 0
Left = 450
TabIndex = 1
Top = 135
Width = 1005
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'當選擇通信端口的Combo控件被選中后啟動此事件
'若使用者改變通信端口時,關閉通信端口
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmbCOM_Click()
'若通信端口號碼和現在的選擇一樣時就不處理,直接跳出此子程序
If cmbCOM.ListIndex + 1 = MSComm1.CommPort Then Exit Sub
TimeDelay 100
MSComm1.PortOpen = False '關閉通信端口
lblMsg.Caption = "已停止檢測并關閉通信端口"
cmdOpenCOM.Enabled = True '允許使用【打開通信端口】按鈕
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'單擊【結束】按鈕后啟動此事件
'使用End命令將系統結束
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdEnd_Click()
End
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'單擊【打開通信端口】按鈕后啟動此事件
'將MSComm控件的參數設置好,并打開
'啟用【開始檢測】按鈕
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdOpenCOM_Click()
'判斷端口號碼是否落在1--16之間
If cmbCOM.ListIndex >= 0 And cmbCOM.ListIndex <= 16 Then
MSComm1.CommPort = cmbCOM.ListIndex + 1
Else
MsgBox "指定通信端口時發生錯誤!", vbCritical + vbOKOnly, "系統信息"
Exit Sub
End If
'觸發錯誤檢測機制
On Error GoTo comErr
MSComm1.Settings = "9600,n,8,1" '設置通信參數
MSComm1.PortOpen = True '打開通信端口
cmdOpenCOM.Enabled = False '將此按鈕設為禁用狀態
cmdStart.Enabled = True '激活【開始檢測】按鈕
lblMsg.Caption = "可單擊【開始檢測】按鈕,執行檢測的工作。"
Exit Sub
comErr:
MsgBox "打開通信端口時發生錯誤!請確定通信端口存在且正常。", vbCritical + vbOKOnly, "系統訊息"
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'單擊【開始檢測】按鈕后啟動此事件
'將定時器啟動或關閉,并顯示對應的文字在按鈕上,以指示用戶操作
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdStart_Click()
If cmdStart.Caption = "開始檢測" Then
cmdStart.Caption = "停止檢測"
Else
cmdStart.Caption = "開始檢測"
End If
Do
GetDIOFrom7060D '取數據的子程序
DoEvents
Loop Until cmdStart.Caption = "開始檢測"
lblMsg.Caption = "已停止檢測"
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'窗體的Load事件
'輸入圖形暫時設為灰色,表示無狀態信息進入
'將通信端口號碼及站號輸入Combo控件;并默認二者的選項是第一個
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_Load()
Dim i%
For i = 0 To 3
imgIN(i).Picture = imgDisable.Picture
Next i
cmbNO.Clear
For i = 1 To 255
cmbNO.AddItem CStr(Hex(i))
Next i
cmbNO.ListIndex = 0
cmbCOM.Clear
cmbCOM.AddItem "COM1"
cmbCOM.AddItem "COM2"
cmbCOM.ListIndex = 0
cmdStart.Enabled = False
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'狀態檢測子程序,所得的字符送到這里作對應位的判斷
'這里將字符和16種可能的情形作比較,再顯示到信號燈上去
'當狀態為ON時則顯示紅色信號燈,而為OFF時顯示綠色信號燈
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CheckDIStatus(DIBuf As String)
If Len(DIBuf) > 1 Then DIBuf = Right(DIBuf, 1)
'可能的字符由"0"~"F",一一作比較。特別注意,A~F的部分一定是大寫,
'手冊上的規定是大寫而不是小寫。
Select Case DIBuf
Case "0"
imgIN(0).Picture = imgON.Picture
imgIN(1).Picture = imgON.Picture
imgIN(2).Picture = imgON.Picture
imgIN(3).Picture = imgON.Picture
Case "1"
imgIN(0).Picture = imgOFF.Picture
imgIN(1).Picture = imgON.Picture
imgIN(2).Picture = imgON.Picture
imgIN(3).Picture = imgON.Picture
Case "2"
imgIN(0).Picture = imgON.Picture
imgIN(1).Picture = imgOFF.Picture
imgIN(2).Picture = imgON.Picture
imgIN(3).Picture = imgON.Picture
Case "3"
imgIN(0).Picture = imgOFF.Picture
imgIN(1).Picture = imgOFF.Picture
imgIN(2).Picture = imgON.Picture
imgIN(3).Picture = imgON.Picture
Case "4"
imgIN(0).Picture = imgON.Picture
imgIN(1).Picture = imgON.Picture
imgIN(2).Picture = imgOFF.Picture
imgIN(3).Picture = imgON.Picture
Case "5"
imgIN(0).Picture = imgOFF.Picture
imgIN(1).Picture = imgON.Picture
imgIN(2).Picture = imgOFF.Picture
imgIN(3).Picture = imgON.Picture
Case "6"
imgIN(0).Picture = imgON.Picture
imgIN(1).Picture = imgOFF.Picture
imgIN(2).Picture = imgOFF.Picture
imgIN(3).Picture = imgON.Picture
Case "7"
imgIN(0).Picture = imgOFF.Picture
imgIN(1).Picture = imgOFF.Picture
imgIN(2).Picture = imgOFF.Picture
imgIN(3).Picture = imgON.Picture
Case "8"
imgIN(0).Picture = imgON.Picture
imgIN(1).Picture = imgON.Picture
imgIN(2).Picture = imgON.Picture
imgIN(3).Picture = imgOFF.Picture
Case "9"
imgIN(0).Picture = imgOFF.Picture
imgIN(1).Picture = imgON.Picture
imgIN(2).Picture = imgON.Picture
imgIN(3).Picture = imgOFF.Picture
Case "A"
imgIN(0).Picture = imgON.Picture
imgIN(1).Picture = imgOFF.Picture
imgIN(2).Picture = imgON.Picture
imgIN(3).Picture = imgOFF.Picture
Case "B"
imgIN(0).Picture = imgOFF.Picture
imgIN(1).Picture = imgOFF.Picture
imgIN(2).Picture = imgON.Picture
imgIN(3).Picture = imgOFF.Picture
Case "C"
imgIN(0).Picture = imgON.Picture
imgIN(1).Picture = imgON.Picture
imgIN(2).Picture = imgOFF.Picture
imgIN(3).Picture = imgOFF.Picture
Case "D"
imgIN(0).Picture = imgOFF.Picture
imgIN(1).Picture = imgON.Picture
imgIN(2).Picture = imgOFF.Picture
imgIN(3).Picture = imgOFF.Picture
Case "E"
imgIN(0).Picture = imgON.Picture
imgIN(1).Picture = imgOFF.Picture
imgIN(2).Picture = imgOFF.Picture
imgIN(3).Picture = imgOFF.Picture
Case "F"
imgIN(0).Picture = imgOFF.Picture
imgIN(1).Picture = imgOFF.Picture
imgIN(2).Picture = imgOFF.Picture
imgIN(3).Picture = imgOFF.Picture
End Select
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'由7060D取得數字輸入狀態子程序,要求命令由此子程序下達
'取得字符串后的比較由另一個子程序負責
'并將輸入結果轉送至繼電器
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub GetDIOFrom7060D()
Dim ChBuf$, Buf$, Buf1$, Buf2$, i%
ChBuf = cmbNO.List(cmbNO.ListIndex)
'若只有一位數,則在此位數的前端加上一個0
If Len(ChBuf) = 1 Then
ChBuf = "0" & ChBuf
End If
MSComm1.Output = "@" & ChBuf & Chr(13) '組合完整的命令字符串
lblMsg.Caption = "檢測" & ChBuf & "中…"
'等待數據返回
Buf = WaitRS(MSComm1, Chr(13), 1000)
If Len(Buf) < 4 Then
'若返回字符串不正確,則顯示灰色信號燈及信息
lblMsg.Caption = "未返回正確信息。"
For i = 0 To 3
imgIN(i).Picture = imgDisable.Picture
imgOut(i).Picture = imgDisable.Picture
Next i
Exit Sub
End If
Buf1 = Mid(Buf, 3, 1) '取出代表數字輸出狀態的字符
CheckDOStatus Buf1 '送入數字輸出狀態檢測子程序
Buf2 = Mid(Buf, 5, 1) '取出代表數字輸入狀態的字符
CheckDIStatus Buf2 '送入狀態檢測子程序
SendRelayOut ChBuf, Buf2 '將檢測的結果轉送至繼電器輸出
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'當數字輸入的結果得到后,轉送至繼電器輸出的子程序在此
'此子程序作位運算,程序較短
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub SendRelayOut(ChBuf As String, RLOut As String)
Dim Buf$, i%
'數字輸出命令
MSComm1.Output = "@" & ChBuf & RLOut & Chr(13)
Buf = WaitRS(MSComm1, vbCr, 1000) '等待返回值
End Sub
Sub CheckDOStatus(Buf As String)
'每一個位均運算后,顯示對應的圖形。
'其中使用IIF作二種情形的判斷
Dim i%
For i = 0 To 3
imgOut(i).Picture = IIf(Val("&H" & Buf) And 2 ^ i, imgOFF.Picture, imgON.Picture)
Next i
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -