network.cpp

An application used to vote, for example in the school, where a teacher have a lot of student and want to know their respons to a question. The teacher using a PC can see the answer of all student on his PC. every student have one of this box and they are connected through RS422 and the PC using a rs232/rs422 converter make a polling for each one of this box and can see the key pressed. (by Gianni Franchi).




#include "p16f84.h"

#include "serial.h"

#include "network.h"





// Packet communication :

// Byte 0:  WAKE_UP

// Byte 1:  Address 

// Byte 2:  Command

// Byte 3:  Checksum of Address and Command







/*

   Packet serial initialization

*/

void Net_Init ()

{

   Init_Serial ();   // Just setup the serial

}





/*

   Packet receive from PC

*/

int Net_Receive (unsigned long TimeOut)

{

   unsigned long xTime;

   int i=0;

   int ch,chk;

   int Ok=0;



   // Wait for incoming char for the time specified in TimeOut

   // It wait for receiving the "NET_WAKE_UP" char from the PC

   xTime=MS_TIMER;

   while (!Ok)

   {

      ch=RxChar ();

      if (ch==NET_WAKE_UP) Ok=1;

      if (xTime+TimeOut<MS_TIMER) Ok=1;

      if (TimeOut==0) Ok=1;

   }

   if (ch!=NET_WAKE_UP) return NET_NONE;  // no wake up received ? exit



   // Now receive the packet

   xTime=MS_TIMER;



   Ok=0;

   chk=0;

   while (!Ok)

   {

      ch=RxChar ();

      if (ch!=NO_CHAR)  // Character received ? put it in the buffer

      {

         NetBuf[i]=ch;

         i++;

         if (i<3) chk=chk ^ ch;  // Calculate the checksum (a simply xor)

      }

      if (i==3) Ok=1;   // Packet complete ? exit

      if (xTime+NET_PAKET_TIMEOUT<MS_TIMER) Ok=1;

   }

   if (i!=3) return NET_TIMEOUT; // Packet incomplete ? exit with error

   if (i==3 && chk!=NetBuf[2]) return NET_CHK;  // Checksum error ?

   return NET_OK; // Packet OK

}





/*

   Packet Send to PC

*/

void Net_Send (char Add,char Code)

{

   portb=128;  // This enable the DS3487 RS422 Line driver

            // The driver is usualli in three-state mode if ENABLE pin is LOW



   TxChar (NET_SEND_MASTER);  // Tx Wake to PC

   TxChar (Add);           // Send the address of the slave that reply

   TxChar (Code);          // Just send back the command received

   TxChar (Add^Code);         // Send the chksum of dataa



   portb=0;             // Put the Trasmitter in three-state

}






http://www.sourceboost.com/home.html

Copyright © 2002-2006 SourceBoost Technologies