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

📄 srecord.c

📁 56f8300E系列dsp的BOOTloader
💻 C
📖 第 1 页 / 共 3 页
字号:
				i = 0;
				while (*szBuffer && *szBuffer != '\n' && *szBuffer != '\r')
				{
					Writebuf[i] = *szBuffer;
					i++;
					szBuffer++;
				}
				Writebuf[i] = 0;
				if (strlen (Writebuf) != 0)
				{
					while (*szBuffer && *szBuffer != 'S')
					{
						Writebuf[i] = *szBuffer;
						i++;
						szBuffer++;
					}

				}
				Writebuf[i] = 0;
			}
				
			
			{
				ClearCommError( idComDev, &dwErrorFlags, &ComStat ) ; 
				
				if (strlen (Writebuf) != 0) 
				{
					while ((ComStat.cbOutQue + strlen (Writebuf)) > ( BUFFER_SIZE - 100) )
					{
						Sleep(1);	
						ClearCommError( idComDev, &dwErrorFlags, &ComStat ) ; 
					}
					res = WriteCommBlock( Writebuf, strlen (Writebuf) );
					if (flagError)
						break;
				}

	
				res = ReadCommBlock(Readbuf, SRECORD_MAX_LENGTH_LINE);

				if (flagError)
					break;

				Readbuf[res] = 0;
				
				if (res != 0)
				{
					if(FirstTime == 0)
					{
						printf ("%s\n\n Waiting for application S-Record.", Readbuf); 
						FirstTime = 1;
					}
					else
					{
						printf ("%s", Readbuf); 
					}
					if (strstr (Readbuf, "Error") != 0)
					{
						printf ("\nError occured. Writting canceled\n");
						flagError = TRUE;
						break;
					}
				}
			}

			ClearCommError( idComDev, &dwErrorFlags, &ComStat ) ; 


		} 

		wSysTimeE = GetTickCount();

		while (!flagError && timeout != 0)
		{
			
			res = ReadCommBlock(Readbuf, SRECORD_MAX_LENGTH_LINE);

			Readbuf[res] = 0;

			if (res != 0 )
			{
				for(j=0; j <75; j++)
				{

					printf ("%c", Readbuf[j]); 
				}
					//break;
			}

			Sleep (20);
			timeout = timeout - 20;


		}


		if (flagError)
		  PurgeComm( idComDev, PURGE_TXABORT | PURGE_RXABORT |
										  PURGE_TXCLEAR | PURGE_RXCLEAR ) ;


		if (logFile)
			printf("\n Loading time %i ms ", wSysTimeE - wSysTimeSt );


		if (logFile)
		{
			fprintf(logOut, "\n Loading time %i ms ", wSysTimeE - wSysTimeSt);
			GetLocalTime(&wSysTime);

			fprintf(logOut, "\n\n%02d:%02d:%02d.%03d",
				wSysTime.wHour, wSysTime.wMinute, wSysTime.wSecond, 
				wSysTime.wMilliseconds);
		}


		fclose (sRecFile);
		if (logFile)
			fclose (logOut);
		CloseConnection();

	}

 	return 1;
}


int parse_opt (char* keystr)
{

	int res;

	res = 1;

	if ( keystr[0] == '-' )
	{
		switch ( tolower(keystr[1]) )
		{
		case 'b':
			{
				CL_KEYVAL_CHECK;
				vBaudrate = atol( &keystr[3] );
				break;
			};
		case 's':
			{
				CL_KEYVAL_CHECK;
				strcpy(sRecordFileName, &keystr[3]);
				break;
			};
		case 'l':
			{
				CL_KEYVAL_CHECK;
				strcpy(logFileName, &keystr[3]);
				break;
			};
		case 'a':
			{
				CL_KEYVAL_CHECK;
				sscanf(&keystr[3], "%x", &idCANRead);
				break;
			};
		case 'r':
			{
				CL_KEYVAL_CHECK;
				sscanf(&keystr[3], "%x", &idCANWrite);
				break;
			};
		case 'i':
			{
				CL_KEYVAL_CHECK;
				strcpy(nameLoad, &keystr[3]);
				break;
			};
		default:
			{
				fprintf( stderr, "Invalid switch: %s\n\n", keystr);
				fprintf( stderr, clUsage);
				exit(1);
			};
		}
	}
	else
	{
		res = 0;
	};
	if ( res == 1 )
	{
		return(1);
	}
	else
	{
		fprintf( stderr, "\n*** Invalid switch: %s\n", keystr);
		fprintf( stderr, clUsage);
		exit(1);
	};
	

	return 0;

}



long GetFileSizeStream( FILE  *Stream )
{
    fpos_t posBackup;
    long ret;

    fgetpos( Stream, &posBackup );
    fseek( Stream, 0, SEEK_END );
    ret = ftell( Stream );
    fsetpos( Stream, &posBackup );

    return ret;
}



//---------------------------------------------------------------------------
//  BOOL WriteCommBlock( BYTE *pByte )
//
//  Description:
//     Writes a block of data to the COM port specified in the associated
//     TTY info structure.
//
//  Parameters:
//
//     BYTE *pByte
//        pointer to data to write to port
//
//  Win-32 Porting Issues:
//     - WriteComm() has been replaced by WriteFile() in Win-32.
//     - Overlapped I/O has been implemented.
//
//---------------------------------------------------------------------------
BOOL WriteCommBlock( LPSTR lpByte , DWORD dwBytesToWrite)
{

   BOOL        fWriteStat = 0;
   DWORD       dwBytesWritten = 0;
   DWORD       dwErrorFlags;
   COMSTAT     ComStat = {0};
   OVERLAPPED  osWrite = {0,0,0};

	fWriteStat = WriteFile( idComDev, lpByte, dwBytesToWrite,
                         &dwBytesWritten, &osWrite) ;


   if (!fWriteStat)
   {
         if (GetLastError() != ERROR_IO_PENDING)
		 {
		   
		   // some other error occurred
			 ClearCommError( idComDev, &dwErrorFlags, &ComStat ) ; 
   			 printf ("Write error occured\n");
   			 flagError = TRUE;
			 return FALSE;
		 }

   }

   return TRUE ;

} 

//---------------------------------------------------------------------------
//  int ReadCommBlock( LPSTR lpszBlock, int nMaxLength )
//
//  Description:
//     Reads a block from the COM port and stuffs it into
//     the provided buffer.
//
//  Parameters:
//
//     LPSTR lpszBlock
//        block used for storage
//
//     int nMaxLength
//        max length of block to read
//
//  Win-32 Porting Issues:
//     - ReadComm() has been replaced by ReadFile() in Win-32.
//     - Overlapped I/O has been implemented.
//
//---------------------------------------------------------------------------

int ReadCommBlock( LPSTR lpszBlock, int nMaxLength )
{
   BOOL       fReadStat ;
   COMSTAT    ComStat = {0};
   DWORD      dwErrorFlags = 0;
   DWORD      dwLength;
   OVERLAPPED  osRead = {0,0,0};

   // only try to read number of bytes in queue
   ClearCommError( idComDev, &dwErrorFlags, &ComStat ) ;

   dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;
   
   if (dwLength > 0)
   {
      fReadStat = ReadFile( idComDev, lpszBlock,
		                    dwLength, &dwLength, &osRead ) ;
      if (!fReadStat)
      {
            dwLength = 0 ;
            ClearCommError( idComDev, &dwErrorFlags, &ComStat ) ;
			if (dwErrorFlags > 0)
			{
				printf ("Read error occured\n");
				flagError = TRUE;
			}
            
		}

   }

   
	if (dwLength < 0)
	{
		dwLength = 0;	
	}

   return ( dwLength ) ;

} 
//---------------------------------------------------------------------------
//  BOOL OpenConnection( H )
//
//  Description:
//     Opens communication port specified in the TTYINFO struct.
//     It also sets the CommState and notifies the window via
//     the fConnected flag in the TTYINFO struct.
//
//  Parameters:
//  Win-32 Porting Issues:
//     - Win-32 has specific communication timeout parameters.
//     - Created the secondary thread for event notification.
//
//---------------------------------------------------------------------------

BOOL OpenConnection(  )
{
 //  char       szPort[ 15 ]; //, szTemp[ 10 ];
   BOOL       fRetVal;

   COMMTIMEOUTS  CommTimeOuts;

 //  strcpy (szPort, "COM1");
   // open COMM device
     idComDev = CreateFile( NameCOMPort, GENERIC_READ | GENERIC_WRITE,
                  0,                    // exclusive access
                  NULL,                 // no security attrs
                  OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL |
                  FILE_FLAG_OVERLAPPED, // overlapped I/O
                  NULL  );
 
	  
	  if (idComDev == NULL)
		return FALSE;

	  // get any early notifications
	  SetCommMask(idComDev, EV_RXCHAR ) ;

	  // setup device buffers
	  SetupComm( idComDev, BUFFER_SIZE, BUFFER_SIZE ) ;

	  // purge any information in the buffer
	  PurgeComm( idComDev, PURGE_TXABORT | PURGE_RXABORT |
									  PURGE_TXCLEAR | PURGE_RXCLEAR ) ;

	  // set up for overlapped I/O
	  CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
	  CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
	  //CommTimeOuts.ReadTotalTimeoutConstant = 1000;
	  CommTimeOuts.ReadTotalTimeoutConstant = 0; //KAN
	  // CBR_9600 is approximately 1byte/ms. For our purposes, allow
	  // double the expected time per character for a fudge factor.
	  CommTimeOuts.WriteTotalTimeoutMultiplier = 2*CBR_9600/BAUDRATE;
	  CommTimeOuts.WriteTotalTimeoutConstant = 0 ;
	  SetCommTimeouts( idComDev, &CommTimeOuts ) ;

      fRetVal = SetupConnection() ;

   
	  return ( fRetVal ) ;

} // end of OpenConnection()

//---------------------------------------------------------------------------
//  BOOL SetupConnection(  )
//
//  Description:
//     This routines sets up the DCB based on settings in the
//     TTY info structure and performs a SetCommState().
//
//  Parameters:
//
//  Win-32 Porting Issues:
//     - Win-32 requires a slightly different processing of the DCB.
//       Changes were made for configuration of the hardware handshaking
//       lines.
//
//---------------------------------------------------------------------------

BOOL SetupConnection()
{
   BOOL       fRetVal ;
   DCB        dcb ;


   dcb.DCBlength = sizeof( DCB ) ;

   GetCommState( idComDev, &dcb ) ;

   dcb.BaudRate = vBaudrate ;
   dcb.ByteSize = BYTESIZE ;
   dcb.Parity = PARITY ;
   dcb.StopBits = STOPBITS ;

   // setup hardware flow control

 
   dcb.fOutxDsrFlow =  FALSE;
   dcb.fDtrControl = DTR_CONTROL_DISABLE;
 

	dcb.fOutxCtsFlow = FALSE ;
	dcb.fRtsControl = RTS_CONTROL_DISABLE;

   // setup software flow control


   dcb.fInX = dcb.fOutX = TRUE ;
   dcb.XonChar = ASCII_XON ;
   dcb.XoffChar = ASCII_XOFF ;
   dcb.XonLim = 0x800;  // 1/2 of buffer size = 0x800, only need for Xon/Xoff protocol
   dcb.XoffLim = 0x200; // 1/8 of buffer size = 0x200, only need for Xon/Xoff protocol

   // other various settings

   dcb.fBinary = TRUE ;
   dcb.fParity = TRUE ;

   fRetVal = SetCommState( idComDev, &dcb ) ;

   return ( fRetVal ) ;

} // end of SetupConnection()
//---------------------------------------------------------------------------
//  BOOL CloseConnection(  )
//
//  Description:
//     Closes the connection to the port.  Resets the connect flag
//     in the TTYINFO struct.
//
//  Parameters:
//
//  Win-32 Porting Issues:
//     - Needed to stop secondary thread.  SetCommMask() will signal the
//       WaitCommEvent() event and the thread will halt when the
//       CONNECTED() flag is clear.
//     - Use new PurgeComm() API to clear communications driver before
//       closing device.
//
//---------------------------------------------------------------------------

BOOL CloseConnection()
{


    // purge any outstanding reads/writes and close device handle
    PurgeComm( idComDev, PURGE_TXABORT | PURGE_RXABORT |
                                   PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
    CloseHandle( idComDev);

    // change the selectable items in the menu
    return ( TRUE ) ;
} // end of CloseConnection()


⌨️ 快捷键说明

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