본문 바로가기

프로젝트/휴대폰을 BarCode Reader 로 사용하여 C# DB에서 이용하기

아두이노 DB와 휴대폰 바코드 연동하여 TFT LCD에 출력하기

동영상 확인하기

 

 

#define   _TFTLCD_      myLcd              // TFT LCD 이름 정의
#define _INDEX_TYPE_2_                     // 인덱스방법 2로 사용시
                                           // 방법 1로 사용시 주석처리 하면 됨                                            
#include  "D:\\Program Files (x86)\\Arduino\\myHeader\\HanGul_TFT.H"
                                                                              
                                         
HanGul_TFT  ht;     // 한글출력 클래스

#define dbFileName  "BarCode.txt"
File    dbFile;

struct Record  // 레코드 구조체

     String   barCode; 
     String   productName; 
}; 

String readLine()
{
  char buff[50];
  int idx=0;
  
  while(true)
  {
     byte b,b2;

         b=dbFile.read();        
        if( b == 0x0d )
        {
            b2=  dbFile.read();          
            if( b2 == 0x0a )
            {             
               buff[idx]= 0;
               return( String( buff ) );
            }
            buff[idx++] = b;
            buff[idx++] = b2;
        }
        else buff[idx++] =b;        
  }
}

void addRecord( String _barCode, String _productName )
{   
    dbFile = SD.open(dbFileName,FILE_WRITE); 
    dbFile.println( _barCode );
    dbFile.println( _productName );    
    dbFile.close();
           
}

Record findRecord( String  _barCode)
{
  Record rec;  
  dbFile = SD.open(dbFileName);  
  while( dbFile.available() )
  {    
      if( _barCode == readLine() )
        {
          rec.barCode=_barCode;
          rec.productName=readLine();
          dbFile.close();
          return( rec );
        }
      readLine();
   }
  dbFile.close(); 
  rec.barCode="-1"; 
  return( rec); 
} // func



bool togColor =true;

void setup()
{     
     
  ht.start();       // 한글출력 클래스 시작   
                                    
  myLcd.InitLCD();            
  myLcd.setRotation(3);   //3 가로 480x320  //2 세로   320x480
  myLcd.clrScr();   
  myLcd.fillScreen(cGreen);
  ht.color(cWhite,cBlue);
  ht.print(F("휴대폰 바코드 연동 DB "),0,0,2,3);
  ht.under(cWhite); 
  ht.color(cWhite,cGreen);
  ht.print(F("메세지:"),5,100,2);
  ht.print(F("바코드:"),5,170,2);
  ht.print(F("상품명:"),5,220,2);
  ht.color(cYellow);
  ht.print(F("블루투스 연결 대기중"),130,100,2);
  Serial.begin(9600);
  while( !Serial.available() ) 
  {
    if( togColor ) ht.fill();  
    else ht.print(F("블루투스 연결 대기중"),130,100,2);
    togColor = ! togColor;
    delay(700);
  }
  ht.print(F("블루투스 연결됨       "),130,100,2);
}

String temp;

void loop()
{
 String msg,cmd;
 Record rec;
   
 if(Serial.available() )
 {
  msg=Serial.readString();
  cmd=msg.substring(0,1);
  msg=msg.substring(1);
 
  if( cmd[0]=='B' )
  {
       rec=findRecord(msg);
       if( rec.barCode =="-1" ) 
       { 
            ht.print(F("등록이 필요한 바코드          "),130,100,2);
            temp=msg;   
            ht.print(msg+"                ",130,178);
            ht.print("                         ",130,220,2);
            Serial.print("-1");
        }
        else
        {
            ht.print(F("검색 되었습니다.              "),130,100,2);  
            ht.print(rec.barCode+"               ",130,178);
            ht.print(rec.productName+"           ",130,220,2);
            Serial.print(rec.productName);
        }
  } else if( cmd[0] =='A')
  {     
         addRecord(temp,msg);    
         ht.print(F("등록 되었습니다.     "),130,100,2);     
         ht.print(temp+"              ",130,178);     
         ht.print(msg+"             ",130,220,2);
         Serial.print("1");
  }
  
 
 }
  
  
}//loop