stuartscott171
Joined: 18 Aug 2015 Posts: 1
|
Modbus TCP Example |
Posted: Tue Aug 18, 2015 9:21 pm |
|
|
I am trying to find a example for implementing the Modbus TCP server.
I have managed to get the TCP stack and Modbus driver to compile but stuck on the basic implementation, looking for something to get me started.
Code: | #include <main.h>
//#include <main.h>
#define MODBUS_PROTOCOL MODBUS_PROTOCOL_TCPIP
#define MODBUS_TYPE MODBUS_TYPE_SERVER
#define MODBUS_LISTEN_SOCKETS 5
//#define MODBUS_OUR_ADDRESS 1
#include "modbus.c"
void IPAddressInit(void)
{
//MAC address of this unit
MY_MAC_BYTE1=MY_DEFAULT_MAC_BYTE1;
MY_MAC_BYTE2=MY_DEFAULT_MAC_BYTE2;
MY_MAC_BYTE3=MY_DEFAULT_MAC_BYTE3;
MY_MAC_BYTE4=MY_DEFAULT_MAC_BYTE4;
MY_MAC_BYTE5=MY_DEFAULT_MAC_BYTE5;
MY_MAC_BYTE6=MY_DEFAULT_MAC_BYTE6;
//IP address of this unit
MY_IP_BYTE1=MY_DEFAULT_IP_ADDR_BYTE1;
MY_IP_BYTE2=MY_DEFAULT_IP_ADDR_BYTE2;
MY_IP_BYTE3=MY_DEFAULT_IP_ADDR_BYTE3;
MY_IP_BYTE4=MY_DEFAULT_IP_ADDR_BYTE4;
//network gateway
MY_GATE_BYTE1=MY_DEFAULT_GATE_BYTE1;
MY_GATE_BYTE2=MY_DEFAULT_GATE_BYTE2;
MY_GATE_BYTE3=MY_DEFAULT_GATE_BYTE3;
MY_GATE_BYTE4=MY_DEFAULT_GATE_BYTE4;
//subnet mask
MY_MASK_BYTE1=MY_DEFAULT_MASK_BYTE1;
MY_MASK_BYTE2=MY_DEFAULT_MASK_BYTE2;
MY_MASK_BYTE3=MY_DEFAULT_MASK_BYTE3;
MY_MASK_BYTE4=MY_DEFAULT_MASK_BYTE4;
}
void main()
{
IPAddressInit();
TickInit();
enable_interrupts(GLOBAL);
StackInit();
int16 hold_regs[] = {0x8800,0x7700,0x6600,0x5500,0x4400,0x3300,0x2200,0x1100};
modbus_init();
while(TRUE)
{
// TCP/IP code
StackTask();
StackApplications();
ModbusTask();
if(modbus_kbhit()) {
}
//TODO: User Code
}
}
|
|
|