
Dear Larry,
How can I set my IP address and other network settings at runtime with the Rabbit API?
Jimi D.
Hello Jimi, that’s a very common question we get here, and it’s easier than you might think. I suggest that you use the ifconfig() function to configure or reconfigure your network.
Let’s take a look at a very short sample that demonstrates how this can work.
//First, you will need to define your network settings:
/**
* The following macros define the network setup of the physical Ethernet
* interface.
*/
#define LOCAL_IP1 "192.168.1.197"
#define LOCAL_NETMASK "255.255.255.0"
#define LOCAL_GATEWAY "192.168.1.1"
/**
* USE_ETHERNET must be defined. This definition simply means that we are
* using the first (and probably only) physical Ethernet interface.
*/
#define USE_ETHERNET 0x01
/**
* NOTE: Since we are setting up the network manually, we are
* intentionally NOT using the TCP_CONFIG.LIB setup.
*/
#define TCPCONFIG 0
#memmap xmem
#use "dcrtcp.lib"
void main(void)
{
//Initialize the TCP/IP stack
sock_init();
//Perform runtime network configuration
printf("Bringing up Main Interface \n");
ifconfig(IF_ETH0,
IFS_IPADDR, aton(LOCAL_IP1),
IFS_NETMASK, aton(LOCAL_NETMASK),
IFS_ROUTER_SET, aton(LOCAL_GATEWAY),
IFS_UP,
IFS_END);
// Wait for the interface to come up
while (ifpending(IF_ETH0) == IF_COMING_UP)
{
tcp_tick(NULL);
}
printf("Interface is up!! Try a ping!!\n\n");
while(1)
{
//Drive the TCP stack
tcp_tick(NULL);
}
}
The ifconfig function is a powerful tool that provides you extensive control of your network configuration. If you like to fine out more on ifconfig, click on the function in Dynamic C and press CTRL-H to view the documentation. I hope this helps! Good luck with your application.
- Larry C.
Larry Cicchinelli is Rabbit Semiconductor’s Technical Support Manager. He has 30 years of embedded experience, and is considered one of the foremost authorities on Rabbit products. Larry and his staff offer comprehensive technical support to Rabbit customers.
Submit your questions for Larry via email at
AskLarry@rabbit.com
Read more Ask Larry Answers