您现在所在的是:

PLC论坛

回帖:5个,阅读:951 [上一页] [1] [下一页]
921
oops1008
文章数:6
年度积分:50
历史总积分:921
注册时间:2006/5/16
发站内信
发表于: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
906
jeffrey1224
文章数:5
年度积分:50
历史总积分:906
注册时间:2006/5/16
发站内信
发表于:2006/5/16 17:41:00
#1楼
呵呵  这个不太在行
2826
流客
文章数:794
年度积分:50
历史总积分:2826
注册时间:2005/8/28
发站内信
发表于:2006/5/16 20:23:00
#2楼
没耐心看下去。
如果是D,你的地址算法是:
FindAddrWord = Hex(Val(strTmp) * 2 + 4096)

你是通过什么通讯?编程口还是通讯板?
声明:我不了解Q系列的通讯协议。是与FX系列一样吗?
921
oops1008
文章数:6
年度积分:50
历史总积分:921
注册时间:2006/5/16
发站内信
发表于:2006/5/16 21:15:00
#3楼
我是通过RS232通讯由编程口送出资料
Q系列跟FX不一样因为目前FX的资料很多很齐全
相对的用VB来控制Q系列PLC的就很少FX系列的通讯协定
应该是包率9600同位检查E资料位7停止位1
而Q系列则是19200 ,E,7,2
小弟查到的资料是这样子的
2826
流客
文章数:794
年度积分:50
历史总积分:2826
注册时间:2005/8/28
发站内信
发表于:2006/5/26 16:48:00
#4楼
关键是各元件的地址算法.Q系列是如何规定的?
1028
jianing
文章数:53
年度积分:50
历史总积分:1028
注册时间:2005/9/19
发站内信
发表于:2006/5/27 12:08:00
#5楼
运行有什么问题?

关于我们 | 联系我们 | 广告服务 | 本站动态 | 友情链接 | 法律声明 | 非法和不良信息举报

工控网客服热线:0755-86369299
版权所有 工控网 Copyright©2024 Gkong.com, All Rights Reserved

62.4004