Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Intelligent voting machine using sd card with barcode scanner

Status
Not open for further replies.

sobia sarwar

Member level 5
Member level 5
Joined
Nov 15, 2012
Messages
81
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,883
basically i m using pic18f452 as a controller and sd(mmc) card as a voters database. my uart. virtual terminal is showing all data read from sd card (i-e list of voters). now i wanted to solve the problem that when someone scans their id card from the bar-code scanner then i need help in how can i compare the scanned id card no with the one already in sd card so that the screen can allow voter to cast vote.the display i m using is visual tft. for simulation i m using proteus p.8 and mikro c pro v5.6.
plz can anyone help me
 

i have compared the scanned id no with one from the txt file but now the problem is id no from txt file is saved in two dimensional array and id no from barcode scanner is saved in one dimensional array. now the problem is when i compare raw data using both as one dimensional array it works but using my above work results comes as not matching returning 0. can anybody please help
my recent work attached here
 

Attachments

  • final final.rar
    170.6 KB · Views: 160

How are you comparing arrays? Please post your proteus .mmc file.

so you have a 2D array for id no from txt file like arry2d[5][5]

you also have a 1D array for bar code data like arry1d[5]

let us assume your 2d array and 1d array has these values

arry2d[0][0] = 'A'
arry2d[0][1] = 'B'
arry2d[0][2] = 'C'
arry2d[0][3] = 'D'
arry2d[0][4] = 'E'

arry1d[0] = "A'
arry1d[1] = 'B'
arry1d[2] = 'C'
arry1d[3] = 'D'
arry1d[4] = 'E'

you have to compare these tow arrays

Code:
i = 0;
j = 0;
k = 0;

for(j=0;j<5;j++0) {
    if(arry2d[i][j] == arry1d[j]) {
               k++;
    }
}

if(k == 5) {     //if array elements are matched
    //then do whatever you want
}
 
Last edited:
thankx alot for replying.. i have tried this one as well but i guess problem is that its not the logic of comparing that went wrong its just that with the 2 dimensional array. i m quiet struck. need to submit project by next monday.......i tried a lot more but same results means dosen't match

- - - Updated - - -

thats the function for reading data from sdcard and then extracting only cnic of the voters.....


void M_Open_File_Read()
{
int j,k;
Mmc_Fat_Assign(&filename, 0);
Mmc_Fat_Reset(&size);
//for (i=1;second<11;i++)
Mmc_Fat_Read(&character);
while(character!='*')

{

if(character==' ')
while(character!=13)
{
Mmc_Fat_Read(&character);
if(character=='*')
chk=1;
}
cnics[first]=character;
++first;
//UART1_Write(character);
Mmc_Fat_Read(&character);
if(chk==1)
break;

}

//UART1_Write_text(cnics);

for(i=0;i<first;i++)
{
if(cnics==13)
{
cnics_array[row][col]='\0'; ++row; col=0; ++i;
}

cnics_array[row][col]= cnics;
col++;
}

// UART1_Write_text(cnics);

for(j=0;j<row;j++)
{
//UART1_Write(13);
for (k=0;k<16;k++)
{
tmp2[k]=cnics_array[j][k];
}
//UART1_Write_text(cnics_array[j][k]);
//UART1_Write_text(tmp2);
//UART1_Write(13);
// UART1_Write(13);
}

}

- - - Updated - - -

thats the function for reading data from sdcard and then extracting only cnic of the voters.....


void M_Open_File_Read()
{
int j,k;
Mmc_Fat_Assign(&filename, 0);
Mmc_Fat_Reset(&size);
//for (i=1;second<11;i++)
Mmc_Fat_Read(&character);
while(character!='*')

{

////////////////for one dimensional array//////////////////

if(character==' ')
while(character!=13)
{
Mmc_Fat_Read(&character);
if(character=='*')
chk=1;
}
cnics[first]=character;
++first;
//UART1_Write(character);
Mmc_Fat_Read(&character);
if(chk==1)
break;

}

//UART1_Write_text(cnics);

for(i=0;i<first;i++)
{
if(cnics==13)
{
cnics_array[row][col]='\0'; ++row; col=0; ++i;
}

cnics_array[row][col]= cnics;
col++;
}

// UART1_Write_text(cnics);

///////////////// 2 dimensional array////////////////////////

for(j=0;j<row;j++)
{
//UART1_Write(13);
for (k=0;k<16;k++)
{
tmp2[k]=cnics_array[j][k];
}
//UART1_Write_text(cnics_array[j][k]);
//UART1_Write_text(tmp2);
//UART1_Write(13);
// UART1_Write(13);
}

}

- - - Updated - - -

porteus.mmc file is actually of 1.84gb....trying to upload

- - - Updated - - -

i made image using usbimage tool of the txt file i attached

- - - Updated - - -

i made image using usbimage tool of the txt file i attached
 

Attachments

  • voter1.txt
    22 bytes · Views: 129

So you are storing cnics to cnics_array[row][col], and you are comparing the elements and they are not matching. Am I right?

Does your barcode scanner sends data in ASCII format.

You have two arrays whose elements are compared. right? Does both the arrays hold the right ascii values of data?

Why is your proteus.mmc 1.84gb? What have you put in it?

Use the file proteus.mm.rar for proteus simulation rename it to proteus.mmc
It contains voter1.txt file.

Which 2 array have to be compared?

I think this code is causing the problem
Code:
for(j=0;j<row;j++)
    {
      //UART1_Write(13);
      for (k=0;k<16;k++)
         {
           tmp2[k]=cnics_array[j][k];
         }
         //UART1_Write_text(cnics_array[j][k]);
         UART1_Write_text(tmp2);
         UART1_Write(13);
         UART1_Write(13);
    }

why are you doing
Code:
 for(j=0;j<row;j++)

you only have to do
Code:
j = 0;
for (k=0;k<16;k++)
         {
           tmp2[k]=cnics_array[j][k];
         }

isn't it?

i found the problem. There is a while(1) loop inside the finction VerifyDuplicateVoter() and I think the break; statement breaks for (cn = 0; cn <= 15; cn++)

How does the while91) loop break?

Code:
int VerifyDuplicateVoter()

 {
               M_Open_File_Read();
               Barcodescan();
               b_Duplicate = 1;
  while(1)
         {
              for (cn = 0; cn <= 15; cn++)
              {

                  // if ((cnics[cn] != text[cn]))
                  //if (barcode[CNIC] != tmp2[CNIC])
                   if (txt[cn] !=tmp2[cn])    // if any character is not matching, make flag 0 and exit for loop
                    {

                      return 0;
                        // UART1_Write('w');
                        b_Duplicate = 0 ;
                       break;
                    }

                    return 1;
              }
  
          }
  

 }

When I debugged the code all I see is it is doing the below code infinitely.

Code:
while(character!=13) 
        {
         Mmc_Fat_Read(&character);
          if(character=='*') 
            chk=1;
        }

Can you post the proper code to check.

I debugger. The below code is not executing

Code:
for(j=0;j<row;j++)
    {
      //UART1_Write(13);
      for (k=0;k<16;k++)
         {
           tmp2[k]=cnics_array[j][k];
         }
         //UART1_Write_text(cnics_array[j][k]);
         UART1_Write_text(tmp2);
         UART1_Write(13);
         UART1_Write(13);
    }


Heeeeeeeeeeeeeeeeyyyyyyyyyy! You can cast vote.

See the attached image 2


See the following code
Code:
for(i=0;i<first;i++)
   {
         if(cnics[i]==13)
         {
         cnics_array[row][col]='\0'; ++row; col=0; ++i;
         }
         
         cnics_array[row][col]= cnics[i];
         col++;
    }
       
     // UART1_Write_text(cnics);

   for(j=0;j<1;j++)
    {
      //UART1_Write(13);
      for (k=0;k<16;k++)
         {
           tmp2[k]=cnics_array[j][k];
         }
         //UART1_Write_text(cnics_array[j][k]);
         UART1_Write_text(tmp2);
         UART1_Write(13);
         UART1_Write(13);
    }

first in the first for loop cnics_array[row][col]= cnics; is performed, then
in the second for loop tmp2[k]=cnics_array[j][k]; is performed

Why can't you use tmp2[k]=cnics; just once?


If I make char txt[] = "34101-5508931-7"; and check it says "you can cast"
If I make char txt[] = "44101-5508931-7"; and check it says "you can't cast"

Is this what you wanted?
 

Attachments

  • proteus.mmc.rar
    283 KB · Views: 148
  • sim ss.jpg
    sim ss.jpg
    250.3 KB · Views: 165
  • sim ss1.jpg
    sim ss1.jpg
    468.1 KB · Views: 149
Last edited:
i found htat one you notified that cnics = cnics_array[row][col] and tmp2 part should be removed but still the problem is yes i wanted to compare the way you understood and yes barcode scanner sends data in ascii format.

what about both arrays holding right ascii values...?

actually i have extracted cnic from txt file in cnics_array[row][col] and i m comparing it with raw data means barcode is not connected yet just taking a char txt[]="34101-5508931-7" as i know its the first cnic in the file and checking whether the comparing is done done or not then i will try it on hardware but i m struck

how to check whether both my arrays have same ascii values?

- - - Updated - - -

proteus.mmc file is of 1.84gb because i m using a 2gb memory card. i used usbimage tool for making image of memory card named procard.mmc
yes this code is causing problem
for(j=0;j<row;j++)
{
//UART1_Write(13);
for (k=0;k<16;k++)
{
tmp2[k]=cnics_array[j][k];
}
//UART1_Write_text(cnics_array[j][k]);
UART1_Write_text(tmp2);
UART1_Write(13);
UART1_Write(13);
}

i m using this
for(j=0;j<row;j++)
so that all rows sholud be read....

while(1) is for all cnic to be read and break is for if statement that if dosen't match it will return 0 and make the bit high and break


my whole code is below

///////////////////////////////////////////code/////////////////////////////////////////////////////////////////////////////////////

#include"image.h"
#define max_character 18 // length of barcode
/*************************SDCARD************************/
sbit Mmc_Chip_Select at RC0_bit;
sbit Mmc_Chip_Select_Direction at TRISC0_bit;

/**************************glcd***************************/
char Glcd_DataPort at PORTD;

sbit Glcd_CS1 at RB5_bit; // GLCD chip select 1
sbit Glcd_CS2 at RB6_bit; // GLCD chip select 2
sbit Glcd_RS at RB2_bit; // GLCD data/instruction
sbit Glcd_RW at RB3_bit; // GLCD read/write
sbit Glcd_EN at RB4_bit; // GLCD enable
sbit Glcd_RST at RB7_bit; // GLCD reset

sbit Glcd_CS1_Direction at TRISB5_bit;
sbit Glcd_CS2_Direction at TRISB6_bit;
sbit Glcd_RS_Direction at TRISB2_bit;
sbit Glcd_RW_Direction at TRISB3_bit;
sbit Glcd_EN_Direction at TRISB4_bit;
sbit Glcd_RST_Direction at TRISB7_bit;

/*******************BARCODE****************************/
sbit PS2_Data at RA3_bit;
sbit PS2_Clock at RA4_bit;
sbit PS2_Data_Direction at TRISA3_bit;
sbit PS2_Clock_Direction at TRISA4_bit;
/*********************************************************/
unsigned short keydata = 0, special = 0, down = 0;
char fat_txt[20] = "FAT16 not found";
char filename[11] = "voter1.txt"; // File names
char character='l';
unsigned long size;
char Buffer[512];
char txt[] = "34101-5508931-7";
char text[] ="34101-5508931-7";
char databuffer[100];
char cnics[160];
char tmp2[18];
char cnics_array[10][16];
int res;
int first=0,second=0,chk=0,row=0,col=0;
char tmp[4];
bit b_Duplicate ;
int cn;
int cni;
int counter = 0; // counter of barcode characters
char barcode [max_character]; // barcode string
char *temp_arr;
unsigned char newStr[50];

///////////////////////////file read//////////////////////////////////

void M_Open_File_Read()
{
int j,k;
Mmc_Fat_Assign(&filename, 0);
Mmc_Fat_Reset(&size);
//for (i=1;second<11;i++)
Mmc_Fat_Read(&character);
while(character!='*')

{

if(character==' ')
while(character!=13)
{
Mmc_Fat_Read(&character);
if(character=='*')
chk=1;
}
cnics[first]=character;
++first;
//UART1_Write(character);
Mmc_Fat_Read(&character);
if(chk==1)
break;

}

//UART1_Write_text(cnics);

for(i=0;i<first;i++)
{
if(cnics==13)
{
cnics_array[row][col]='\0'; ++row; col=0; ++i;
}

cnics_array[row][col]= cnics;
col++;
}

//UART1_Write_text(cnics);

for(j=0;j<row;j++)
{
//UART1_Write(13);
for (k=0;k<16;k++)
{
tmp2[k]=cnics_array[j][k];
}
//UART1_Write_text(cnics_array[j][k]);
UART1_Write_text(tmp2);
UART1_Write(13);
// UART1_Write(13);
}

}
///////////////////////////////////barcode scan///////////////////////////////

void Barcodescan()
{ memset(barcode, 0, max_character); // all character set to 0


if (Ps2_Key_Read(&keydata, &special, &down))
{
if (down && (keydata == 13))
{ // Enter
// Glcd_Fill(0);
//Glcd_Write_Text(barcode, 0, 0, 1);
memset(barcode, 0, max_character);
counter = 0;
// UART1_Write(text);

}
else if (down && (keydata == 16))
{
counter--;
barcode[counter] = 0;
// set previous charater to 0
}
else if (down && !special && keydata)
{ // other keys
barcode[counter++] = keydata;


}
}

}

///////////////////////////////////verification/////////////////////////////////

int VerifyDuplicateVoter()

{ int cni = 0;
int cnii = 0;
M_Open_File_Read();
Barcodescan();
b_Duplicate = 0;
while(1)
{
for (cn = 0; cn <= 15; cn++)
{

// if ((cnics[cn] != text[cn]))
//if (barcode[CNIC] != tmp2[CNIC])
if (tmp2[cn] ==txt[cn]) // if any character is not matching, make flag 0 and exit for loop
{ cni++;

}
}
if(cni==15)
{
return 1;
// UART1_Write('w');
b_Duplicate = 1 ;
break;
}
else
{

return 0;
}

}


}


//-------------- Main. Uncomment the function(s) to test the desired operation(s)

void main()
{
PORTB = 0;
TRISB = 0;
PORTA = 1;
TRISA = 1;
TRISD=0;
TRISC =0;
PORTC = 0x02;
UART1_Init(9600);
Ps2_Config();
Glcd_Init();

Glcd_Fill(0xff);

Glcd_Set_Font(Font_Glcd_Character8x7, 8, 7, 32);

Ps2_Config();
delay_ms(1000);



SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);


if (Mmc_Fat_Init ()==0)
{
// reinitialize spi at higher speed

SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

//M_Open_File_Read();
res = VerifyDuplicateVoter();
if (res == 0)
{

UART1_Write_text("Warning ! not matched !");
Glcd_Write_Text("you can't cast vote", 3, 3, 0);
}
else
{

UART1_Write_text("can cast vote!");
Glcd_Write_Text("Welcome To Voting Machine", 3, 3, 0);

}

}

else if (Mmc_Fat_Init()== 1)
{
UART1_Write('y');
}

else
{
UART1_Write('z');

}

}
 

Edit your last post and post the code between code tags. Press the # button and you will get the code tags.

i m using this
for(j=0;j<row;j++)
so that all rows sholud be read....

Then you should make row to max before the above code like this, other wise if row = 0 then for(j=0;j<row;j++) will not be executed.

if row is 10, let us say

row = 10;
i m using this
for(j=0;j<row;j++)
so that all rows sholud be read....

then what happenes is

according to your code below

Code:
for(j=0;j<row;j++)
{
//UART1_Write(13);
for (k=0;k<16;k++)
{
tmp2[k]=cnics_array[j][k];
}
//UART1_Write_text(cnics_array[j][k]);
UART1_Write_text(tmp2);
UART1_Write(13);
UART1_Write(13);
}


tmp2[k] is assigned cnics_array[0][0..15];
then it assigns
tmp2[k] = cnics_array[1][0..15];
finally if your row count is 10 then
tmp2[k] = cnics_array[10][0..15] is performed.
Every time the old value of tmp2[k] is overritten with new values as row changes.

So, finally the value you get for tmp2[k] is the value of last row, in this example cnics_array[10][0..15]

It is not doing anything other than assigning 2d array to 1d array continuosly, without performing any other comparison.

proteus.mmc file is of 1.84gb because i m using a 2gb memory card. i used usbimage tool for making image of memory card named procard.mmc

You dont have to make a 2 gb .mmc file. you can use the .mmc file I sent for simulation purpose. If your sd card is 2gb doesn't mean that you have to use a 2gb .mmc file.

how to check whether both my arrays have same ascii values?

I have mentioned in post 3 how to compare 2 arrays. The same method is used to compare arrays holding ascii values.

How are you finding how many rows there are?

array or string matching

if

arry2d[0][0] = '1'
arry2d[0][1] = '3'
arry2d[0][2] = '0'
arry2d[0][3] = '4'
arry2d[0][4] = '-'
arry2d[0][5] = '7'
arry2d[0][6] = '1'
arry2d[0][7] = '4'
arry2d[0][8] = '8'
arry2d[0][9] = '5'
arry2d[0][10] = '6'
arry2d[0][11] = '2'
arry2d[0][12] = '8'
arry2d[0][13] = '-'
arry2d[0][14] = '7'
arry2d[0][15] = '\0'

and

arry1d[0] = '1'
arry1d[1] = '3'
arry1d[2] = '0'
arry1d[3] = '4'
arry1d[4] = '-'
arry1d[5] = '7'
arry1d[6] = '1'
arry1d[7] = '4'
arry1d[8] = '8'
arry1d[9] = '5'
arry1d[10] = '6'
arry1d[11] = '2'
arry1d[12] = '8'
arry1d[13] = '-'
arry1d[14] = '7'
arry1d[15] = '\0'

i = 0;
j = 0;
k = 0;

for(j=0;j<15;j++0) {
if(arry2d[j] == arry1d[j]) {
k++;
}
}

if(k == 15) { //if array elements are matched
//then do whatever you want
}


another method is

unsigned int result;

result = strcmp(arry1, arry2);

result = 0 if arrays match. Both arrays should be of same dimension and last element of both arrays should be '\0'.


Try this code and see what it displays on Virtual Terminal.
Here cnics[] is assigned to cnics_array[][] and then cnics_array[][] is assigned to tmp2[] and printed.

Code:
sbit Mmc_Chip_Select at RC0_bit;
sbit Mmc_Chip_Select_Direction at TRISC0_bit;

unsigned short keydata = 0, special = 0, down = 0;
char fat_txt[20] = "FAT16 not found";
char filename[11] = "voter1.txt"; // File names
char character='l';
unsigned long size;
char Buffer[512];
char txt[] = "34101-5508931-7";
char text[] ="34101-5508931-7";
char databuffer[100];
char cnics[16];
char tmp2[16];
char cnics_array[10][16];
int res;
int first=0,second=0,chk=0;
char tmp[4];
bit b_Duplicate ;
int cn;
int cni;
int counter = 0; // counter of barcode characters
char *temp_arr;
unsigned char newStr[50];
unsigned int i, count, row, col = 0;

void M_Open_File_Read()
{
     unsigned int j = 0, k = 0;

     Mmc_Fat_Assign(&filename, 0);
     Mmc_Fat_Reset(&size);
     Mmc_Fat_Read(&character);

                while(character!='*') {
                         cnics[count] = character;
                         count++;
                         Mmc_Fat_Read(&character);
                }
                cnics[count] = '\0';
                UART_Write_Text("cnics = ");
                UART1_Write_Text(cnics);
                UART_Write(13);
                UART_Write(10);

     i = 0;
     row = 0;
     col = 0;

     for(i=0;i<=(count);i++) {

          if(cnics[i] == '\0')
          {
                cnics_array[row][col]='\0';
                row++; 
                col = 0;
          }
          else {
                  cnics_array[row][col]= cnics[i];
                  col++;
          }
     }

     for(j=0;j<1;j++) {
           for (k=0;k<16;k++) {

                tmp2[k] =  cnics_array[j][k];
           }
     }
     UART_Write_Text("tmp2 = ");
     UART1_Write_text(tmp2);
     UART1_Write(13);
     UART1_Write(10);
}

int VerifyDuplicateVoter()
{
            M_Open_File_Read();
}


void main()
{
     PORTB = 0;
     TRISB = 0;
     PORTA = 1;
     TRISA = 1;
     TRISD=0;
     TRISC =0;
     PORTC = 0x02;
     UART1_Init(9600);
     SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
     
     if (Mmc_Fat_Init ()==0)
     {
            SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
            res = VerifyDuplicateVoter();
     }
}

Check the 2 simulations. The codes are same for both the simulations, but voter1.txt in proteus.mmc contains text "34101-5508931-7*" and voter1.txt in proteus wrong data.mmc has text "34101-5508931-8*"

So they display the messages shown in the attached images.
 

Attachments

  • array.jpg
    array.jpg
    143.5 KB · Views: 149
  • sim ss 5.jpg
    sim ss 5.jpg
    141.3 KB · Views: 139
  • sim ss 6.jpg
    sim ss 6.jpg
    133.8 KB · Views: 141
Last edited:
i have changed my code a bit now i m trying to save my string array in 2 dimensional array.
cnics = contains cnics as string of cnics seperated by \n.
now i m trying to save that string using \n to seperate.
it is also giving problem.

Code:
void M_Open_File_Read()
{
  int j,k;
  int row =10;
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Reset(&size);


  Mmc_Fat_Read(&character);

  while(character!='*')
   
   {
   
    if(character==' ')
       while(character!=13) 
        {
         Mmc_Fat_Read(&character);

          if(character=='*') 
            chk=1;
        }

       cnics[first]=character;
       ++first;

    //UART1_Write(character);
      Mmc_Fat_Read(&character);

      if(chk==1) 
       break;


   }

for (j=0;j<first;j++)
{
   if(cnics[j] == 10)
   { 
    cnics_array[count][index+1] = '\0';

   count++;
   index = 0;

    }
  else
  {
  cnics_array[count][index] = cnics[j];
  index++;

  }          
}   UART1_Write_text(cnics_array);

 }
 

Code:
UART1_Write_text(cnics_array);
is wrong

UART_Write_Text() expects a string or a char array terminated with '\0' as argument, but you are trying to pass a 2d array as argument. :shock:


Try this code

Code:
void M_Open_File_Read()
{
  int j,k;
  int row =10;
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Reset(&size);


  Mmc_Fat_Read(&character);

  while(character!='\n')
   
   {
   
    if(character==' ')
       while(character!=13) 
        {
         Mmc_Fat_Read(&character);

          if(character=='\n') 
            chk=1;
        }

       cnics[first]=character;
       ++first;

    //UART1_Write(character);
      Mmc_Fat_Read(&character);

      if(chk==1) 
       break;


   }

for (j=0;j<first;j++)
{
   if(cnics[j] == '\n')
   { 
    cnics_array[count][index] = '\0';

   count++;
   index = 0;

    }
  else
  {
  cnics_array[count][index] = cnics[j];
  index++;

  }

or try this code

Code:
sbit Mmc_Chip_Select at RC0_bit;
sbit Mmc_Chip_Select_Direction at TRISC0_bit;

unsigned short keydata = 0, special = 0, down = 0;
char fat_txt[20] = "FAT16 not found";
char filename[11] = "voter1.txt"; // File names
char character='l';
unsigned long size;
char Buffer[512];
char txt[] = "34101-5508931-7";
char cnics[16];
char tmp2[16];
char cnics_array[10][16];
int res;
unsigned int i, count, row, col = 0;

void M_Open_File_Read()
{
     unsigned int j = 0, k = 0;

     Mmc_Fat_Assign(&filename, 0);
     Mmc_Fat_Reset(&size);
     Mmc_Fat_Read(&character);

                while(character!='\n') {
                         cnics[count] = character;
                         count++;
                         Mmc_Fat_Read(&character);
                }
                cnics[count] = '\0';
                UART_Write_Text("cnics = ");
                UART1_Write_Text(cnics);
                UART_Write(13);
                UART_Write(10);

     i = 0;
     row = 0;
     col = 0;

     for(i=0;i<=(count);i++) {

          if(cnics[i] == '\0')
          {
                cnics_array[row][col]='\0';
                row++;
                col = 0;
          }
          else {
                  cnics_array[row][col]= cnics[i];
                  col++;
          }
     }

     for(j=0;j<1;j++) {
           for (k=0;k<16;k++) {
                tmp2[k] =  cnics_array[j][k];
           }
     }
     UART_Write_Text("tmp2 = ");
     UART1_Write_text(tmp2);
     UART1_Write(13);
     UART1_Write(10);
}

int VerifyDuplicateVoter()
{
      unsigned int match_counter = 0;

      M_Open_File_Read();

      for(i=0;i<15;i++) {
             if(txt[i] == tmp2[i]) {
                    match_counter++;
             }
      }

      if(match_counter == 15) {
            UART_Write_Text("txt[] matches with tmp2[]");
      }
      else {
            UART_Write_Text("txt[] doesn't matches with tmp2[]");
      }
      UART1_Write(13);
      UART1_Write(10);
}


void main()
{
     PORTB = 0;
     TRISB = 0;
     PORTA = 1;
     TRISA = 1;
     TRISD=0;
     TRISC =0;
     TRISC.F6 = 0;
     TRISC.F7 = 1;
     PORTC = 0x02;
     UART1_Init(9600);
     SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

     if (Mmc_Fat_Init ()==0)
     {
            SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
     }

     res = VerifyDuplicateVoter();

}

Check your simulation which uses '\n' with these .mmc files
 

Attachments

  • sobia sarwar mmc.rar
    1.5 KB · Views: 155
Last edited:
above code is working the way you described but its just for first cnic. why not for all 10 cnics.
 

What do you mean by not working? Are you trying to compare 10 different cnics with txt[]? Are all the 10 cnics have the same data as txt[]. If yes it will match but you have to code such that it reads each cnic and compares it and prints the result. Have you stored 10 cnics in voter1.txt in .mmc file with each cnic terminating with '*' or CR?

Post your code which is used to compare 10 cnics.
 
the code you gave is working absolutely fine and thankx for that but what i m doing is that i have a file named voter1.txt containing data of all voters. i then use the below code to extract only cnics which are the first entries of the 10 rows.rest of the voters data is neede at the end for printer
Code:
int j,k;
  int row =10;
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Reset(&size);
  //for (i=1;second<11;i++)
  Mmc_Fat_Read(&character);
  while(character!='*')

   {

    if(character==' ')
       while(character!=13)
        {
         Mmc_Fat_Read(&character);
          if(character=='*')
            chk=1;

        }
       cnics[first]=character;
       ++first;

    //UART1_Write(character);
      Mmc_Fat_Read(&character);
      if(chk==1)
       break;
    }

it works fine i-e by the above code a string of cnics seperated by \n are saved in char cnics[].

now the problem is i am trying to save the above string in 2 d array so that i can pick index wise and compare
Code:
for (j=0;j<first;j++)
{
   if(cnics[j] == '\n')

   
   { 
    cnics_array[count][index+1] = '\0';

   count++;
   index = 0;

    }
  else
  {
  cnics_array[count][index] = cnics[j];
  index++;

  } 

}

the above code for 2 d array works fine but just for first cnics in the string as
Code:
UART1_Write_text(cnics_array[0]);
and when i used to access second cnic then result is that skipping the first one all cnics are displayed
Code:
UART1_Write_text(cnics_array[1]);

just wanted to do this

cnics_array[0][0..15];
cnics_array[1][0..15];
...................................
cnics_array[10][0..15];

- - - Updated - - -

how did you make proteus.mmc file?
 

See the attachment. It is not working fine. Might be a bug of Proteus. I will try to fix it.
I used WinImage Pro 8.5 to create .mmc files

Is your cnics in voter1.txt like mentioned below?

34101-5508931-7
34101-5508931-0
34101-5508931-1
34101-5508931-2
34101-5508931-3
34101-5508931-4
34101-5508931-5
34101-5508931-6
34101-5508931-7
34101-5508931-8
34101-5508931-9
 

Attachments

  • sobia sarwar v4.rar
    160.9 KB · Views: 113
yes i wanted to compare ten different cnics with txt[]
all 10 cnics have different values according to which i can change values of txt[] because its a raw data which can be changed.
no i didn't stored 10 cnics terminating with '*'. its just the cnics are extracted from voter1.txt

this code shows whole data of voter1.txt
Code:
Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Reset(&size);
  for (i=0;i<size;i++)
{
  Mmc_Fat_Read(&character);

Uart_Write(character);
}

then i extracted cnics as string in char cnics[]

- - - Updated - - -

no the cnics are like this

34101-5508931-7
34101-9759002-3
34101-4432710-7
34105-2759550-7
34101-9764655-9
34101-3564359-1
34101-9854213-7
34101-1026066-9
34101-4190836-9
34101-5608931-2
 

no i didn't stored 10 cnics terminating with '*'. its just the cnics are extracted from voter1.txt

Earlier you used these two statements
Code:
 while(character!='\n') while(character!='\*')
to check the end of a cnic. So I put '*' in the voter1.txt in .mmc file after each cnic. Later I changed it to CR i.e., '\n'

all 10 cnics have different values according to which i can change values of txt[] because its a raw data which can be changed.

How do you change the value of txt[]. Show an example.

no the cnics are like this

34101-5508931-7
34101-9759002-3
34101-4432710-7
34105-2759550-7
34101-9764655-9
34101-3564359-1
34101-9854213-7
34101-1026066-9
34101-4190836-9
34101-5608931-2

If you just want to read the 10 cnics from your voter1.txt and store it in a 2d array and compare it with txt[], then the code has to be modified. You don't have to use cnic[] and tmp2[]. You just have to use cnics_array[10][16] and txt[].
 

Attachments

  • sobia sarwar v6.rar
    160.9 KB · Views: 103
  • sobia sarwar v7.rar
    162.8 KB · Views: 106
  • sobia sarwar v8.rar
    162.7 KB · Views: 114
Last edited:
i have done saving my cnics in 2 d array just like this
cnics_array[0][0..15];
cnics_array[1][0..15];
...................................
cnics_array[10][0..15];

Code:
void M_Open_File_Read()
{
  int j,k;
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Reset(&size);
  //for (i=1;second<11;i++)
  Mmc_Fat_Read(&character);
  while(character!='*')
   
   {
   
    if(character==' ')
       while(character!=13) 
        {
         Mmc_Fat_Read(&character);
          if(character=='*') 
            chk=1;
        }
       cnics[first]=character;
       ++first;
    //UART1_Write(character);
      Mmc_Fat_Read(&character);
      if(chk==1) 
       break;
   
   }
   
  // UART1_Write_text(cnics);
   
    for (j=0;j<first;j++)
{
   if(cnics[j] == '\r')


   {
    cnics_array[count][index+1] = '\0';

   count++;
   index = 0;

    }
  else
  {
  cnics_array[count][index] = cnics[j];
  index++;

  }

}    
 UART1_Write_text(cnics);

     UART1_Write(13);
    UART1_Write_text("************************************");
     UART1_Write(13);
   UART1_Write(cnics_array[0][0]);
   UART1_Write(cnics_array[0][1]);
   UART1_Write(cnics_array[0][2]);
   UART1_Write(cnics_array[0][3]);
   UART1_Write(cnics_array[0][4]);
   UART1_Write(cnics_array[0][5]);
   UART1_Write(cnics_array[0][6]);
   UART1_Write(cnics_array[0][7]);
   UART1_Write(cnics_array[0][8]);
   UART1_Write(cnics_array[0][9]);
   UART1_Write(cnics_array[0][10]);
   UART1_Write(cnics_array[0][11]);
   UART1_Write(cnics_array[0][12]);
   UART1_Write(cnics_array[0][13]);
   UART1_Write(cnics_array[0][14]);
   UART1_Write(13);
   UART1_Write(13);
    UART1_Write(cnics_array[1][0]);
   UART1_Write(cnics_array[1][1]);
   UART1_Write(cnics_array[1][2]);
   UART1_Write(cnics_array[1][3]);
   UART1_Write(cnics_array[1][4]);
   UART1_Write(cnics_array[1][5]);
   UART1_Write(cnics_array[1][6]);
   UART1_Write(cnics_array[1][7]);
   UART1_Write(cnics_array[1][8]);
   UART1_Write(cnics_array[1][9]);
   UART1_Write(cnics_array[1][10]);
   UART1_Write(cnics_array[1][11]);
   UART1_Write(cnics_array[1][12]);
   UART1_Write(cnics_array[1][13]);
    UART1_Write(cnics_array[1][14]);
    UART1_Write(cnics_array[1][15]);
    UART1_Write(13);
    UART1_Write(13);
    UART1_Write(cnics_array[2][0]);
   UART1_Write(cnics_array[2][1]);
   UART1_Write(cnics_array[2][2]);
   UART1_Write(cnics_array[2][3]);
   UART1_Write(cnics_array[2][4]);
   UART1_Write(cnics_array[2][5]);
   UART1_Write(cnics_array[2][6]);
   UART1_Write(cnics_array[2][7]);
   UART1_Write(cnics_array[2][8]);
   UART1_Write(cnics_array[2][9]);
   UART1_Write(cnics_array[2][10]);
   UART1_Write(cnics_array[2][11]);
   UART1_Write(cnics_array[2][12]);
   UART1_Write(cnics_array[2][13]);
    UART1_Write(cnics_array[2][14]);
    UART1_Write(cnics_array[2][15]);
    UART1_Write(13);
    UART1_Write(13);
    UART1_Write(cnics_array[3][0]);
   UART1_Write(cnics_array[3][1]);
   UART1_Write(cnics_array[3][2]);
   UART1_Write(cnics_array[3][3]);
   UART1_Write(cnics_array[3][4]);
   UART1_Write(cnics_array[3][5]);
   UART1_Write(cnics_array[3][6]);
   UART1_Write(cnics_array[3][7]);
   UART1_Write(cnics_array[3][8]);
   UART1_Write(cnics_array[3][9]);
   UART1_Write(cnics_array[3][10]);
   UART1_Write(cnics_array[3][11]);
   UART1_Write(cnics_array[3][12]);
   UART1_Write(cnics_array[3][13]);
    UART1_Write(cnics_array[3][14]);
    UART1_Write(cnics_array[3][15]);
    UART1_Write(13);
    UART1_Write(13);
   }

now i move on to comparing
 

Check this simulation and project files. I have written a function to print 2D array.

Why have you passing each element of 2d array to UART_Write() function? 8-O


Here is the code to send 2D array to UART.

You can call the function like this
Code:
 UART_Write_2DArray(cnics_array, row, col);

row = value of row to be printed. If you want to print 0th row then row = 0.
col = value of no. of columns in that row. If array is array[10][16] then col = 16.

Code:
void UART_Write_2DArray(char TwoDArray[][16], unsigned int _row, unsigned int _col) {
           for(i=0;i<=_col;i++) {
                 TXREG = TwoDArray[_row][i];
                 while(!PIR1.TXIF);
                 PIR1.TXIF = 0;
                 PIE1.TXIE = 1;
           }
}

Try this code to compare arrays i.e., cnic_array[][] and txt[]

Code:
[COLOR="#0000FF"]

void UART_Write_2DArray(char _2DArray[10][16], unsigned int _row, unsigned int _col);
void compare_arrays(unsigned char _2Darray[10][16], unsigned char _array[16]);

unsigned int i = 0, j = 0, count = 0, row = 0, col = 0, _row = 0, _col = 0;
unsigned char _2DArrayDArray[10][16];

void compare_arrays(unsigned char _2Darray[10][16], unsigned char _array[16]) {
	match_counter = 0;
	row = 0;

	for(row=0;row<10;row++) {
		for(col=0;col<15;col++) {

			if(txt[col] == cnics_array[row][col]) {
				match_counter++;
			}
		}
		UART_Write_2DArray(cinic_array, row, col);
		if(match_counter == 15) {
			UART_Write_Text("txt[] matches with cnic");
        	}
        	else {
            		UART_Write_Text("txt[] doesn't match with cnic");
        	}
	}
}


void UART_Write_2DArray(char _2DArray[][16], unsigned int _row, unsigned int _col) {
           for(i=0;i<=_col;i++) {
                 TXREG = _2DArray[_row][i];
                 while(!PIR1.TXIF); //(while(!TXSTA.TRMT); TXSTA>TRMT = 0;
                 PIR1.TXIF = 0;
                 PIE1.TXIE = 1;
           }
}

void main() {

	compare_arrays(cnic_array, txt);

}

[/COLOR]
 

Attachments

  • sobia sarwar v9.rar
    163.9 KB · Views: 118
  • sim ss 9.jpg
    sim ss 9.jpg
    377.1 KB · Views: 136
Last edited:

i wanted to know that your proteus.mmc file contains just the cnics or whole data of voter1.txt

- - - Updated - - -

i wanted to know that your proteus.mmc file contains just the cnics or whole data of voter1.txt.plz reply i m doing it right now..
 

My proteus.mmc file contains voter1.txt file. The contents of voter1.txt file is as mentioned below.



34101-5508931-7
34101-9759002-3
34101-4432710-7
34105-2759550-7
34101-9764655-9
34101-3564359-1
34101-9854213-7
34101-1026066-9
34101-4190836-9
34101-5608931-2

 

can you please tell me how that code can be implemented for my voter1.txt file i uploaded before
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top