发表于:2006/5/16 16:47:00
#0楼
小弟我是用VB6.0写的,连线的PLC是三菱Q系列的
用RS232连线.
MSComm设定是19200,E,7,2 交握协定是设定2-com RTSXonXoff
我最主要的需求就是将数值传送给寄存器D
以下是我的VB编程以及模组
Private Sub Command1_Click()
Call Word_Write(MSComm1, FindAddrWord("D151"), Text1.Text)
End Sub
Private Sub Command2_Click()
Call Word_Write(MSComm1, FindAddrWord("D231"), Text2.Text)
End Sub
Private Sub Command3_Click()
Call Word_Write(MSComm1, FindAddrWord("D151"), Text1.Text)
Call Word_Write(MSComm1, FindAddrWord("D231"), Text2.Text)
Call Word_Write(MSComm1, FindAddrWord("D141"), Text3.Text)
End Sub
Private Sub Command4_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
End Sub
Private Sub Command5_Click()
End
End Sub
Private Sub Command6_Click()
Call Word_Write(MSComm1, FindAddrWord("D141"), Text3.Text)
End Sub
Private Sub Timer1_Timer()
Text4.Text = Word_Read(MSComm1, FindAddrWord("D141"))
End Sub
模组1:
Public Function FindAddrWord(sName As String) As String
Dim strTagType As String
Dim strTmp As String
strTagType = Left$(sName, 1)
strTmp = Right$(sName, Len(sName) - 1)
Select Case UCase(strTagType)
Case "C"
FindAddrWord = "0" + Hex(Val(strTmp) * 2 + 2560)
Case "T"
FindAddrWord = "0" + Hex(Val(strTmp) * 2 + 2048)
Case "D"
FindAddrWord = Hex(Val(strTmp) * 2 + 4096)
End Select
Exit Function
End Function
模组2:
Public Function Word_Read(Comm As MSComm, sAddr As String) As String
Dim STX As String
Dim Cmd As String
Dim Addr As String
Dim ByteNum As String
Dim ETX As String
Dim CheckString As String
Dim CheckCode As String
Dim CmdCode As String
Dim Sum As Integer
Dim n As Integer
Dim tmp As String
STX = Chr(2)
Cmd = "0"
Addr = sAddr
ByteNum = "02"
ETX = Chr(3)
CheckString = Cmd + Addr + ByteNum + ETX
For n = 1 To Len(CheckString)
Sum = Sum + Asc(Mid$(CheckString, n, 1))
Next n
CheckCode = Right$(Hex(Sum), 2)
CmdCode = STX + Cmd + Addr + ByteNum + ETX + CheckCode
Comm.PortOpen = True
Comm.Output = CmdCode
Do While Len(tmp) < 8
tmp = tmp + Comm.Input
n = n + 1
If n >= 3000 Then
Comm.PortOpen = False
Exit Function
End If
Loop
tmp = Mid$(tmp, 2, 4)
Word_Read = Hex2Dec(Right$(tmp, 2) + Left$(tmp, 2))
Comm.PortOpen = False
End Function
Public Function Word_Write(Comm As MSComm, sAddr As String, sWriteData As Integer) As String
Dim STX As String
Dim Cmd As String
Dim Addr As String
Dim ByteNum As String
Dim WriteData As String
Dim ETX As String
Dim CheckString As String
Dim CheckCode As String
Dim CmdCode As String
Dim Sum As Integer
Dim n As Integer
Dim tmp As String
STX = Chr(2)
Cmd = "1"
Addr = sAddr
ByteNum = "02"
WriteData = Hex(sWriteData)
If Len(WriteData) < 4 Then
Do While Len(WriteData) < 4
WriteData = "0" + WriteData
Loop
Else
WriteData = Right$(WriteData, 4)
End If
WriteData = Right$(WriteData, 2) + Left$(WriteData, 2)
ETX = Chr(3)
CheckString = Cmd + Addr + ByteNum + WriteData + ETX
For n = 1 To Len(CheckString)
Sum = Sum + Asc(Mid$(CheckString, n, 1))
Next n
CheckCode = Right$(Hex(Sum), 2)
CmdCode = STX + Cmd + Addr + ByteNum + WriteData + ETX + CheckCode
Comm.PortOpen = True
Comm.Output = CmdCode
Do While Len(tmp) < 1
tmp = tmp + Comm.Input
n = n + 1
If n >= 3000 Then
Comm.PortOpen = False
Exit Function
End If
Loop
Word_Write = Hex(Asc(tmp))
Comm.PortOpen = False
End Function
模组3:
Public Function Hex2Dec(strHex As String) As Long
Dim strTmp As String
Dim longTmp As Long
Dim longDec As Long
Dim intLen As Integer
Dim n1 As Integer
intLen = Len(strHex)
For n1 = 1 To intLen
strTmp = Mid(strHex, n1, 1)
Select Case Asc(strTmp)
Case 48 To 57
longTmp = Val(strTmp)
Case 65 To 70
longTmp = Asc(strTmp) - 55
Case Else
Hex2Dec = 0
Exit Function
End Select
longDec = longDec + longTmp * 16 ^ (intLen - n1)
Next n1
Hex2Dec = longDec
End Function
用RS232连线.
MSComm设定是19200,E,7,2 交握协定是设定2-com RTSXonXoff
我最主要的需求就是将数值传送给寄存器D
以下是我的VB编程以及模组
Private Sub Command1_Click()
Call Word_Write(MSComm1, FindAddrWord("D151"), Text1.Text)
End Sub
Private Sub Command2_Click()
Call Word_Write(MSComm1, FindAddrWord("D231"), Text2.Text)
End Sub
Private Sub Command3_Click()
Call Word_Write(MSComm1, FindAddrWord("D151"), Text1.Text)
Call Word_Write(MSComm1, FindAddrWord("D231"), Text2.Text)
Call Word_Write(MSComm1, FindAddrWord("D141"), Text3.Text)
End Sub
Private Sub Command4_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
End Sub
Private Sub Command5_Click()
End
End Sub
Private Sub Command6_Click()
Call Word_Write(MSComm1, FindAddrWord("D141"), Text3.Text)
End Sub
Private Sub Timer1_Timer()
Text4.Text = Word_Read(MSComm1, FindAddrWord("D141"))
End Sub
模组1:
Public Function FindAddrWord(sName As String) As String
Dim strTagType As String
Dim strTmp As String
strTagType = Left$(sName, 1)
strTmp = Right$(sName, Len(sName) - 1)
Select Case UCase(strTagType)
Case "C"
FindAddrWord = "0" + Hex(Val(strTmp) * 2 + 2560)
Case "T"
FindAddrWord = "0" + Hex(Val(strTmp) * 2 + 2048)
Case "D"
FindAddrWord = Hex(Val(strTmp) * 2 + 4096)
End Select
Exit Function
End Function
模组2:
Public Function Word_Read(Comm As MSComm, sAddr As String) As String
Dim STX As String
Dim Cmd As String
Dim Addr As String
Dim ByteNum As String
Dim ETX As String
Dim CheckString As String
Dim CheckCode As String
Dim CmdCode As String
Dim Sum As Integer
Dim n As Integer
Dim tmp As String
STX = Chr(2)
Cmd = "0"
Addr = sAddr
ByteNum = "02"
ETX = Chr(3)
CheckString = Cmd + Addr + ByteNum + ETX
For n = 1 To Len(CheckString)
Sum = Sum + Asc(Mid$(CheckString, n, 1))
Next n
CheckCode = Right$(Hex(Sum), 2)
CmdCode = STX + Cmd + Addr + ByteNum + ETX + CheckCode
Comm.PortOpen = True
Comm.Output = CmdCode
Do While Len(tmp) < 8
tmp = tmp + Comm.Input
n = n + 1
If n >= 3000 Then
Comm.PortOpen = False
Exit Function
End If
Loop
tmp = Mid$(tmp, 2, 4)
Word_Read = Hex2Dec(Right$(tmp, 2) + Left$(tmp, 2))
Comm.PortOpen = False
End Function
Public Function Word_Write(Comm As MSComm, sAddr As String, sWriteData As Integer) As String
Dim STX As String
Dim Cmd As String
Dim Addr As String
Dim ByteNum As String
Dim WriteData As String
Dim ETX As String
Dim CheckString As String
Dim CheckCode As String
Dim CmdCode As String
Dim Sum As Integer
Dim n As Integer
Dim tmp As String
STX = Chr(2)
Cmd = "1"
Addr = sAddr
ByteNum = "02"
WriteData = Hex(sWriteData)
If Len(WriteData) < 4 Then
Do While Len(WriteData) < 4
WriteData = "0" + WriteData
Loop
Else
WriteData = Right$(WriteData, 4)
End If
WriteData = Right$(WriteData, 2) + Left$(WriteData, 2)
ETX = Chr(3)
CheckString = Cmd + Addr + ByteNum + WriteData + ETX
For n = 1 To Len(CheckString)
Sum = Sum + Asc(Mid$(CheckString, n, 1))
Next n
CheckCode = Right$(Hex(Sum), 2)
CmdCode = STX + Cmd + Addr + ByteNum + WriteData + ETX + CheckCode
Comm.PortOpen = True
Comm.Output = CmdCode
Do While Len(tmp) < 1
tmp = tmp + Comm.Input
n = n + 1
If n >= 3000 Then
Comm.PortOpen = False
Exit Function
End If
Loop
Word_Write = Hex(Asc(tmp))
Comm.PortOpen = False
End Function
模组3:
Public Function Hex2Dec(strHex As String) As Long
Dim strTmp As String
Dim longTmp As Long
Dim longDec As Long
Dim intLen As Integer
Dim n1 As Integer
intLen = Len(strHex)
For n1 = 1 To intLen
strTmp = Mid(strHex, n1, 1)
Select Case Asc(strTmp)
Case 48 To 57
longTmp = Val(strTmp)
Case 65 To 70
longTmp = Asc(strTmp) - 55
Case Else
Hex2Dec = 0
Exit Function
End Select
longDec = longDec + longTmp * 16 ^ (intLen - n1)
Next n1
Hex2Dec = longDec
End Function