Design Template - siod

 

siod is a full functional network to serial converter with following features:

 

*          TCP server, TCP client and UDP modes.

*          Packet mode for serial input

*          Inactive timeout

*          User commands from serial port

 

The source code of "siod" is included in "app/siod" directory. It was designed to be a reference template for users. Its source code contains only about 350 lines (excluding the user command module) which makes users easier to modify the source without the need to write their programs from scratch.

 

siod demonstrates important tasks by using commonly used uClinux system calls:

*          Read configuration settings

*          Use sockets for TCP server, TCP client and UDP.

*          Use interval timer

*          Use software interrupt - signal().

*          Read and write to serial ports

*          Use poll() to manage read / write timeout.

 

 

Configuration

 

siod uses settings of [SIO 1] and [SIO 2] sections in /flash/config/app.ini.

[SIO 1]

SioPort=100

HostIP=

HostPort=

SioMode=0

SioBaud=9

SioParity=0

SioBits=3

SioStops=0

SioFlow=0

PktMode=1

Inactive=10

 

 

 

Web Interface

 

The web interface (cgi.c and io.htm) manages all the settings in [SIO 1] and [SIO 2] sections.

 

 

 

Program Code

 

The central part of siod is a data pumping loop which move data between network and serial port. Users can easily replace the data pump with custom codes.

 

app/siod/siod.c

    // data pumping loop

    while(p->bConnect)

    {  // wait a while. do not use CPU all the time.

        Sleep(10);

         // make polling of 2 handles

       poll(Flag,2,0);

      // move data from socket to uart

        if(Flag[1].revents & POLLIN)         // if socket has data

        {  if(Flag[0].revents & POLLOUT)     // if serial port can output

           {  n=recv(p->Skt,p->Buf,2048,0);

             if(n<=0) break;        // socket closed

              if(n>0)

               {  SioWrite(p->Sio,p->Buf,n);

                  p->tmInactive=0;    // reset inactive counter

               }

          }

        }

       // yield. required.

       Sleep(0);

       // check if packet gap detected for packet mode

       if(!SioStatus(p->Sio,SIO_PKT)) continue;

      // move data from uart to socket

      if(Flag[0].revents & POLLIN)         // if serial port has data

      {  if(Flag[1].revents & POLLOUT)     // if socket can output

          {  n=SioRead(p->Sio,p->Buf,2048);

             if(n>0)

              {  // TCP socket

                 if(p->Mode!=MOD_UDP)

                 {   send(p->Skt,p->Buf,n,0); 

                 } else 

                 // UDP socket

                 {   sendto(p->Skt,p->Buf,n,0,

                          (struct sockaddr *)&p->Dest,sizeof(p->Dest));

                 }

                 p->tmInactive=0;    // reset inactive counter

             }

        }

   }

       // yield. required.

       Sleep(0);

    }