?? ccm2.frm
字號(hào):
Width = 615
End
Begin VB.Label Label1
Caption = "R"
Height = 375
Left = 600
TabIndex = 8
Top = 480
Width = 495
End
End
Begin VB.Label Label5
Caption = "CCM2通訊例子"
BeginProperty Font
Name = "宋體"
Size = 21.75
Charset = 134
Weight = 700
Underline = 0 'False
Italic = -1 'True
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 6000
TabIndex = 14
Top = 4920
Width = 3255
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'
'
'本程序是一個(gè)通過(guò)CCM2協(xié)議來(lái)和光洋PLC通訊的例子。要了解CCM2協(xié)議的詳細(xì)信息可以到光洋電子(無(wú)錫)的主頁(yè)上去下載。
'地址為http://www.koyoele.com.cn/kew3/download/DM%C4%A3%BF%E9CCM%D0%AD%D2%E9/ccm2.pdf
'本程序演示了讀、寫(xiě)寄存器的操作,讀、寫(xiě)單個(gè)線圈Q的操作。
'本程序調(diào)試時(shí)用的是計(jì)算機(jī)的COM3,若您使用的計(jì)算機(jī)端口不是COM3,請(qǐng)到Private Sub Form_Load()里修改相應(yīng)的參數(shù)。
'再使用本程序之前還要把PLC的CCM通訊方式設(shè)定成ASCII方式。因?yàn)楸境绦蚴腔贏SCII方式編寫(xiě)的。其他通訊參數(shù)的設(shè)置應(yīng)
'和本程序中的Private Sub Form_Load()里的通訊參數(shù)設(shè)置一致。
'
'
'
'
'
Public comm_fg As Boolean
Dim w_fg, r_fg, rq_fg, setq_fg, resetq_fg As Boolean
'以上分別是寫(xiě)寄存器標(biāo)志,讀寄存器標(biāo)志,讀q狀態(tài)標(biāo)志,setQ標(biāo)志,reset Q標(biāo)志
Dim sentWC, sentWI As Boolean
Dim sentQRA, sentQRC, sentSQC, sentRSQC As Boolean
Public r_station, RA, RC, RD, RE, RF, RQA, RQC As String
Public w_station, WA, WB, WC, WD, WE, WF, WI As String
Public SQA, SQC, SQI As String
Public RSQA, RSQC, RSQI As String
Public t3 As String
Public w3 As String
Dim data() As Byte '通訊空件接收到的數(shù)據(jù)
Dim ct As Integer
Dim q0byte As String
'wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Private Sub Command1_Click() '寫(xiě)入按鈕
Dim temp1 As String
WA = "4E" & LTrim(Str(Hex(32 + Val("&H" & w_station)))) & "05"
WC = ToAsc(w_station & "81" & w3 & "0004") & "3031"
temp1 = Lrc((WC)) '注意:自變量要加個(gè)()?。?!
WC = "01" & WC & "17" & ToAsc((temp1))
WI = WI
w_fg = True
End Sub
'wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Private Sub Command4_Click() '讀取按鈕
Dim temp1 As String
RA = "4E" & LTrim(Str(Hex(32 + Val("&H" & r_station)))) & "05"
RC = ToAsc(r_station & "01" & t3 & "0004") & "3031"
temp1 = Lrc((RC)) '注意:自變量要加個(gè)()!??!
RC = "01" & RC & "17" & ToAsc((temp1))
r_fg = True
End Sub
'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Private Sub Command5_Click() 'SET Q 按鈕
Dim n As Integer
Dim temp1, key As String
n = Val(Text8.Text)
Select Case n
Case 0
n = 1
Case 1
n = 2
Case 2
n = 4
Case 3
n = 8
Case 4
n = 16
Case 5
n = 32
Case 6
n = 64
Case 7
n = 128
End Select
key = Right("00" & Hex(Val("&H" & q0byte) Or n), 2)
'由于CCM2協(xié)議沒(méi)有針對(duì)單個(gè)線圈操作的功能碼,因此若要對(duì)單個(gè)線圈進(jìn)行寫(xiě)操作,必須先讀一組Q的狀態(tài),再通過(guò)一定的算法把
'結(jié)果重新寫(xiě)入到原來(lái)的那組線圈。例如,要set Q0:把Q0~Q7的狀態(tài)作為一個(gè)2位十六進(jìn)制數(shù)或(邏輯或)上“1”再重新寫(xiě)入
'Q0~Q7。
SQA = "4E" & LTrim(Str(Hex(32 + Val("&H" & "1")))) & "05" '默認(rèn)子局是1號(hào)局
SQC = ToAsc("01" & "83" & "01010002") & "3031"
temp1 = Lrc((SQC)) '注意:自變量要加個(gè)()?。?!
SQC = "01" & SQC & "17" & ToAsc((temp1))
SQI = "02" & ToAsc(key) & "03" & ToAsc(Lrc(ToAsc(key)))
setq_fg = True
End Sub
Private Sub Command6_Click() 'RESET Q 按鈕
Dim n As Integer
Dim temp1, key As String
n = Val(Text8.Text)
Select Case n
Case 0
n = 1
Case 1
n = 2
Case 2
n = 4
Case 3
n = 8
Case 4
n = 16
Case 5
n = 32
Case 6
n = 64
Case 7
n = 128
End Select
key = Right("00" & Hex(Val("&H" & q0byte) And (255 - n)), 2)
RSQA = "4E" & LTrim(Str(Hex(32 + Val("&H" & "1")))) & "05" '默認(rèn)子局是1號(hào)局
RSQC = ToAsc("01" & "83" & "01010002") & "3031"
temp1 = Lrc((RSQC)) '注意:自變量要加個(gè)()!?。?RSQC = "01" & RSQC & "17" & ToAsc((temp1))
RSQI = "02" & ToAsc(key) & "03" & ToAsc(Lrc(ToAsc(key)))
resetq_fg = True
End Sub
'********************************************************************************************************
'*
'*
Private Sub MSComm1_OnComm() '接收到數(shù)據(jù)?。。。。?!以后
Dim ArriveStr, cc As String
Dim i As Integer
If MSComm1.CommEvent = comEvReceive Then '接收到數(shù)據(jù)?。。。。?!以后
data = MSComm1.Input
ct = UBound(data)
For i = 0 To ct
'Text6.Text = Text6.Text & Str(i) & ")" & Hex(data(i)) & ";" '顯示通訊過(guò)程中的數(shù)據(jù),可以方便調(diào)試
Next
If r_fg = True Then '(1)如果是讀寄存器操作的請(qǐng)求
If ct = 3 Then
If data(0) = &H4E And data(1) = &H21 And data(2) = &H6 Then '如果讀到了ACK信號(hào)
Call sendRC
End If
End If
If ct = 7 Then
If data(0) = &H6 And data(1) = &HD Then '顯示寄存器數(shù)值?。。? Text4.Text = Chr(data(5)) & Chr(data(6)) & Chr(data(3)) & Chr(data(4))
End If
End If
If ct = 2 Then
If data(2) = &HD Then Call sendRJ '發(fā)送應(yīng)答ack
End If
If ct = 1 Then
If data(0) = 4 And data(1) = &HD Then
Call sendRK
r_fg = False '讀寄存器標(biāo)志復(fù)位
comm_fg = False '通訊標(biāo)志復(fù)位
End If
End If
End If
If w_fg = True Then '(2)如果是寫(xiě)寄存器操作的請(qǐng)求
If ct = 3 Then
If data(0) = &H4E And data(1) = &H21 And data(2) = &H6 Then
Call sendWC
End If
End If
If sentWC = True And ct = 1 Then
If data(0) = &H6 And data(1) = &HD Then
Call sendWI
sentWC = False
End If
w_fg = False '寫(xiě)寄存器標(biāo)志復(fù)位
comm_fg = False '通訊標(biāo)志復(fù)位
End If
If sentWI = True And ct = 1 Then '
If data(0) = &H6 And data(1) = &HD Then
Call sendRK '<-------
w_fg = False
sentWI = False
comm_fg = False
MsgBox " 寫(xiě)入成功!"
End If
End If
End If
If rq_fg = True Then '(3)如果是讀Q點(diǎn)的操作的請(qǐng)求
If ct = 3 Then
If data(2) = &H6 And data(3) = &HD Then
Call sendQRC
End If
End If
If ct = 7 Then
If data(0) = &H6 And data(1) = &HD Then
q0byte = Chr(data(3)) & Chr(data(4))
Call sendRK
Call checkq
Else
MsgBox "子局沒(méi)有正確應(yīng)答!"
End If
rq_fg = False '讀Q標(biāo)志復(fù)位
comm_fg = False '通訊標(biāo)志復(fù)位
End If
End If
If setq_fg Then '(4)如果set Q點(diǎn)的操作的請(qǐng)求
If ct = 3 Then
If data(2) = &H6 And data(3) = &HD Then
Call sendSQC
End If
End If
If sentSQC = True Then
If sentSQC = True And ct = 1 Then
If data(0) = &H6 And data(1) = &HD Then
SQI = SQI
Call sendSQI
sentSQC = False
End If
setq_fg = False 'set Q標(biāo)志復(fù)位
comm_fg = False '通訊標(biāo)志復(fù)位
End If
End If
End If
If resetq_fg Then '(4)如果reset Q點(diǎn)的操作的請(qǐng)求
If ct = 3 Then
If data(2) = &H6 And data(3) = &HD Then
Call sendRSQC
End If
End If
If sentRSQC = True Then
If sentRSQC = True And ct = 1 Then
If data(0) = &H6 And data(1) = &HD Then
Call sendRSQI
sentRSQC = False
End If
resetq_fg = False 'reset Q標(biāo)志復(fù)位
comm_fg = False '通訊標(biāo)志復(fù)位
End If
End If
End If
If data(0) = &H15 Then MsgBox "子局沒(méi)有正確應(yīng)答!" '若通訊出錯(cuò),則產(chǎn)生提示
'Text6.Text = Text6.Text & vbCrLf
End If
End Sub
'*
'*
'********************************************************************************************************
Private Sub Text1_LostFocus() '檢查寫(xiě)入寄存器定義號(hào)
Dim L, i As Integer
w3 = Text1.Text
L = Len(w3)
For i = 1 To L
If Mid(w3, i, 1) <> 0 And Mid(w3, i, 1) <> 1 And Mid(w3, i, 1) <> 2 And Mid(w3, i, 1) <> 3 And Mid(w3, i, 1) <> 4 And Mid(w3, i, 1) <> 5 And Mid(w3, i, 1) <> 6 And Mid(w3, i, 1) <> 7 Then
MsgBox "請(qǐng)檢查寄存器定義號(hào)!"
Exit Sub
End If
Next
w3 = Hex(Val("&o" & w3) + 1) '(1)oct-->hex
If Len(w3) = 1 Then w3 = "000" & w3
If Len(w3) = 2 Then w3 = "00" & w3
If Len(w3) = 3 Then w3 = "0" & w3
End Sub
Private Sub Combo1_LostFocus() ' 檢查寫(xiě)入局號(hào)
w_station = Combo1.Text
w_station = Hex(Val(w_station))
If Len(w_station) = 1 Then
w_station = "0" & w_station
End If
End Sub
Private Sub Text2_LostFocus() '檢查寫(xiě)入數(shù)據(jù)
Dim i, lenth As Integer
Dim aaa, temp As String
aaa = Text2.Text
lenth = Len(aaa)
Select Case lenth
Case 0
MsgBox "請(qǐng)?zhí)钊胍獙?xiě)入的數(shù)據(jù)!"
Exit Sub
Case 1
aaa = "000" & aaa
Case 2
aaa = "00" & aaa
Case 3
aaa = "0" & aaa
Case 4
aaa = aaa
Case Else
MsgBox "請(qǐng)檢查寫(xiě)入的數(shù)據(jù)是否合法!"
Exit Sub
End Select
aaa = Mid(aaa, 3, 2) & Mid(aaa, 1, 2)
For i = 1 To 4
If Not ((Asc(Mid(aaa, i, 1)) > 47 And Asc(Mid(aaa, i, 1)) < 58) Or (Asc(Mid(aaa, i, 1)) > 64 And Asc(Mid(aaa, i, 1)) < 71) Or (Asc(Mid(aaa, i, 1)) > 96 And Asc(Mid(aaa, i, 1)) < 103)) Then
MsgBox "請(qǐng)檢查寫(xiě)入的數(shù)據(jù)是否合法!"
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -