📄 srecord.c
字号:
long fileSize;
unsigned long sRecCountLast = 0;
char Writebuf [SRECORD_MAX_LENGTH_LINE];
char Readbuf [SRECORD_MAX_LENGTH_LINE];
DWORD dwErrorFlags;
COMSTAT ComStat = {0};
lastLine = FALSE;
sRecCount = 0;
// title(); /* Print title */
if( argc <= 1 )
{
help();
return(0);
}
for (i = 1; i < argc; i++)
{
strcpy( buf, argv[i] );
res = parse_opt( buf ); /* Parse command line options */
};
if( *nameLoad == '\0' || (strstr (nameLoad, SCI_INTERFACE_NAME) == 0 && strstr (nameLoad, CAN_INTERFACE_NAME) == 0 ))
{
fprintf( stderr, "\n*** The name of Interface shall be defined - %s[0..31] or %s[0..3] \n", CAN_INTERFACE_NAME, SCI_INTERFACE_NAME);
fprintf( stderr, clUsage);
help();
return(0);
}
if (*sRecordFileName != '\0') /* srecord file is required */
{
/* config file open for reading */
sRecFile = fopen( sRecordFileName, "rt" );
if ( sRecFile == NULL )
{
fprintf( stderr, "*** Can not open sRecord file %s\n*** Error %#0x\n",
sRecordFileName, GetLastError());
exit(3);
};
}
else
{
fprintf( stderr, "\n*** sRecord file required\n\n" );
fprintf( stderr, clUsage);
exit(3);
};
if (*logFileName != '\0')
{
/* log file open - rewrite */
logOut = fopen( logFileName, "wt" );
if ( logOut == NULL )
{
fprintf( stderr, "*** Can not open log file %s\n",
"*** Error \n",
logFileName);
fprintf( stderr, clUsage);
exit(4);
};
logFile = TRUE;
}
else
logFile = FALSE;
/* Read line from S-Record file */
if (logFile)
fprintf(logOut, "S-Record file %s\n\n", sRecordFileName);
fileSize = GetFileSizeStream(sRecFile);
szBuffer = malloc(fileSize);
memset(szBuffer, 'X', fileSize);
i = fread(szBuffer, sizeof(char), fileSize, sRecFile);
szBuffer[i] = 0;
GetLocalTime(&wSysTime);
if (logFile)
fprintf(logOut, "%02d:%02d:%02d.%03d\n\n",
wSysTime.wHour, wSysTime.wMinute, wSysTime.wSecond,
wSysTime.wMilliseconds);
if (strstr (nameLoad, CAN_INTERFACE_NAME) != 0)
{
SetAttribute ();
// if ( timeout == 0 ) timeout = NC_DURATION_10SEC;
timeout = NC_DURATION_10SEC;
if ( vBaudrate == 0 ) clAttrValue[0] = CL_BAUD_RATE;
else clAttrValue[0] = vBaudrate;
if (idCANRead == 0)
{
fprintf( stderr, "\n*** CAN ID for reading shall be defined\n");
fprintf( stderr, clUsage);
help();
return(0);
}
if (idCANWrite == 0)
{
fprintf( stderr, "\n*** CAN ID for writting shall be defined\n");
fprintf( stderr, clUsage);
help();
return(0);
}
ncReset((NCTYPE_STRING)nameLoad, 0);
// ncReset((NCTYPE_STRING)CL_PORT2_NAME, 0);
/* Configure the CAN Network Interface Objects and open its */
//Read
// clAttrValue[CAN_COMP_XTD_PLACE] = idCANRead;
res = clCANOpen(nameLoad, &clPortObjh_CAN0);
CL_NONZERO_ERR(res);
//Write
// clAttrValue[CAN_COMP_STD_PLACE] = NC_CAN_ARBID_NONE;
// clAttrValue[CAN_COMP_XTD_PLACE] = NC_CAN_ARBID_NONE;
// res = clCANOpen(CL_PORT2_NAME, &clPortObjh_CAN1);
// CL_NONZERO_ERR(res);
res = SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
wSysTimeSt = GetTickCount();
wSysTimeCur = wSysTimeSt;
dwErrorFlags = 0;
while ( *szBuffer )
{
res = writeData (szBuffer);
CL_NONZERO_ERR(res);
if (newL == 1 && cntLine != 1 )
{
Status = ncWaitForState(clPortObjh_CAN0, (NC_ST_WRITE_SUCCESS | NC_ST_WARNING
| NC_ST_ERROR), 100, &State);
wSysTimeStart = GetTickCount();
do
{
//Read from CAN if whole line was written
Status = ncWaitForState(clPortObjh_CAN0,
(NC_ST_READ_AVAIL | NC_ST_STOPPED | NC_ST_ERROR | NC_ST_WARNING),
timeout, &State);
res = clPrintStat(Status, "ncWaitForState", stderr);
CL_NONZERO_ERR(res);
Status= ncRead(clPortObjh_CAN0, sizeof(NCTYPE_CAN_FRAME_TIMED), &Receive);
ReadCount += 1;
res = clPrintStat(Status, "ncRead", stderr);
CL_NONZERO_ERR(res);
wSysTimeLast = GetTickCount();
if (logFile)
{
// fprintf(logOut, "Received Data: ");
fprintf(logOut, " ID %08x ", Receive.ArbitrationId);
for (i = 0; i < Receive.DataLength ; i++)
{
fprintf(logOut, "%02x ", Receive.Data[i]);
}
fprintf(logOut, " %i ms ", wSysTimeLast - wSysTimeCur);
fprintf(logOut, "\n");
}
wSysTimeCur = wSysTimeLast;
if (Receive.ArbitrationId == idCANRead)
{
break;
}
wSysTimeEnd = GetTickCount();
} while ((wSysTimeEnd - wSysTimeStart) <= timeout);
if (Receive.ArbitrationId == idCANRead)
{
// Compare cntRecord with S-Record Counter
BootlReceive = (NCTYPE_CAN_BOOTLOADER_FRAME*) Receive.Data;
pTemp = &Receive.Data[0];
//It is the first line. The version of the bootloader is located in the S-Rec counter
if (newL == 1 && cntLine == 2)
{
for(i=0; i<Receive.DataLength; i++)
{
printf("%c", pTemp[i]);
}
for(j=0; j < 8; j++)
{
Status = ncWaitForState(clPortObjh_CAN0,
(NC_ST_READ_AVAIL | NC_ST_STOPPED | NC_ST_ERROR | NC_ST_WARNING),
timeout, &State);
res = clPrintStat(Status, "ncWaitForState", stderr);
CL_NONZERO_ERR(res);
Status= ncRead(clPortObjh_CAN0, sizeof(NCTYPE_CAN_FRAME_TIMED), &Receive);
pTemp = &Receive.Data[0];
for(i=0; i<Receive.DataLength; i++)
{
printf("%c", pTemp[i]);
}
}
printf("\n");
for(j=0; j < 5; j++)
{
Status = ncWaitForState(clPortObjh_CAN0,
(NC_ST_READ_AVAIL | NC_ST_STOPPED | NC_ST_ERROR | NC_ST_WARNING),
timeout, &State);
res = clPrintStat(Status, "ncWaitForState", stderr);
CL_NONZERO_ERR(res);
Status= ncRead(clPortObjh_CAN0, sizeof(NCTYPE_CAN_FRAME_TIMED), &Receive);
pTemp = &Receive.Data[0];
for(i=0; i<Receive.DataLength; i++)
{
printf("%c", pTemp[i]);
}
}
//printf( "%s", "\r\n\r\n (c) 2003 MOTOROLA. S-Record loader for the MC56F83xx. ver. ");
// for (i = 0; i < 5; i++)
// {
// printf("%c", pTemp[i]);
// }
// printf( "%s", "\n ");
}
sRecCountLast = sRecCount;
// sRecCount = BootlReceive->SRecordCnt;
sRecCount = Receive.DataLength;
// BootlReceive->ErrorCode = 0;
// if (BootlReceive->ErrorCode > 0 && BootlReceive->ErrorCode < 100 )
if((pTemp[3] == 'E') && (pTemp[4] == 'r'))
{
Status= ncRead(clPortObjh_CAN0, sizeof(NCTYPE_CAN_FRAME_TIMED), &Receive);
if(pTemp[4] >= 0x41)
{
TempError = 10 + ((int)pTemp[4] - 0x41);
}
else
{
TempError = (int)pTemp[4] - 0x30;
}
if(TempError <= NUM_ERROR)
{
printf("\nError");
printf(" #%d-%s occured!!\n", TempError, Error_Code[TempError]);
}
/*
BootlReceive->ErrorCode = 0;
if (logFile)
fprintf (logOut, "Error occured (error code %04x)\n", BootlReceive->ErrorCode);
if (BootlReceive->ErrorCode <= NUM_ERROR)
{
printf ("%s\n", Error_Code[BootlReceive->ErrorCode - 1]);
if (logFile)
fprintf (logOut, "%s\n", Error_Code[BootlReceive->ErrorCode - 1]);
}
*/
dwErrorFlags = 1;
break;
}
}
else
{
fprintf( stderr, "*** No data read %s\n",
"*** Error \n",
logFileName);
fprintf( stderr, clUsage);
if (logFile)
fprintf (logOut, "No data read, timeout %i \n", wSysTimeEnd - wSysTimeStart);
exit(4);
}
}
if (newL == 1 && cntLine != 1)
printf(".");
if (newL == 1 && cntLine == 2)
printf(".");
}
wSysTimeE = GetTickCount();
if (logFile)
{
fprintf(logOut, "\n Loading time %i ms ", wSysTimeE - wSysTimeSt);
fprintf(logOut, "\n Total in the file %i bytes ", cntData);
fprintf(logOut, "\n Bootloader loaded %i bytes ", sRecCountLast);
fprintf(logOut, "\n Data in the file %i bytes ", (cntData - (cntLine - SREC_NUMBER_OF_NOT_WRITTEN_LINE) * SREC_NUMBER_OF_NOT_DATA_IN_LINE)/2);
fprintf(logOut, "\n Number of lines in the S-Record file %i ", cntLine);
}
if (dwErrorFlags != 1)
{
// printf("\n Loaded 0x%08X bytes.", sRecCountLast);
Sleep (1);
printf("\n");
pTemp = &Receive.Data[0];
for(i=0; i<Receive.DataLength; i++)
{
printf("%c", pTemp[i]);
}
for(j=0; j < 10; j++)
{
Status = ncWaitForState(clPortObjh_CAN0,
(NC_ST_READ_AVAIL | NC_ST_STOPPED | NC_ST_ERROR | NC_ST_WARNING),
timeout, &State);
res = clPrintStat(Status, "ncWaitForState", stderr);
CL_NONZERO_ERR(res);
Status= ncRead(clPortObjh_CAN0, sizeof(NCTYPE_CAN_FRAME_TIMED), &Receive);
pTemp = &Receive.Data[0];
if(j == 2)
{
printf(" bytes");
}
for(i=0; i<Receive.DataLength; i++)
{
printf("%c", pTemp[i]);
}
}
// printf ("\n Application started from address 0x");
// printf ("%c", pTemp[2]);
// printf ("%c", pTemp[3]);
// printf ("%c", pTemp[0]);
// printf ("%c", pTemp[1]);
// printf("\n Loaded 0x");
// printf("%c%c%c%c", pTemp[4], pTemp[5], pTemp[6],pTemp[7]);
// Status = ncWaitForState(clPortObjh_CAN0,
// (NC_ST_READ_AVAIL | NC_ST_STOPPED | NC_ST_ERROR | NC_ST_WARNING),
// timeout, &State);
// Status= ncRead(clPortObjh_CAN0, sizeof(NCTYPE_CAN_FRAME_TIMED), &Receive);
// printf("%c%c bytes", pTemp[0],pTemp[1]);
Sleep (1);
pTemp = (char *)Receive.Data;
Sleep (1);
}
if (logFile)
printf("\n Loading time %i ms \n", wSysTimeE - wSysTimeSt);
GetLocalTime(&wSysTime);
if (logFile)
fprintf(logOut, "\n\n%02d:%02d:%02d.%03d",
wSysTime.wHour, wSysTime.wMinute, wSysTime.wSecond,
wSysTime.wMilliseconds);
fclose (sRecFile);
if (logFile)
fclose (logOut);
/* close CAN Objects */
if ( clPortObjh_CAN0 )
{
Status = ncCloseObject(clPortObjh_CAN0);
strcpy (buf, "ncCloseObject");
strcat( buf, nameLoad );
res = clPrintStat(Status, buf, stderr);
};
/* set normal priority for this process (and its threads) */
res = SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
}
if (strstr (nameLoad, SCI_INTERFACE_NAME) != 0)
{
// if ( timeout == 0 ) timeout = READ_BUFFER_TIMEOUT;
timeout = READ_BUFFER_TIMEOUT;
if ( vBaudrate == 0 ) vBaudrate = BAUDRATE;
strcpy( NameCOMPort, nameLoad);
if (!OpenConnection())
{
fprintf( stderr, "*** Can not open COM port %s\n",
"*** Error \n",
logFileName);
fprintf( stderr, clUsage);
exit(4);
}
wSysTimeSt = GetTickCount();
/* if (*menuItem != '\0') //Bootloader menu is available
res = WriteCommBlock( menuItem, strlen (menuItem) );
*/
while ( *szBuffer || ComStat.cbOutQue != 0)
{
memset(Writebuf, 0, SRECORD_MAX_LENGTH_LINE);
if (*szBuffer )
{
//fgets( Writebuf, SRECORD_MAX_LENGTH_LINE, sRecFile);
//read one line from the buffer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -