PLC论坛-全力打造可编程控制器专业技术论坛

 找回密码
 注册哦

QQ登录

只需一步,快速开始

微信扫码登录

查看: 1418|回复: 5

[西门子] 为什么我读D地址不成功?

[复制链接]
发表于 2010-5-7 14:58:00 | 显示全部楼层 |阅读模式
我用的是三菱FX1N系列的PLC,用VB编写下面这段程序,但是不能成功读取D地址的数据,请教高手,能否帮我调试已下。。。 Private Sub Command8_Click() Dim b(7) As Integer If Read_PLC(b, 0, 7) = True Then Print "ok" Else Print "?" For i = 0 To 7 Print b(i) Next End Sub '*************************************** '*读取PLC '*Read_PLC(mData(),mStart,mCounts) '*mData() 读出的数据--以数组的形式表示 '*mSstar 起始读的地址位 '*mCounts 读几位地址数据 '*************************************** Public Function Read_PLC(mData() As Integer, mStart As Integer, mCounts As Integer) As Boolean Dim myHex As String, Q As Integer, myTest As String On Error GoTo ReadErr Q = mStart * 2 + &H1000 '地址转换 mCounts = mCounts * 2 ReDim mData(mCounts / 2 - 1) '定义数组的数据个数 myHex = "0" & Hex(Q) & Check_Hex(mCounts) & Chr(3) myHex = Chr(2) & myHex & Check_FCS(myHex) Form1.MSComm1.Output = myHex tim = Timer '返回一个 Single,代表从午夜开始到现在经过的秒数 Do If Timer > tim + 1 Then MsgBox "与PLC没有连接!": Exit Function Loop Until MSComm1.InBufferCount = 1 myTest = Form1.MSComm1.Input 'Debug.Print myHex, myTest '检查是否传送有错误 If Len(myTest) < 4 + 2 * mCounts Then GoTo ReadErr If Right(myTest, 2) <> Check_FCS(Mid(myTest, 2, 2 * mCounts + 1)) Then GoTo ReadErr Read_PLC = True For Q = 0 To (mCounts \ 2) - 1 mData(Q) = "&H" & (Mid(myTest, 4 + Q * 4, 2) & Mid(myTest, 2 + Q * 4, 2)) '把读到的数据写进数组 Next Exit Function ReadErr: Read_PLC = False End Function
回复

使用道具 举报

发表于 2010-5-7 16:31:00 | 显示全部楼层
Dim QV As String Dim i, j, iB As Integer Dim TempRcv As String Dim TempRcv_(1 To 9) As String Dim TempRcv_4(1 To 9) As String Dim Over As Integer Debug.Print "--------receive START-----------------" Dim ComCls As CLS Dim Now1 Set ComCls = New CLS Now1 = Format(Now(), "yymmddhhmmss") \'QV = CStr(ComCls.onCommRevPLC("00", "FF", "S", "0550", "1")) main_Form.MSComm1.Output = Chr$(5) + "00FFBRAS055001" Do DoEvents timedelay 200 Loop While main_Form.MSComm1.InBufferCount <= 4 QV = main_Form.MSComm1.Input Set ComCls = Nothing \'delay_mscom Set ComCls = New CLS Now1 = Format(Now(), "yymmddhhmmss") main_Form.MSComm1.Output = Chr$(5) + "00FFWRAD060009" Do DoEvents timedelay 200 \'If Then Exit Do Loop While main_Form.MSComm1.InBufferCount <= 4 TempRcv = CStr(main_Form.MSComm1.Input) TempRcv = Mid$(TempRcv, 6, Val("&H" & "9") * 4) Set ComCls = Nothing Debug.Print TempRcv Debug.Print "--------receive END-----------------" 这是一段完整的读写程序,你看看!主要是单独都和写有没有问题
回复 支持 反对

使用道具 举报

发表于 2010-5-7 16:32:00 | 显示全部楼层
00FFBRAS055001表示读 00FFWRAD060009表示写 如果不清楚看看三菱的通讯手册
回复 支持 反对

使用道具 举报

 楼主| 发表于 2010-5-7 16:36:00 | 显示全部楼层
不行诶。。。。ComCls As Cls 用户定义类型未定义
回复 支持 反对

使用道具 举报

 楼主| 发表于 2010-5-7 16:37:00 | 显示全部楼层
请教高手,能否用我已给的程序,帮我把读地址程序调试出来,哪些不对帮我改出来~~
回复 支持 反对

使用道具 举报

发表于 2010-5-8 09:18:00 | 显示全部楼层
ClS.cls文件如下: VERSION 1.0 CLASS BEGIN MultiUse = -1 \'True Persistable = 0 \'NotPersistable DataBindingBehavior = 0 \'vbNone DataSourceBehavior = 0 \'vbNone MTSTransactionMode = 0 \'NotAnMTSObject END Attribute VB_Name = "CLS" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = True Attribute VB_PredeclaredId = False Attribute VB_Exposed = False \'Option Explicit Private Const CommSettings = "9600,E,7,1" Private Const CommInputMode = 0 Private Const CommCommPort = 1 Public ClsState As String Public SendOK As Boolean Private Sub Class_Initialize() If main_Form.MSComm1.PortOpen <> True Then With main_Form.MSComm1 .CommPort = CommCommPort .Settings = CommSettings .InputMode = CommInputMode .InBufferSize = 1024 .RTSEnable = True \'.SThreshold = 10 .RThreshold = 1 .InputLen = 0 .OutBufferCount = 0 .InBufferCount = 0 .PortOpen = True End With End If End Sub Private Sub Class_Terminate() \'If main_Form.MSComm1.PortOpen = True Then With main_Form.MSComm1 .RTSEnable = False .InputLen = 0 .OutBufferCount = 0 .InBufferCount = 0 .PortOpen = False End With \'End If End Sub Public Function RevPLC(iNo As String, iPLCNo As String, iType As String, iAdd As String, iNum As String) \'iNo is station\'no,iPLCNo is PLC\'s no,iType is element ,iAdd is address,iNum is number Dim Stmp As String Dim cha, j As Integer Dim ChaCount As Integer Dim Comm As String Dim Pnow Select Case iType Case "D" Comm = "WR" cha = Val("&H" & iNum) * 4 Case "M", "X", "Y", "S" Comm = "BR" cha = Val("&H" & iNum) End Select iAdd = Right$("000" & iAdd, 4) iNum = Right$("0" & iNum, 2) Stmp = Chr$(5) & iNo & iPLCNo & Comm & "0" & iType & iAdd & iNum ChaCount = 6 + cha main_Form.MSComm1.Output = Stmp Dim SNow SNow = Time() If Not Do_Events_Buffer("Rev", main_Form.MSComm1, ChaCount) Then MsgBox "CLS wrong,please pay attention to line", vbOKOnly, "wrong" ClsState = "Port is close" Else RevPLC = CStr(main_Form.MSComm1.Input) ClsState = "Port is open" End If RevPLC = Mid$(RevPLC, 6, cha) Debug.Print RevPLC End Function Public Function SendPLC(iNo As String, iPLCNo As String, iType As String, iAdd As String, iNum As String, iValue As String) \'SendPLC( )(no sum check code) \'iNo is station\'no,iPLCNo is PLC\'s no,iType is element ,iAdd is address,iNum is number,iValue is element\'value Dim Stmp As String Dim Comm As String Dim SNow As String Dim ChaCount As Integer Select Case iType Case "D" Comm = "WW" Case "M", "X", "Y", "S" Comm = "BW" End Select iAdd = Right$("000" & iAdd, 4) iNum = Right$("0" & iNum, 2) Stmp = Chr$(5) & iNo & iPLCNo & Comm & "A" & iType & iAdd & iNum & iValue main_Form.MSComm1.Output = Stmp If Not Do_Events_Buffer("Send", main_Form.MSComm1, 0) Then MsgBox "CLS wrong,please pay attention to line", vbOKOnly, "wrong" SendPLC = False Else SendPLC = True End If End Function Private Function Do_Events_Buffer(Action As String, DoMSComm As MSComm, number As Integer) Dim SNow SNow = Time() Do_Events_Buffer = False If Action = "Rev" Then Do DoEvents If SNow - Time() > 1 Then Exit Function Loop Until DoMSComm.InBufferCount = number If DoMSComm.InBufferCount = number Then Do_Events_Buffer = True End If End If If Action = "Send" Then Do DoEvents If SNow - Time() > 1 Then Exit Function Loop Until DoMSComm.OutBufferCount = number If DoMSComm.OutBufferCount = number Then Do_Events_Buffer = True End If End If End Function Private Function msComm_onComm(Action As String, DoMSComm As MSComm, ChaCount As Integer) Dim SNow SNow = Time() msComm_onComm = False If Action = "Rev" Then Do DoEvents If SNow - Time() > 1 Then Exit Function Loop Until DoMSComm.CommEvent > 0 \'Debug.Print DoMSComm.CommEvent Select Case DoMSComm.CommEvent Case comEvReceive Do DoEvents If SNow - Time() > 1 Then Exit Function Loop Until DoMSComm.InBufferCount = ChaCount \'If DoMSComm.InBufferCount = ChaCount Then msComm_onComm = True \'End If End Select End If If Action = "Send" Then Do DoEvents \'If SNow - Time() > 1 Then Exit Function Loop Until DoMSComm.CommEvent > 0 \'Debug.Print DoMSComm.CommEvent Select Case DoMSComm.CommEvent Case comEvReceive Do DoEvents \'If SNow - Time() > 1 Then Exit Function Debug.Print DoMSComm.OutBufferCount Loop Until DoMSComm.OutBufferCount = ChaCount \'If DoMSComm.OutBufferCount = ChaCount Then msComm_onComm = True SendOK = True \'End If End Select End If \'Debug.Print TEMP \'msComm_onComm = TEMP End Function Public Function onCommRevPLC(iNo As String, iPLCNo As String, iType As String, iAdd As String, iNum As String) \'iNo is station\'no,iPLCNo is PLC\'s no,iType is element ,iAdd is address,iNum is number Dim Stmp As String Dim cha, j As Integer Dim ChaCount As Integer Dim Comm As String Dim Pnow Select Case iType Case "D" Comm = "WR" cha = Val("&H" & iNum) * 4 Case "M", "X", "Y", "S" Comm = "BR" cha = Val("&H" & iNum) End Select iAdd = Right$("000" & iAdd, 4) iNum = Right$("0" & iNum, 2) Stmp = Chr$(5) & iNo & iPLCNo & Comm & "0" & iType & iAdd & iNum ChaCount = 6 + cha Do DoEvents Loop Until Stmp <> "" main_Form.MSComm1.Output = Stmp If Not msComm_onComm("Rev", main_Form.MSComm1, ChaCount) Then MsgBox "CLS wrong,please pay attention to line", vbOKOnly, "wrong" ClsState = "Port is close" Else onCommRevPLC = CStr(main_Form.MSComm1.Input) ClsState = "Port is open" End If onCommRevPLC = Mid$(onCommRevPLC, 6, cha) Debug.Print onCommRevPLC End Function Public Function onCommSendPLC(str As String) \'main_Form.MSComm1.Output = Chr$(5) + "00FFBWAS0550010" main_Form.MSComm1.Output = Chr$(5) + str msComm_onComm "Send", main_Form.MSComm1, "0" If SendOK = False Then MsgBox "通讯错误", vbOKOnly, "通讯错误" onCommSendPLC = False Else onCommSendPLC = True End If End Function
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册哦

本版积分规则

QQ|小黑屋|手机版|Archiver|PLC技术网-PLC论坛 ( 粤ICP备17165530号 )|网站地图

GMT+8, 2024-9-20 15:23 , Processed in 0.071762 second(s), 24 queries .

快速回复 返回顶部 返回列表