📄 usbulkctl.cpp
字号:
if(crc!=CRC)
{
strResult.Format("Err: CRC is %2X",CRC);
free((void*)rxBuf);
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
if (*((WORD *)rxBuf+0)==0x5245)
{
strResult.Format("%2X",*((char *)rxBuf+4));
strResult = "Err:" + strResult;
//strResult = "1";
free((void*)rxBuf);
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
else if (*((WORD *)rxBuf+0)==0x4B4F)
{
nSumLength = *((DWORD *)rxBuf+1);
}
else
{
strResult.Format("Err: Head %2X %2X",*((char *)rxBuf+0),*((char *)rxBuf+1));
free((void*)rxBuf);
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
free((void *)rxBuf);
// close devices if needed
//if(hRead != INVALID_HANDLE_VALUE)
CloseHandle(hRead);
if (nSumLength<=0)
{
strResult = "Err: File size is 0."; //源文件不存在
//strResult = "1";
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
}
strResult.Format("File size : %d",nSumLength);
RecordLog("ReadUsbFile",fileName,strResult);
filePath = dPath;
if (filePath.IsEmpty())
filePath = "C:\\";
BOOL t = CreateDir(filePath); //创建目标路径
if(t == FALSE)
{
strResult = "Err: Create Directory Failed!"; //创建目标路径失败
//strResult = "1";
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
//发送读文件命令
filePath = fileName;
//filePath = filePath + "/";
NameLen = filePath.GetLength();
//memcpy(szFileName+0,filePath,NameLen);
//txBufSize = NameLen+10;
txBufSize = NameLen+8;
txBuf=(char *)malloc(txBufSize);
if(txBuf==NULL)
{
strResult = TEXT("Err: Memory Allocation Fail:(%d)\n"),txBufSize; //内存不够
//strResult = "1";
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
*((DWORD *)txBuf+0) = 0x0004101B;
*((DWORD *)txBuf+1) = txBufSize-2;
memcpy((char *)txBuf+8, filePath,NameLen);
//crc = GetCRC16((unsigned char *)txBuf,txBufSize-2);
//*((WORD *)(txBuf+8+NameLen))=crc; //attach checksum
res = SendToUSB64((void *)0); //发送
if (res==(int)txBufSize)
{
iTxBuf=0;
//rxLength = 4096*4;
//txBufSize = nSumLength;
if ((nSumLength%TX_SIZE) !=0)
{
txBufSize = 2*((nSumLength / TX_SIZE)+1);
}else
{
txBufSize = 2*((nSumLength / TX_SIZE)+0);
}
txBufSize = txBufSize + nSumLength;
rxBuf = (char *)malloc(txBufSize);
if(rxBuf==NULL)
{
strResult = TEXT("Err: Memory Allocation Fail:(%d)\n"),txBufSize; //内存不够
//strResult = "1";
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
//初始化进度框
//
_hDlgDownloadProgress=NULL;
HINSTANCE hInstance=AfxGetInstanceHandle();
CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),m_hWnd,DownloadProgressProc); //modaless
InitDownloadProgress();
DWORD txBufSize100=txBufSize/100;
if(txBufSize100==0) txBufSize100=1;
//
hRead = open_file(inPipe);
//char *tmBuf;
//tmBuf=(char *)malloc(TX_SIZE);
//ReadFile(hRead,tmBuf,TX_SIZE,&respLen,NULL);
while(1)
{
CheckMessageQueue();
if((txBufSize-iTxBuf) > TX_SIZE)
txBlkSize=TX_SIZE;
else
txBlkSize=txBufSize-iTxBuf;
txBlk=(void *)(rxBuf+iTxBuf);
success = ReadFile(hRead,txBlk,txBlkSize,&respLen,NULL);
if (*((WORD *)rxBuf+(iTxBuf/2))!=0x4B4F)
{
strResult.Format("Head : %d",*((WORD *)rxBuf+(iTxBuf/2)));
RecordLog("ReadUsbFile",fileName,strResult);
free((void *)rxBuf);
return strResult.AllocSysString();
}
//memcpy((char *)rxBuf+iTxBuf,tmBuf,txBlkSize);
iTxBuf+=txBlkSize;
//
if( ((iTxBuf/txBufSize100)%2)==1 )
DisplayDownloadProgress(iTxBuf/txBufSize100);
if(downloadCanceled==1)
{
strResult = "Err: Read is canceled by user";
//strResult = "1";
break; //download is canceled by user.
}
//
if(iTxBuf>=txBufSize) break;
/*
{
DisplayDownloadProgress(iTxBuf/txBufSize100);
char *rxEndBuf;
rxEndBuf = (char *)malloc(rxLength);
if(rxEndBuf==NULL)
{
strResult = TEXT("Err: Memory Allocation Fail:(%d)\n"),rxLength; //内存不够
//strResult = "1";
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
//hRead = open_file(inPipe);
UINT success = ReadFile(hRead,rxEndBuf,rxLength,&respLen,NULL);
if (*((WORD *)rxEndBuf+0)!=0x4B4F)
{
strResult.Format("%1x",*((char *)txBuf+4));
strResult = "Err:" + strResult;
RecordLog("ReadUsbFile",fileName,strResult);
}
free((void*)rxEndBuf);
break;
}
*/
}
CloseDownloadProgress();
// close devices if needed
//if(hRead != INVALID_HANDLE_VALUE)
CloseHandle(hRead);
if(strResult == "Err: Read is canceled by user") //exit by user.
{
free((void *)rxBuf);
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
if (*((WORD *)rxBuf+0)==0x5245)
{
strResult.Format("%1x",*((char *)txBlk+4));
strResult = "Err:" + strResult;
//strResult = "1";
free((void *)rxBuf);
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
tFileName = dPath;
tFileName = tFileName + "\\" + fileName;
tFileName.Replace("//","\\");
if(nTargetFile.Open(_T(tFileName), CFile::modeCreate | CFile::modeWrite)==0)
{
strResult.Format("Err: Create file %s Fail.",tFileName);
//strResult = "1";
free((void *)rxBuf);
RecordLog("ReadUsbFile",fileName,strResult);
return strResult.AllocSysString();
}
iTxBuf=0;
txBlkSize=0;
while(1)
{
if((txBufSize-iTxBuf) > TX_SIZE)
txBlkSize=TX_SIZE;
else
txBlkSize=txBufSize-iTxBuf;
nTargetFile.Write(((void *)(rxBuf+iTxBuf+2)),txBlkSize-2);
iTxBuf+=txBlkSize;
if(iTxBuf>=txBufSize) break;
}
//nTargetFile.Write(rxBuf,txBufSize);
nTargetFile.Close();
free((void *)rxBuf);
}
strResult = "0";
RecordLog("ReadUsbFile",fileName,strResult);
/*
//延时100毫秒,等待隔离器关闭文件
DWORD dwStart = GetTickCount();
DWORD dwEnd = dwStart;
do
{
CheckMessageQueue();
dwEnd = GetTickCount()-dwStart;
}while(dwEnd <100);
*/
return strResult.AllocSysString();
}
long CUsbulkCtrl::GetFileLength(LPCTSTR uDir, LPCTSTR uFileName)
{
CString filePath;
//int crc;
DWORD res;
int NameLen;
filePath = uDir;
if (!filePath.IsEmpty())
{
filePath = filePath + "/" + uFileName;
}
else
filePath = uFileName;
NameLen = filePath.GetLength();
//txBufSize = NameLen+10;
txBufSize = NameLen+8;
txBuf=(char *)malloc(txBufSize);
if(txBuf==NULL)
{
return -1;
}
/*
txBufSize = NameLen+10;
int ii = 64 - (txBufSize % 64);
txBufSize = txBufSize +ii;
txBuf=(char *)malloc(txBufSize);
*/
*((DWORD *)txBuf+0) = 0x0008101B;
*((DWORD *)txBuf+1) = txBufSize-2;
//*((DWORD *)txBuf+1) = NameLen+8;
memcpy((char *)txBuf+8, filePath,NameLen);
//crc = GetCRC16((unsigned char *)txBuf,txBufSize-2);
//*((WORD *)(txBuf+8+NameLen))=crc; //attach checksum
//for (int i=0;i<ii;i++)
// *((char *)txBuf + NameLen + 10 + i) = 0x00;
res = SendToUSB64((void *)0); //发送文件名及文件路径
if (res==(int)txBufSize)
{
UINT success;
UINT rxLength=64;
ULONG respLen;
char *rxBuf;
rxBuf = (char *)malloc(rxLength);
if(rxBuf==NULL)
{
return -1;
}
hRead = open_file(inPipe);
success = ReadFile(hRead,rxBuf,rxLength,&respLen,NULL);
if (*((WORD *)rxBuf+0)==0x4B4F)
{
res = *((DWORD *)rxBuf+1);
}
else
{
if (*((WORD *)rxBuf+0)==0x5245)
{
res = -2;
}
else
res = -3;
}
free((void *)rxBuf);
// close devices if needed
//if(hRead != INVALID_HANDLE_VALUE)
CloseHandle(hRead);
}
return res;
}
BOOL CUsbulkCtrl::CreateDir(LPCTSTR sDir)
{
// TODO: Add your dispatch handler code here
CString pathname = sDir;
pathname.Replace("//","\\");
if(pathname.Right(1) != "\\")
pathname += "\\" ;
int end = pathname.ReverseFind('\\');
int pt = pathname.Find('\\');
if (pathname[pt-1] == ':')
pt = pathname.Find('\\', pt+1);
CString path;
while(pt != -1 && pt<=end)
{
path = pathname.Left(pt+1);
::CreateDirectory(path,NULL);
pt = pathname.Find('\\', pt+1);
}
return true;
}
BSTR CUsbulkCtrl::SearchFiles(LPCTSTR extName)
{
CString strResult;
CString strTmp;
CString strXML;
//int crc;
//*****DWORD dwNum;
//*****wchar_t *pwText;
DWORD res;
int NameLen;
strTmp = extName;
//*****strXML = "<?xml version=\"1.0\" encoding=\"Unicode\" ?>\r\n<SEARCHFILES>\r\n";
strXML = "<?xml version=\"1.0\" encoding=\"GB2312\" ?>\r\n<SEARCHFILES>\r\n";
strXML = strXML + "\t<folder name=\"\\\">\r\n";
NameLen = strTmp.GetLength();
//txBufSize = NameLen+10;
txBufSize = NameLen+8;
txBuf=(char *)malloc(txBufSize);
if(txBuf==NULL)
{
strResult = TEXT("Err: Memory Allocation Fail:(%d)\n"),txBufSize; //内存不够
//strResult = "1";
RecordLog("SearchFiles",extName,strResult);
return strResult.AllocSysString();
}
*((DWORD *)txBuf+0) = 0x0009101B;
*((DWORD *)txBuf+1) = txBufSize-2;
memcpy((char *)txBuf+8, strTmp,NameLen);
//crc = GetCRC16((unsigned char *)txBuf,txBufSize-2);
//*((WORD *)(txBuf+8+NameLen))=crc; //attach checksum
res = SendToUSB64((void *)0); //发送文件名及文件路径
if (res==(int)txBufSize)
{
UINT success;
UINT rxLength=64;
ULONG respLen;
ULONG iLen;
int iClass; //当前文件目录名长度
int lClass; //上个文件目录名长度
CString iFolder; //当前文件夹名称
CString lFolder; //上个文件夹名称
char *rxBuf;
void *txBlk;
rxBuf = (char *)malloc(1024*1024);
if(rxBuf==NULL)
{
strResult = TEXT("Err: Memory Allocation Fail:(%d)\n"),0x100000; //内存不够
//strResult = "1";
RecordLog("SearchFiles",extName,strResult);
return strResult.AllocSysString();
}
hRead = open_file(inPipe);
iLen = 0;
strTmp = "";
iFolder = "";
lFolder = "";
while(1)
{
txBlk=(void *)(rxBuf+iTxBuf);
//txBlk = (void *)malloc(rxLength);
success = ReadFile(hRead,txBlk,rxLength,&respLen,NULL);
if (*((WORD *)txBlk+0)==0x4B4F)
{
NameLen = *((char *)txBlk+3)-4;
for(int i=0;i<NameLen;i++)
{
strTmp = strTmp + *((char *)txBlk+4+i);
}
if (*((char *)txBlk+2)==0x03) //收到完整的路径名
{
if(strTmp.Right(1)!="/")
{
strTmp = strTmp + "/";
}
iFolder = strTmp; //当前文件夹名称
}
if (*((char *)txBlk+2)==0x02) //收到完整的文件名
{
if (iFolder!=lFolder) //当前目录改变
{
if (iFolder.Find(lFolder,0)>=0) //当前路径包含了上次路径,创建子文件夹
{
lClass = lFolder.GetLength();
for(int i=lClass;i<iFolder.GetLength();i++)
{
iClass = iFolder.Find("/",i);
if(iClass<0) break;
if(iClass>i)
{
lFolder = iFolder.Mid(i,iClass-i);
strXML = strXML + "\t<folder name=\"" + lFolder + "\">\r\n";
}
i=iClass;
}
}
else
{
if(lFolder.Find(iFolder,0)>=0)//上次路径包含了当前路径,关闭子文件夹
{
lClass = iFolder.GetLength();
for(int i=lClass;i<lFolder.GetLength();i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -