Академический Документы
Профессиональный Документы
Культура Документы
The following example tinyslave.cpp shows how to implement a small Modbus RTU slave:
C#
VB
Copy
//
// @file tinyslave.cs
//
// A simple Modbus RTU slave program.
//
// @if NOTICE
//
// Copyright (c) proconX Pty Ltd. All rights reserved.
//
// The following source file constitutes example program code and is
// intended merely to illustrate useful programming techniques. The user
// is responsible for applying the code correctly.
//
// THIS SOFTWARE IS PROVIDED BY PROCONX AND CONTRIBUTORS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PROCONX OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// @endif
//
using System;
using FieldTalk.Modbus.Slave;
class TcpSlaveApp
{
private MbusRtuSlaveProtocol mbusServer = new MbusRtuSlaveProtocol();
private MyDatatable dataTable = new MyDatatable();
MbusSerialSlaveProtocol.SER_PARITY_EVEN);
}
if (result != BusProtocolErrors.FTALK_SUCCESS)
{
Console.WriteLine(BusProtocolErrors.getBusProtocolErrorText(result));
Environment.Exit(result);
}
Console.WriteLine("Modbus server started on serial interface " +
mbusServer.portName);
}
do
{
result = mbusServer.serverLoop();
} while (!(result != BusProtocolErrors.FTALK_SUCCESS));
if (result != BusProtocolErrors.FTALK_SUCCESS)
{
Console.WriteLine(BusProtocolErrors.getBusProtocolErrorText(result));
}
}
MODBUS/TCP Example
The following example tcpslave.cpp shows how to implement a small Modbus/TCP slave:
C#
VB
Copy
//
// @file tcpslave.cs
//
// A simple console based example using FieldTalk in Modbus/TCP slave mode
// @if NOTICE
//
// Copyright (c) proconX Pty Ltd. All rights reserved.
//
// The following source file constitutes example program code and is
// intended merely to illustrate useful programming techniques. The user
// is responsible for applying the code correctly.
//
// THIS SOFTWARE IS PROVIDED BY PROCONX AND CONTRIBUTORS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PROCONX OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// @endif
//
using System;
using FieldTalk.Modbus.Slave;
//
// Validate start address range
// Note: In this example, we have 2000 references at 5000 and 2000
references at 7000.
//
if ((startRef < 5000) || (startRef >= 9000))
return false;
else
startRef -=5000;
if (startRef + regArr.Length > enronData.Length)
{
return false;
}
// Copy registers from local data array to Modbus
for (int i = 0; i < regArr.Length; i++)
{
regArr[i] = enronData[startRef + i];
}
return true;
}
//
// Validate start address range
// Note: In this example, we have 2000 references at 5000 and 2000
references at 7000.
//
if ((startRef < 5000) || (startRef >= 9000))
return false;
else
startRef -= 5000;
if (startRef + regArr.Length > enronData.Length)
{
return false;
}
// Copy registers from Modbus to local data block
for (int i = 0; i < regArr.Length; i++)
{
enronData[startRef + i] = regArr[i];
}
return true;
}
#endif
class TcpSlaveApp
{
private MbusTcpSlaveProtocol mbusServer = new MbusTcpSlaveProtocol();
private MyDatatable dataTable = new MyDatatable();
do
{
result = mbusServer.serverLoop();
} while (!(result != BusProtocolErrors.FTALK_SUCCESS));
if (result != BusProtocolErrors.FTALK_SUCCESS)
{
Console.WriteLine(BusProtocolErrors.getBusProtocolErrorText(result));
}
}