⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 主要是用VC++编写的卫星云图的的帧解码及画图程序
💻 C
字号:
/*++

  
    Module Name:
    Main.c
    
                  
                    
--*/

#include <windows.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>

#include "resource.h"
#include "main.h"

#include <winioctl.h>
#include "ezusbsys.h"

HINSTANCE hGInstance = NULL;
BOOL CALLBACK bMainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL StopTest;

char *build_time = __TIME__;
char *build_date = __DATE__;

#define mBufSize 2	//0xf000
unsigned char buf[ mBufSize ];
unsigned int guiIndexW=0;
unsigned int guiIndexR=0;

unsigned char FrameBuf[0x9800];


unsigned char gCRC[16];          //尾随机序列
unsigned char gucOddByte=0xff;
unsigned long len=0;

#define revBufSize	64000
unsigned int revBufWp=0;
unsigned int revBufRp=0;
unsigned char revBuf[ revBufSize ];
unsigned char ProFlag=0;

//FILE *fp;

HWND hWnd;

long WINAPI WndProc( HWND hWnd, UINT iMessage, UINT wParam, LONG lParam );

BOOL InitWindowsClass( HINSTANCE hInstance );
BOOL InitWindows( HINSTANCE hInstance,int nCmdShow );
HWND hWndMain;
void start_iso( void );
void read_iso( void );
void stop_iso( void );
/*******************************
*WinMain: Windows Entry point  *
********************************/
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,
                   int       nCmdShow)
{
    MSG Message;
	hGInstance=hInstance;

	//START DIALOGBOX AS THREAD

	start_iso();

	if( !InitWindowsClass(hInstance) )
		return FALSE;
	if( !InitWindows(hInstance,nCmdShow))
		return FALSE;
	_beginthread( (void *)read_iso, 0, NULL);

   	while( GetMessage(&Message,0,0,0) )
	{
		TranslateMessage(&Message);
		DispatchMessage(&Message);
	}
 
   return 0;
}

BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
    //HWND hWnd;
	hWnd = CreateWindow("WinFill","abc",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
	if(!hWnd)
		return FALSE;
	hWndMain = hWnd;
	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);
	return TRUE;
}

BOOL InitWindowsClass(HINSTANCE hInstance)
{
	WNDCLASS WndClass;
	WndClass.cbClsExtra=0;
	WndClass.cbWndExtra=0;
	WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
	WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	WndClass.hIcon=LoadIcon(NULL,"END");
	WndClass.hInstance=hInstance;
	WndClass.lpfnWndProc=WndProc;
	WndClass.lpszClassName="WinFill";
	WndClass.lpszMenuName=NULL;
	WndClass.style=CS_HREDRAW|CS_VREDRAW;
	return RegisterClass(&WndClass);
}

char GetBit( void )                              //一次收一位
{
	static unsigned char IndexBit=0;
	static int InByte;
	unsigned char ValueRet;
	if( IndexBit == 0 )
	{
		if( revBufRp == revBufWp ) 
		{
			return( -1 );
		}
		InByte = revBuf[ revBufRp ];
		if( ++revBufRp >= revBufSize ) revBufRp=0;
		IndexBit=0x80;
	}
	if( InByte & IndexBit ) ValueRet=1; else ValueRet=0;
	IndexBit >>= 1;
	return( ValueRet );
}

unsigned char WaitFrameStart( void )
{
	static unsigned char i;

	while(1)
	{
		switch( GetBit() )
		{
			case -1: return(0);
			case  0: i=24; break;
			case  1: if(--i == 0 ){ i=25; return(1); }
		}
	}
}

unsigned int GetByte( void )                 //收一个字节
{
	static unsigned char Bit8Cnt=8;
	static unsigned char ValueRet;
	for( ; Bit8Cnt!=0; Bit8Cnt-- )
	{
		unsigned char i;
		char dat;
		dat = GetBit();
		if( dat==-1 ) return( 0x5aa5 );
		ValueRet <<= 1;
		if( dat ^ gCRC[0] ) ValueRet |= 0x01;
		for(i=15; i!=0; i--) gCRC[i]=gCRC[i-1];
		gCRC[0]=gCRC[15] ^ gCRC[14];

	}
	Bit8Cnt=8;
	gucOddByte ^= 0xff;
	return( ValueRet ^ gucOddByte );
}

unsigned char GetFrameData( void )
{
	static unsigned int i;
	static unsigned char sts=0;
	unsigned int dat;

	switch( sts )
	{
	case 0: if( WaitFrameStart()==0 ) return(0);                                      //找寻帧头
		gucOddByte = 0xff;
		gCRC[0]=0x00;
		for(i=1; i<16; i++) gCRC[i]=0x01;
		sts=1;
		i=0;
	case 1:
		for( ; i<0x9800; i++)
		{
			dat = GetByte();
			if( dat==0x5aa5 ) return( 0 );
			FrameBuf[i]=(unsigned char)dat;
		}
		sts=0;
	}
	return( 1 );
}

long WINAPI WndProc( HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{
	static HDC hDC;
	PAINTSTRUCT PtStr;

///////////////////////////////////////////////////
	/*
	if((fp = fopen("savedata.dat","rb")) == NULL)
	{
	//	printf("cannot open file\n");
		exit(0);
	}
	//*/

///////////////////////////////////////////////////

	switch(iMessage)
	{
		static unsigned int i=0;
		static unsigned int y=10;
		int color;
		int x;
		//*
	case WM_PAINT:
		{
			static unsigned char sts=0;
			switch( sts )
			{
				case 0:
					hDC=BeginPaint(hWnd,&PtStr);
					SetMapMode(hDC,MM_ANISOTROPIC);
					sts = 1;
				case 1:	
					while( GetFrameData() != 0 )
					{
						for(x=0; x<610; x++ )
						{
							color= FrameBuf[4*x+2551];
							SetPixel(hDC,x+15, y, RGB(color,20,10));				//在坐标( X+15,Y)打点显示
						}
						if(++y>=480) y=10;							//满屏后,卷回屏幕首行显示
					}
					ProFlag=0;
			}
		}
		//*/
		//SetPixel(hDC,i++, y++, RGB(revBuf,20,10));
		//EndPaint(hWnd,&PtStr);
		return 0;
		
	case WM_DESTROY:
		stop_iso();
		PostQuitMessage(0);
		return 0;
	default:
		return(DefWindowProc(hWnd,iMessage,wParam,lParam));
	}
}

void CopyData( unsigned char *ptr, unsigned char len )
{
	while( len != 0 )
	{
		revBuf[ revBufWp ] = *ptr;
		ptr++;
		if( ++revBufWp >= revBufSize ) revBufWp=0;
		len--;
	}
}

void read_iso( void )
{
   	 /* 
	 int ch;
	 int sh;
	 BOOL ret;

	 for( sh=0; sh<1000; sh++)
	 {
		 for( ch=0; ch<500; ch++)
		 {
			revBuf=ch;//fgetc( fp );
			SendMessage(hWnd,WM_PAINT,0,0);
			//ret=PostMessage(hWnd,WM_PAINT,0,0);
			
		 }
	 }
}
*/
//*	
	
	HANDLE  hDevice                         = NULL;
   BOOLEAN bResult                         = FALSE;
   int     nBytes                          = 0;
   ISO_TRANSFER_CONTROL 	IsoControl;
   ULONG                   bytesToRead;
   PUCHAR                  buffer = NULL;
   ULONG                   packetsRead;
   PUSBD_ISO_PACKET_DESCRIPTOR isoDesc;
   ULONG                   i;
   PUCHAR                  ptr;
   ULONG                   pass = 0;
   UCHAR                   count = 0;
   ULONG                   framesRead = 0;
   ULONG                   framesMissed = 0;
   
   FILE	*fp;
   
//   int temp;
//   static int i;

	//return;
   if (bOpenDriver (&hDevice, "Ezusb-0") != TRUE) return;

   IsoControl.PacketSize = 85;
   IsoControl.PacketCount =400;
   IsoControl.PipeNum = 7;
   IsoControl.BufferCount = 2;
   IsoControl.FramesPerBuffer = 2;

   bytesToRead = IsoControl.PacketCount * (IsoControl.PacketSize + sizeof(USBD_ISO_PACKET_DESCRIPTOR));
   buffer = (PUCHAR) malloc(bytesToRead);


   if (!buffer)
   {
      CloseHandle(hDevice);
      return;
   }
   
	if((fp = fopen("savedata","wb")) == NULL)
	{
		exit(0);
	}
   
	while (!StopTest)
	{
      bResult = DeviceIoControl (hDevice,
                     IOCTL_EZUSB_READ_ISO_BUFFER,
                     &IsoControl,
                     sizeof(ISO_TRANSFER_CONTROL),
                     buffer,
                     bytesToRead,
                     &nBytes,
                     NULL);

      if (bResult != TRUE) continue;

      packetsRead = nBytes / (IsoControl.PacketSize + sizeof(USBD_ISO_PACKET_DESCRIPTOR));

      ptr = buffer;
      isoDesc = (PUSBD_ISO_PACKET_DESCRIPTOR) (buffer + (packetsRead * IsoControl.PacketSize));
	
	
/*		if((fp = fopen("savedata.dat","rb")) == NULL)
		{
			exit(0);
		}
		while(1)
		{
			while( ProFlag==1 );
			for( i=0; i<1000; i++ )
				
				if( (revBuf[i] = fgetc( fp )) == EOF )
				{
					fclose( fp );
					return;
				}
				
			ProFlag = 1;
			SendMessage(hWnd,WM_PAINT,0,0);
		}
*/

      for (i=0; i<packetsRead; i++)
      {
         framesRead++;
		 fwrite(ptr+2,*(ptr+1),1,fp);

		 CopyData( ptr+2, *(ptr+1) );


	     if (count != *ptr)
         {
			framesRead=0L;
            count = *ptr;
         }

         count++;
         ptr += IsoControl.PacketSize;
      }
	  PostMessage(hWnd,WM_PAINT,0,0);
   
   }
	fclose( fp );   
   //CloseHandle (hDevice);
   free(buffer);
}

/**************************************************
* Main Dialog proc                               *
**************************************************/

void start_iso( void )
{
   HANDLE  hDevice                         = NULL;
   BOOLEAN bResult                         = FALSE;
   int     nBytes                          = 0;
   int     nItems                          = 0;
   HFONT   hFont                           = NULL;
   ULONG   ulLength                         = 0;
   
      
     ISO_TRANSFER_CONTROL 	IsoControl;
     ULONG                   pipenum;
     ULONG                   count=0;

     if (bOpenDriver (&hDevice, "Ezusb-0") != TRUE) return;

     IsoControl.PacketSize = 85;
     pipenum = 7;

     bResult = DeviceIoControl (hDevice,
                 IOCTL_Ezusb_RESETPIPE,
                 &pipenum,
                 sizeof(ULONG),
                 NULL,
                 0,
                 &nBytes,
                 NULL);

     if (bResult != TRUE)
     {
        CloseHandle (hDevice);
        return;
     }
     // perform the ISO transfer
     IsoControl.PacketCount = 1000;
     IsoControl.PipeNum = 7;
     IsoControl.BufferCount = 2;
     IsoControl.FramesPerBuffer = 2;

     bResult = DeviceIoControl (hDevice,
                    IOCTL_EZUSB_START_ISO_STREAM,
                    &IsoControl,
                    sizeof(ISO_TRANSFER_CONTROL),
                    NULL,
                    0,
                    &nBytes,
                    NULL);


     CloseHandle (hDevice);
}


void stop_iso( void )
{
   HANDLE  hDevice                         = NULL;
   int     nBytes                          = 0;
   BOOLEAN bResult                         = FALSE;

	// Open the driver
	if (bOpenDriver (&hDevice, "Ezusb-0") == FALSE) return;

	bResult = DeviceIoControl (hDevice,
				IOCTL_EZUSB_STOP_ISO_STREAM,
				NULL,
				0,
				NULL,
				0,
				&nBytes,
				NULL);

	if( bResult == FALSE ){ ; }
	CloseHandle (hDevice);
}


#define BYTES_PER_LINE 0x10


/**************************************************
* bOpenDriver proc                               *
*                                                *
* Purpose:                                       *  
*      Opens the device driver using symbolic    *
*      name provided                             *
*                                                *
* Input:                                         *
*      phDeviceHandle:                           *
*          Pointer to Device Driver handle where *
*          the file handle is placed.            *
*      devname:                                  *
*          Null terminated string containing the *
*          device name                           *
*                                                *
* Return Value:                                  *
*      Boolean that indicates if the driver was  *
*      successfully opened or not.               *
*                                                *
**************************************************/

BOOLEAN
bOpenDriver (HANDLE * phDeviceHandle, PCHAR devname)
{
   char completeDeviceName[64] = "";
   char pcMsg[64] = "";
   
   strcat (completeDeviceName,
      "\\\\.\\"
      );
   
   strcat (completeDeviceName,
		    devname
          );
   
   *phDeviceHandle = CreateFile(   completeDeviceName,
      GENERIC_WRITE,
      FILE_SHARE_WRITE,
      NULL,
      OPEN_EXISTING,
      0,
      NULL);
   
   if (*phDeviceHandle == INVALID_HANDLE_VALUE) {
      return (FALSE);
   } else {
      return (TRUE);
   } /*else*/
   
   
}//OpenDevice




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -