VB 里怎么样吧数据写进PLC?我已经有写数据的函数了,但是就是没办法吧这个函数调用处来,给D地址写数据。有高手能帮我做做吗?
以下是我已有的写D地址的函数:
'*写入PLC
'*Write_PLC(mData(),mStart)
'*mData() 写入的数据--以数组形式来表示
'*mStart 写入的首地址
'*******************************
Public Function Write_PLC(mData() As Integer, mStart As Integer) As Boolean
Dim myHex As String, Q As Integer, myTest As String, mCounts As Integer
On Error GoTo WriteErr
Q = mStart * 2 + &H1000 '地址转换
mCounts = (UBound(mData) + 1) * 2 '写几个D的地址
myHex = "1" & Hex(Q) & Check_Hex(mCounts) '定义的一个函数来对写入的值进行补0操作
For Q = 0 To UBound(mData)
myHex = myHex & Check_Hex(mData(Q) Mod 256) & Check_Hex(mData(Q) \ 256)
Next
myHex = myHex & 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
If Asc(myTest) = 6 Then Write_PLC = True
Exit Function
WriteErr:
Write_PLC = False
End Function |