📄 commtestdlg.cpp
字号:
}
unsigned short int CCommtestDlg::vVerify(unsigned char * buf,unsigned char * hbuff, unsigned short int set,unsigned short int leng)
{
unsigned char m_filesbuf;
unsigned char m_verifybuf;
unsigned char * k;
unsigned short int i;
unsigned short int ErrSum=0;
k = buf;
k += set;
for(i=0;i<leng;i++)
{
m_filesbuf = *(k++);
m_verifybuf= hbuff[i];
if(m_filesbuf != m_verifybuf)ErrSum++;
}
return ErrSum;
}
unsigned char CCommtestDlg::vHexDdecoder(unsigned char hex_data)
{
unsigned char BackCom;
static m_HexRdState = REC_MARK;
BackCom = HEX_DEC_OK;
switch(m_HexRdState)
{
case REC_MARK:
if (hex_data == ':')
{
m_HexRdState = REC_LEN_1;
nb_byte=0;
}
else
m_HexRdState = REC_MARK;
break;
case REC_LEN_1:
ulength = toint(hex_data) * 16;
m_HexRdState = REC_LEN_2;
break;
case REC_LEN_2:
ulength = ulength + toint(hex_data);
m_HexRdState = OFFSET_1;
break;
case OFFSET_1:
offset = toint(hex_data) * 4096;
m_HexRdState = OFFSET_2;
break;
case OFFSET_2:
offset = offset + toint(hex_data) * 256;
sum_1= offset / 256;
m_HexRdState = OFFSET_3;
break;
case OFFSET_3:
offset = offset + toint(hex_data) * 16;
m_HexRdState = OFFSET_4;
break;
case OFFSET_4:
offset = offset + toint(hex_data);
sum_2 = offset - sum_1 * 256;
m_HexRdState = REC_TYP_1;
break;
case REC_TYP_1:
type = toint(hex_data) * 16;
m_HexRdState =REC_TYP_2;
break;
case REC_TYP_2:
type = type + toint(hex_data);
sum = ulength + sum_1 + sum_2 + type;
if(ulength==0x00)
m_HexRdState=CHEKSUM_1;
else
m_HexRdState = DATA_1;
break;
case DATA_1:
data_value = toint(hex_data) * 16 ;
m_HexRdState = DATA_2;
break;
case DATA_2:
data_value = data_value + toint(hex_data);
sum = sum + data_value;
hexbuff[nb_byte] = data_value;
nb_byte++;
if(ulength<0xFF)
{
if(nb_byte == ulength)m_HexRdState = CHEKSUM_1;
else m_HexRdState = DATA_1;
}
else
{
if(nb_byte == (ulength+1))m_HexRdState = CHEKSUM_1;
else m_HexRdState = DATA_1;
}
break;
case CHEKSUM_1:
sum_1 = toint(hex_data) * 16;
sum = (~sum) + 1;
m_HexRdState = CHEKSUM_2;
break;
case CHEKSUM_2:
sum_1 = sum_1 + toint(hex_data);
m_HexRdState = REC_MARK;
if(sum_1 != sum)
{
if((ulength == 0x80)||(ulength == 0xFF))
{
if(type==0x00)BackCom = HEX_DEC_FRAME_VERIFY_ERR; //比较帧数据错误
else if(type==0x14)BackCom = HEX_DEC_SEG_VERIFY_ERR; //比较段地址错误
}
else BackCom = HEX_DEC_ERR; //错误
}
else
{
if(ulength == 0xFF)
{
ulength += 1;
if(type==0x00)BackCom = HEX_DEC_FRAME_VERIFY_DATA; //比较帧数据包
else BackCom = HEX_DEC_OK;
}
else if(ulength == 0x80)
{
if(type==0x00)BackCom = HEX_DEC_FRAME_VERIFY_DATA; //比较帧数据包
else BackCom = HEX_DEC_OK;
}
else if(type==0x00)BackCom = HEX_DEC_FRAME_DATA; //HEX数据包
else if(type==0x01)BackCom = HEX_DEC_END; //HEX解包结束
else if(type==0x05)BackCom = HEX_DEC_REDY; //就绪
else if(type==0x06)BackCom = HEX_DEC_FRAME_OK; //帧正确
else if(type==0x07)BackCom = HEX_DEC_FRAME_ERR; //帧错误
else if(type==0x08)BackCom = HEX_DEC_SEG_OK; //段正确
else if(type==0x09)BackCom = HEX_DEC_SEG_ERR; //段错误
else if(type==0x0B)BackCom = HEX_DEC_SEG_NEXT; //下一段
else if(type==0x0C)BackCom = HEX_DEC_SEG_END; //段结束
else if(type==0x0E)BackCom = HEX_DEC_FRAME_NEXT; //下一帧
else if(type==0x0F)BackCom = HEX_DEC_FRAME_END; //帧结束
else if(type==0x11)BackCom = HEX_DEC_VERIFY_REDY; //比较就绪
else if(type==0x13)BackCom = HEX_DEC_SEG_VERIFY_OK; //比较段地址正确
else if(type==0x17)BackCom = HEX_DEC_VERIFY_FRAME_NEXT; //比较帧继续
else if(type==0x18)BackCom = HEX_DEC_VERIFY_FRAME_END; //比较帧结束
else if(type==0x1A)BackCom = HEX_DEC_PROGRAM_ERR; //烧写错误
else if(type==0x1D)
{
BackCom = HEX_DEC_SECTOR_BANK; //FLASH扇区
offset = hexbuff[0]*256+hexbuff[1];
}
else if(type==0x1E)
{
BackCom = HEX_DEC_START; //烧写开始
offset = hexbuff[0]*256+hexbuff[1];
}
}
break;
}
return BackCom;
}
void CCommtestDlg::OnFileOpen()
{
static char szFilter[] = "Files (*.*)|*.*|Files (*.BIN)|*.BIN";
int datbmp[1760][1168];
static char Files[] = "c:\\da.raw";
char * SectorID = ":0000001CE4"; //扇区设置命令
HANDLE hFile;
CFile myFile;
HANDLE hile;
CFile mFile;
unsigned char mRed,mGreen,mBlue;
unsigned short int ly;
unsigned long i;
unsigned long j;
unsigned char k;
unsigned char l;
float y;
float b0 = 0.299;
float b1 = 0.114;
float b2 = 0.587;
CFileDialog MyFileDlg(TRUE,NULL,"*.*",OFN_HIDEREADONLY,szFilter,NULL);
MyFileDlg.DoModal();
m_Pathname = MyFileDlg.GetPathName( );
hFile = ::CreateFile(m_Pathname, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, 0, NULL);
hile = ::CreateFile(Files, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
CFile myFile((int)hFile);
CFile mFile((int)hile);
myFile.Seek(0,CFile::begin);
for(j=0;j<1168;j++)
{
for(i=0;i<1760;i++)
{
myFile.Read(&mRed,1);
datbmp[i][j] = mRed;
}
}
CloseHandle(hFile);
mFile.Seek(0,CFile::begin);
//myFile.Read(&mBlue,1);
//myFile.Read(&mGreen,1);
//y = b0*(float)mRed+b1*(float)mBlue+b2*(float)mGreen;
//ly = (unsigned short int)y;
//mFile.Write(&ly, 1);
for(i=0;i<1168-3;i++)
{
for(j=0;j<1760-3;j++)
{
ly = 0;
for(k=0;j<3;j++)
{
for(l=0;l<3;l++)
{
ly += datbmp[j+k][i+l];
}
}
ly = ly/9;
mFile.Write(&ly, 1);
}
}
}
CloseHandle(hile);
//OnOpenFile("H00");
//OnSaveHex();
//m_mediaTimer.Start(); //启动定时器
m_ProcessType = 0xFF; //下载处理
m_Ports.WriteToPort(SectorID); //扇区设置命令
}
void CCommtestDlg::OnOpenFile(CString mStr)
{
unsigned char FileTxtRplace;
unsigned char ReadHex;
CString mFileExt;
HANDLE hFile;
CFile myFile;
if(mStr == "H00")
{
FileTxtRplace = m_Pathname.Replace("H00", "H00");
ASSERT(FileTxtRplace == 1);
}
if(mStr == "H01")
{
FileTxtRplace = m_Pathname.Replace("H00", "H01");
ASSERT(FileTxtRplace == 1);
}
if(mStr == "H02")
{
FileTxtRplace = m_Pathname.Replace("H01", "H02");
ASSERT(FileTxtRplace == 1);
}
if(mStr == "H03")
{
FileTxtRplace = m_Pathname.Replace("H02", "H03");
ASSERT(FileTxtRplace == 1);
}
memset(Filesbuf, 0, sizeof(Filesbuf));
hFile = ::CreateFile(m_Pathname, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
CFile myFile((int)hFile);
myFile.Seek(0,CFile::begin);
//文件解包
//源: 打开文件
//目标: Filesbuf存储Bin码
status = HEX_DEC_OK;
while(status == HEX_DEC_OK)
{
myFile.Read(&ReadHex,1);
status = vHexDdecoder(ReadHex);
switch(status)
{
case HEX_DEC_FRAME_DATA :
vWriteBuff(Filesbuf,hexbuff,offset,ulength);
status = HEX_DEC_OK;
break;
case HEX_DEC_END :
status = HEX_DEC_END;
break;
default :
status = HEX_DEC_OK;
break;
}
}
}
CloseHandle(hFile);
}
void CCommtestDlg::OnSaveBin()
{
unsigned char FileTxtRplace;
int mFLength;
HANDLE hFile;
CFile myFile;
mFLength = m_Pathname.GetLength();
mFLength = mFLength - 3;
if((m_Pathname.GetAt(mFLength)) == 'H')
{
mFLength = mFLength - 1;
FileTxtRplace = m_Pathname.Delete(mFLength, 2);
m_Pathname += ".BIN";
}
hFile = ::CreateFile(m_Pathname, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
CFile myFile((int)hFile);
myFile.Seek(0,CFile::begin);
myFile.Write(Filesbuf, 64*1024);
}
CloseHandle(hFile);
mFLength = m_Pathname.GetLength();
mFLength -= 6;
if(m_Pathname.Find("00",mFLength) != -1)
{
FileTxtRplace = m_Pathname.Replace("00.BIN", ".H00");
ASSERT(FileTxtRplace == 1);
}
if(m_Pathname.Find("01",mFLength) != -1)
{
FileTxtRplace = m_Pathname.Replace("01.BIN", ".H01");
ASSERT(FileTxtRplace == 1);
}
if(m_Pathname.Find("02",mFLength) != -1)
{
FileTxtRplace = m_Pathname.Replace("02.BIN", ".H02");
ASSERT(FileTxtRplace == 1);
}
if(m_Pathname.Find("03",mFLength) != -1)
{
FileTxtRplace = m_Pathname.Replace("03.BIN", ".H03");
ASSERT(FileTxtRplace == 1);
}
}
void CCommtestDlg::OnSaveHex()
{
unsigned char FileTxtRplace;
unsigned short int FileBank;
int mFLength;
HANDLE hFile;
CFile myFile;
mFLength = m_Pathname.GetLength();
mFLength = mFLength - 3;
if((m_Pathname.GetAt(mFLength)) == 'H')
{
mFLength = mFLength - 1;
FileTxtRplace = m_Pathname.Delete(mFLength, 2);
m_Pathname += ".HEX";
}
hFile = ::CreateFile(m_Pathname, GENERIC_WRITE, FILE_SHARE_WRITE,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -