Hello all,
I'm working on a class that's communicating with hardware by the Modbus-protocol.
I have found a automation server on the internet, but this came only with VB examples.
This is a method that I should convert to foxpro, but I don't know where to begin.
The thing where I have problems with is the use of arrays.Should I use the function COMARRAY() or should I convert the array into a string, ... ?
This is the example-code in VB.
Please help me :-&
TxRxMessage
mbusrv.TxRxMessage(vQuery, QueryLength, vResponse, RespLength, [vNumOfBytesRead])
HRESULT TxRxMessage([in] VARIANT vQuery , [in] short QueryLength , [out] VARIANT* vResponse, [in] short RespLength, [out,optional] VARIANT* vNumOfBytesRead , [out,retval] long* plRetVal);
Return Value
Error code if the function failed or mbErrOk if succeed.
Parameters
vQuery - Query message using RTU- Frame format. (input), this must be a byte array.
QueryLength - Query size. (input)
vResponse - Response from device (output). The response is returned in a byte array.
RespLength - Response Size. (input) . This value must be 0 if you do not know the response size, all bytes the slave sent to master will be in abyResponse buffer.
vNumOfBytesRead-A variable that will be set with the number of bytes read from device.
Remarks
This function allow to send general modbus function to a slave or create new modbus functions implemented in a particular device model.
example:
This example show how to create a new modbus function. Supose we want to implement Modbus function 8 (Diagnostics) for checking communication. The function will be called LoopbackTest, it just send message and verify if the device return the same bytes of the query.
Function LoopbackTest(nAddr As Integer) As Long
Const QUERY_LENGHT = 6
Const nReplyLength = 6
Dim abyQuery() As Byte
Dim vReply As Variant
Dim nError As Long
Dim iReg As Integer
Dim iRetry As Integer
ReDim abyQuery(0 To QUERY_LENGHT)
'new modbus message see modbus protcol on
www.modicon.comabyQuery(0) = nAddr 'ADDR
abyQuery(1) = 8 'Function
abyQuery(2) = 0 'diag. code=0
abyQuery(3) = 0
abyQuery(4) = &HA5
abyQuery(5) = &H37
Do
nError = m_ModbusServer.TxRxMessage(CVar(abyQuery), QUERY_LENGHT, vReply, nReplyLength) 'send message to device
If (nError = mbErrOk) Then 'interpret response ,
For iReg = 0 To QUERY_LENGHT - 1
If (abyQuery(iReg) <> vReply(iReg)) Then
nError = mbErrInvResp
Exit For
End If
Next iReg
End If
iRetry = iRetry + iRetry
Loop While ((iRetry < m_ModbusServer.Retries) And (mbErrOk <> nError))
LoopbackTest = nError
End Function 'end LoopBackTest