06c Seriale Manager
This is the routine that manages the serial communication:
//Title: gest_seriale_100.c
//Author: najro.prestato
//Date: 12/03/07
//Current Rev: 100
//Mod.Descr.: RS232 management module:
// Communication protocol -> STX CMD DATI ETX
// Commands managed -> MI: Visualize on the display the ASCII string
// contained in DATI
// -> MA: sends to the serial an ASCII string
// contained in DATI
//
//1: Directives for the compiler
#include "sfr62.h"
#include "global.h"
#include "gest_lcd.h"
#include "messaggi_I.h"
#include "gest_seriale.h"
//2: Local Variables
union registro flagrx0;
union registro flagtx0;
unsigned char bufrx0[cBUFRX0];
unsigned char buftx0[cBUFTX0];
unsigned char pibufrx0,pubufrx0;
unsigned char bufpk0[cBUFPK0];
unsigned char lenpkrx0;
unsigned char statopkrx0;
unsigned char bytetx0,bytetxed0,tmrcmdmatx0;
unsigned char r0uart,r1uart,r2uart;
//3: Local functions prototypes
void gest_seriale(void);
void check_bufrx0(void);
void ela_bufpk0(void);
void pktx0(void);
//4: Serial Management Module
void gest_seriale(void)
{
//Verify if a new data package has arrived on bufrx0
check_bufrx0();
//Elaborate an eventual package extrapolated by buffer rx0(bufpk0)
ela_bufpk0();
//Sends data/reply through buftx0
pktx0();
}
//5: Local Function Codes
//
//5a) Function extrapolation package from buffer RX0
void check_bufrx0(void)
{
unsigned char datorx;
//Verify if there is any data to elaborate
while (pibufrx0 ^ pubufrx0)
{
//acquire data from serial
datorx=bufrx0[pubufrx0];
pubufrx0++;
//verify if the index is for reseting (buffer circolare)
if (pubufrx0 > cBUFRX0-1)
pubufrx0 = 0;
//analize acquired data
switch (datorx)
{
//research cLF without to have cSTX
//NB: This happens when waiting the echo of a command
// from a remote system ex: MA
case cLF:
//if STX not received
if (!flagrx0.bit.FSTX)
{
if (lenpkrx0 > 2)
{
if ( (bufpk0[lenpkrx0-1]==cCR)
&& (bufpk0[lenpkrx0-2]>0x19)
&& (bufpk0[lenpkrx0-3]>0x19))
{
bufpk0[0]=bufpk0[lenpkrx0-3]; //
bufpk0[1]=bufpk0[lenpkrx0-2]; //
flagrx0.bit.FPKRX=1;
//found identified end package rx
lenpkrx0=0; //reset data package counter
}
}
}
//If this is a non valid character and
//in specific it has to restart the cycle
else
{
lenpkrx0=0;
flagrx0.bit.FSTX=0;
}
break;
//research start of package
case cSTX:
flagrx0.bit.FSTX=1;
//found identified start of package rx
lenpkrx0=0;
//reset data package counter
break;
//research end of package
case cETX:
//if not received flag of the head of the package discard this ETX
if (flagrx0.bit.FSTX)
{
//verify that it has been found at least 'cNUMRXVAL'
//given among STX and ETX
if (lenpkrx0 < cNUMRXVAL)
{
flagrx0.bit.FSTX=0;
//declares not found identified start package rx
lenpkrx0=0;
//reset data package counter
}
else
{
flagrx0.bit.FPKRX=1;
//found identified end of package rx
flagrx0.bit.FSTX=0;
//reset flag identified start package rx
}
}
break;
//Data acquisition
default:
//if not received flag start package discard this ETX
if (flagrx0.bit.FSTX)
{
//Verify if received a non valid character (<20h)
if (datorx > 0x19)
{
//memorize package in dedicated buffer
bufpk0[lenpkrx0]=datorx;
lenpkrx0++;
if (lenpkrx0 > cBUFPK0-1)
lenpkrx0 = 0;
}
//If no discard all
else
{
lenpkrx0=0;
flagrx0.bit.FSTX=0;
}
}
else
{
//Memorize data that could be of an eventual
//response from the remote system (see case cLF: )
bufpk0[lenpkrx0]=datorx;
lenpkrx0++;
}
break;
}
}
}
//
//5b) Function elaboration package buffer RX0 (bufpk0)
void ela_bufpk0(void)
{
unsigned char wki,wkj;
switch(statopkrx0)
{
//Command Recognizing
case 0:
if (flagrx0.bit.FPKRX==1)
{
//Verify for commands Mx
if (bufpk0[0]=='M')
//recognized command 'M'
statopkrx0=1;
//Reinit if no valid command group it is identified
else
{
flagrx0.bit.FPKRX=0;
//reenable RX on RS232
statopkrx0=00;
//turn to state 0
}
}
break;
//Commands Mx
case 1:
//Verify for subcommand MI
if (bufpk0[1]=='I')
{
statopkrx0=10;
break;
}
//Verify for echo command MA
else if (bufpk0[1]=='A')
{
statopkrx0=11;
break;
}
//Reinit if no valid subcommand Mx identified
else
{
flagrx0.bit.FPKRX=0;
//reenable RX on RS232
statopkrx0=00;
//turn to state 0
}
break;
//Elaborate command MI
case 10:
//Reinit buffer indexes lcd rx if new message to be aquired
rowlcdrx=0;
cullcdrx=0;
prowlcdrx=00;
irowlcdrx=00;
//Clean buffer display visualisation
clearbuflcdrx();
//Enable backlight dislpay
tmronlcd=cTMRONLCD;
//Prepare buffer lcd rx. NB: substract 2 from lenpkrx0 to consider 'MI'
for (wki=2; wki < (lenpkrx0); wki++)
{
//Load data in LCD buffer already organized in line/rows
if ((bufpk0[wki] != '[') && (bufpk0[wki] != ']'))
{
//NB: Use wki+2 to discharge initial 'MI'
buflcdrx[rowlcdrx][cullcdrx]= bufpk0[wki];
cullcdrx++;
if (cullcdrx >= cCULLCDRX)
{
cullcdrx=0;
rowlcdrx++;
if (rowlcdrx > cROWLCDRX)
rowlcdrx=0;
}
}
else
{
//Hexadecimal string management
//If [0D]:
if ( (bufpk0[wki] == '[')
&& (bufpk0[wki+1]=='0')
&& ((bufpk0[wki+2]=='D') || (bufpk0[wki+2]=='d'))
&& (bufpk0[wki+3]==']') )
{
unsigned char wkj;
if (cullcdrx < (cCULLCDRX-1))
{
for (wkj=cullcdrx; wkj < cCULLCDRX; wkj++)
buflcdrx[rowlcdrx][wkj] = ' ';
cullcdrx=0;
rowlcdrx++;
if (rowlcdrx > cROWLCDRX)
rowlcdrx=0;
}
wki+=2;
}
//If [xx]:
else
{
if ( (bufpk0[wki] == '[') && (bufpk0[wki+3]==']') )
{
buflcdrx[rowlcdrx][cullcdrx] = ' ';
cullcdrx++;
if (cullcdrx > cCULLCDRX)
{
cullcdrx=0;
rowlcdrx++;
if (rowlcdrx > cROWLCDRX)
rowlcdrx=0;
}
wki+=2;
}
}
}
}
flaglcd.bit.FBLRX=1; //Declare message for available LCD
flagrx0.bit.FPKRX=0; //Free RX0 serial
statopkrx0=00; //Wait new package
break;
//Ricezione echo comando MA
case 11:
flagrx0.bit.FPKRX=0; //Free RX0 serial
statopkrx0=00; //Wait new package
flaglcd.bit.FBLTX=1; //Declare echo received command
break;
//No valid/known command
default:
flagrx0.bit.FPKRX=0; //reenable RX0
statopkrx0=00; //turn to state 0
break;
}
}
//
//5c) Function send data to buftx0
void pktx0(void)
{
//Verify if package ready && UART available
if ((flagtx0.bit.FTXAT==1) && (ti_u0c1==1))
{
if (flagtx0.bit.FTXST==0)
{
flagtx0.bit.FTXST=1; //Declare STX sent
u0tbl=cSTX; //Transmit STX (start of package)
bytetxed0=00; //Reset counter sent data to uart
}
else
{
if (flagtx0.bit.FTXDA==0)
{
u0tbl=buftx0[bytetxed0];
bytetxed0++;
if (bytetxed0==bytetx0)
{
flagtx0.bit.FTXDA=1; //declare tx data
}
}
else
{
if (flagtx0.bit.FTXET==0)
{
flagtx0.bit.FTXET=1; //declare ETX sent
u0tbl=cETX; //transmit ETX (end of package)
}
else
{
flagtx0.bit.FTXAT=0;
//declares package not available
}
}
}
}
} 

