An Example
A DS1820 temperature sensor is mounted on the Rita51 board prototyping area and connected to the Internet.  The following Rita51 program illustrates the use of the NetApi51 functions.  These functions have the prefix “Net.”  The function GetTemperature() is specific to the hardware.  It returns TRUE if successful and modifies the integers nT and nF, the temperature in Celsius and Fahrenheit.

main(){

int           nSize, nSrcPort, nDestPort, nC, nF;

unsigned char cIP[4], cEther[6], szMsg[1024];

 

 NetSetEtherAddress(0x0504, 0x0302, 0x0100);           // set the node’s ether address (5-4-3-2-1-0)

 NetSetIPAddress(0xC864, 0x6402);                      // set the node’s ip address (200.100.100.2)

 while(NetInitialize(IM_BROADCAST | IM_INDIVIDUAL));   // wait for hardware initialization

 

// the main polling loop

 while(TRUE)

  {

   NetServiceMsgs(TRUE);                               // process arp and icmp messages

   nSize=NetGetUdpMsg(szMsg);                          // check incoming UDP messages

   if(nSize<=0) continue;                              // no msg if size is 0

   if(!szMsg[0]) continue;                             // only Rita StdAscii messages

   szMsg[nSize]=0;                                     // terminate C-string

   NetGetUdpInSrcEthernAddress(cEther);                // get sender's ether address

   NetGetUdpInSrcIpAddress(cIP);                       // get sender's IP address (source IP)

   nSrcPort=NetGetUdpInSrcPort();                      // get source UDP port

   nDestPort=NetGetUdpInDestPort();                    // get destination UDP port

 

   // prepare response msg

   NetSetUdpOutSrcPort(0x2000);                        // our UDP port

   NetSetUdpOutDestPort(nSrcPort);                     // use sender's port, ether and ip

   NetSetUdpOutDestEthernAddress(cEther);

   NetSetUdpOutDestIpAddress(cIP);

 

   switch(szMsg[0])

    {

     case 't' :

     case 'T' : if(!GetTemperature(&nC, &nF)) csprintf(szMsg, "Temperature sensor is off line.");

                 else csprintf(szMsg, "Temperature : %d C    (%d F)", nC, nF);

                break;

     default  : csprintf(szMsg, "Please use T temperature.”);

     }

 

   NetSendUdpMsg(szMsg, strlen(szMsg));                // transmit message

   }                                                   // ... the endless while loop

}

Many of the NetApiXX functions are self-explanatory.  The bulk of network connectivity is handled by the function NetServiceMsgs().  Its argument (TRUE) instructs the function that the node should automatically respond to ARP and ICMP messages, making its IP address known to the network.  The two functions NetGetUdpMsg()and NetSendUdpMsg() allow the application to receive and transmit messages, composed as standard C-strings.

More Examples may be found in the Reads51 Software.

RITA  |  Rita-51  |  Rita-51J  |  Design Philosophy  |  RitaBrowser  |  NetApi51  |  ExamplePing UtilityNetCat

 

 ___________________________________________

 

      Copyright 1995-2008 Rigel Corporation
       Hardware Warranty | Software License Agreement | Privacy Policy | Contact Us 
       Last updated 3/18/08