简易串口读取代码

/********[email protected]*******/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define COM "COM9"/******must change here by your com_id*******/
#undef DEBUG_ENABLE    
#ifdef DEBUG_ENABLE
  #define Debug(fmt,args...) printf("[zpx debug] "fmt"\n",##args)    
#else
  #define Debug(fmt,args...)
#endif
#define MAX_RANGE(x,y) ((x)>(y)?(y):(x))
static int image_full_flag=0;
unsigned char buff[256*256];

int  read_com(HANDLE hFile)
{
  DWORD dwNumRead=0;
  DWORD dwErr=0;
  int b=2,i;
  static int firstFrame=1;
  static int pixSum=0; 
  COMSTAT comStat;
  b=ClearCommError(hFile,&dwErr,&comStat);
  if(comStat.cbInQue==0)return 1;  
  Debug("b=%d,dwErr=%d,comStat.cbInQue=%d",b,(int)dwErr,(int)comStat.cbInQue);
  OVERLAPPED readOverlapped;  
  readOverlapped.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
  //Sleep(10);
  b=ReadFile(hFile,buff,comStat.cbInQue,&dwNumRead,&readOverlapped);
  Debug("ReadFile=%d",b); 
  if(!b)
  {
    if(GetLastError()==ERROR_IO_PENDING){  
       Debug("GetLastError==ERR_IO_PENDING==%ld",ERROR_IO_PENDING); 
       GetOverlappedResult(hFile,&readOverlapped,&dwNumRead,TRUE); 
    }            
    else {
       Debug("GetLastError()==%d",(int)GetLastError());
       free(buff);
       return 0;    
    }        
  }   
  Debug("dwNumRead=%d",(int)dwNumRead);
  Debug("\n==================show msg==========================\n");
  for(i=0;i<comStat.cbInQue;i++)
               printf("%X ",buff[i]);
  Debug("\n==================show msg==========================\n");
/******you should set MY_DATA_PROCESS undef**/
#define MY_DATA_PROCESS
#ifdef MY_DATA_PROCESS  
  pixSum+=comStat.cbInQue;
  if(firstFrame!=0)
  {
    if(comStat.cbInQue==1)
    {
      if(firstFrame==2) 
      {     
         firstFrame=0;
         if(pixSum==96*96*2+2) { 
            image_full_flag=1;
            printf("\ndon't skip first frame\n");
            pixSum=0 ;
            return 1;
         }
         pixSum=0;
         return 0;
      }
      if(buff[comStat.cbInQue-1]>=0xfd)firstFrame=2 ;
    }
    else if(buff[comStat.cbInQue-1]==0xfd&&buff[comStat.cbInQue-2]==0xfe)
    {      
      firstFrame=0;
      if(pixSum==96*96*2+2) { 
            image_full_flag=1;
            printf("\ndon't skip first frame\n");
      }
      else printf("\nskip first frame\n");      
      pixSum=0;
    }    
  }
  else{  
      if(pixSum==96*96*2+2){ //must remove 2Byte data in the end ;
        pixSum=0;
        image_full_flag=1;
        printf("\nget full image\n");
      }  
  }
#endif
  return 1;
} 
void SetDcbInfo(HANDLE hFile,OVERLAPPED waitOverlapped)
{  
   int b;
   DWORD dwMask=EV_RXCHAR;   
   while(1)
   { 
     SetEvent(waitOverlapped.hEvent) ;
     WaitCommEvent(hFile,&dwMask,&waitOverlapped);
     WaitForSingleObject(waitOverlapped.hEvent,INFINITE);
     ResetEvent(waitOverlapped.hEvent);
     if(dwMask==EV_RXCHAR)
     {       
         b=read_com(hFile);
         if(b==1) break;       
     }     
   }  
}                                                                           
int main(int argc,char **argv)
{
  static HANDLE hFile;
  int getFileHandle=-1,cnt=0;
  while(1)
  { 
      hFile=CreateFile(COM,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
      getFileHandle=(int)hFile;
      if(getFileHandle!=-1)break;
      Debug("Can't get %s %d times,Please check the Device!",COM,cnt++);
      Sleep(1000);
  }                  
  Debug("hFile=%d",(int)hFile);
  DCB *dcb=(DCB*)malloc(1*sizeof(DCB));
  GetCommState(hFile,(LPDCB)dcb);
  dcb->BaudRate=CBR_57600;
  dcb->ByteSize=8;//数据位8bit
  dcb->StopBits=ONESTOPBIT;//停止位 1bit
  dcb->Parity=NOPARITY;// 校验位
  SetCommState(hFile,(LPDCB)dcb);   
  GetCommState(hFile,(LPDCB)dcb);
  PurgeComm(hFile,PURGE_RXCLEAR);
  SetCommMask(hFile,EV_RXCHAR);
  printf("wait for rs232 event\n");
  OVERLAPPED waitOverlapped;   
  waitOverlapped.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
  for(;;)
  {
     SetDcbInfo(hFile,waitOverlapped);
     if(image_full_flag)
     {
        image_full_flag=0;
        break;
      }
  }
  Debug("get rs232 event");
  system("pause");
  free(dcb);
  return 0;
}

验证结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/piao3956/article/details/70174485
今日推荐