EthernetDNS

SwordfishUser.EthernetDNS History

Hide minor edits - Show changes to output

Added lines 1-28:
This sample program uses the DNS module to obtain an IP address from a host name. Here's what you need to do.

*Plug a network cable into your board and connect to your network. For example, just plug your Ethernet board into your broadband router, which will act as a DHCP server.
*Compile and then program the code into your device.
*Start the Swordfish Serial Communicator plugin, and a connect to your MCUs USART at 115200 baud.
*Reset the MCU.

If all goes well, something like the following should be displayed in the Serial Communicator window

>>default bgcolor=#f5f5f5<<
 Waiting for DHCP...
 Host Name    : SWORDFISH     
 MAC Address  : $00 $04 $A3 $00 $00 $00
 IP address    : 192.123.223.1
 Mask          : 255.255.255.0
 Gateway      : 192.123.223.254
 Primary DNS  : 192.123.223.254
 Secondary DNS : 158.123.223.58
>><<

Now enter the host name and press enter. If all goes well, the program should return the correct IP address.

>>default bgcolor=#f5f5f5<<
 Resolving : www.microchip.com
 Resolved  : 88.221.127.45
>><<

!!!Sample Code
Added lines 1-135:
=code [=
// device and clock - code will work on an 18F452 as well...
Device = 18F4680
Clock = 20

// important configurations...
#option ENC28J60_CS = PORTD.6
#option NET_ICMP = true  // enable ping
#option NET_DHCP = true  // enable Dynamic Host Configuration Protocol (DHCP)
#option NET_NBNS = true  // enable NETBIOS
#option NET_DNS = true    // enable Domain Name Service (DNS)
Include "NETApp.bas"      // always first!

// TCPIP stack specifics...
Include "NETTypes.bas"
Include "NETUtils.bas"
Include "Tick.bas"
Include "DNS.bas"

// program helpers...
Include "NETAppDisplay.bas"
Include "usart.bas"
Include "convert.bas"
Include "ISRRX.bas"

// state machine constants...
Const
  DNS_HOME = 0,
  DNS_RESOLVING = 1

// state machine variable...
Dim DNSState As Byte
{
****************************************************************************
* Name    : DNSInit                                                        *
* Purpose : Initialise the DNS state machine                              *
****************************************************************************
}
Sub DNSInit()
  DNSState = DNS_HOME
End Sub
{
****************************************************************************
* Name    : DNSResolve                                                    *
* Purpose : Start resolving hostname to IP address                        *
****************************************************************************
}
Sub DNSResolve(ByRef pHostName As String)
  If DNSBeginUsage() Then
      USART.Write("Resolving : ")
      USART.Write(pHostName,13,10)
      DNS.DNSResolve(@pHostName, DNS_TYPE_A)
      DNSState = DNS_RESOLVING
  EndIf 
End Sub
{
****************************************************************************
* Name    : DNSTask                                                        *
* Purpose : Handle DNS processing                                          *
****************************************************************************
}
Sub DNSTask()
  Dim DNSHostIP As IP_ADDR
  If DNSState = DNS_RESOLVING Then
      DNSHostIP.Val = 0
      If DNSIsResolved(DNSHostIP) Then
        DNSEndUsage
        DNSState = DNS_HOME
        If DNSHostIP.Val <> 0 Then
            Write("Resolved  : ")
            DisplayIPAddress(DNSHostIP)
        Else
            Write("Unable to resolve",13,10)
        EndIf
      EndIf
  EndIf
End Sub 
{
****************************************************************************
* Name    : OnData                                                        *
* Purpose : The TCPIP stack is a co-operative state machine. Therefore, we *
*        : cannot wait around for USART data and use a buffered interrupt *
*        : instead. When a CR is received, set a ReadyToRead flag to true *
****************************************************************************
}
Dim ReadyToRead As Boolean
Event OnData()
  If ISRRX.DataChar = #13 Then
      ReadyToRead = true
  EndIf
End Event

// program variables...
Dim StrHostname As String(60)

// program start...
SetBaudrate(br115200)
DNSInit()

#if NET_DHCP
Write("Waiting for DHCP...",13,10)
#else
DisplayConfig
#endif

// use interrupt buffered USART...
ReadyToRead = false
USART.ReadTerminator = 13
ISRRX.Initialize(OnData)

// loop forever...
While true

  // This task performs normal stack task including checking for incoming
  // packet, type of packet and calling appropriate stack entity to process it.
  NETApp.Task
 
  // If the ready to read flag has been set, read a string from the interrupt
  // buffer and start resolving hostname to IP address...
  If ReadyToRead Then
      ReadyToRead = false
      ISRRX.ReadStr(StrHostname,13)
      DNSResolve(StrHostname)
  EndIf
  DNSTask()
 
  // If DHCP is enabled, display configuration when a DHCP event
  // has occurred...
  #if NET_DHCP
  If NETApp.DHCPEvent() Then
      DisplayConfig()
  EndIf
  #endif             
Wend
=]