驅(qū)動(dòng)已經(jīng)成功安裝,設(shè)備管理器里出現(xiàn)圖標(biāo)?,F(xiàn)在想用下載的程序:CH372DBG/DOWNFILE/DOWNFILE.EXE來以十六進(jìn)制輸入3個(gè)字符共12位到下位機(jī)板來調(diào)試。其中DEMO.C如下: // 2003.09.08, 2003.12.28, 2004.12.10 //**************************************** //** Copyright (C) W.ch 1999-2004 ** //** Web: http://www.winchiphead.com ** //**************************************** //** EXE for USB interface chip CH375 ** //** C, VC5.0 ** //**************************************** // // USB總線接口芯片CH375的應(yīng)用層演示程序 V1.1 // 南京沁恒電子有限公司 作者: W.ch 2004.12 // CH375-EXE V1.1 , Support: Ctrl/Bulk/Int // 運(yùn)行環(huán)境: Windows 98/ME, Windows 2000/XP // support USB chip: CH372/CH375 //
#include #include #include #include
#include "CH375DLL.H" // CH372/CH375的動(dòng)態(tài)鏈接庫 #include "DEMO.H"
HINSTANCE mSaveInstance; // 實(shí)例 HWND mSaveDialogMain; // 主對(duì)話框
UINT mIndex = 0; // 如果有多個(gè)CH372/CH375則指示序號(hào)
CHAR mCaptionInform[] = " 信息提示 "; CHAR CmdFile[256]; HANDLE hFile; OPENFILENAME mOpenFile;
LRESULT CALLBACK mDialogMain(HWND,UINT,WPARAM,LPARAM); //主對(duì)話框調(diào)用程序 HANDLE OpencmdFile(HWND hDialog,UCHAR *FileName); BOOL TransFile(HWND hDialog);
//程序入口 int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd ) { CHAR *CmdLine; CmdLine = lpCmdLine; memcpy(CmdFile,CmdLine,strlen(CmdLine)); mSaveInstance = hInstance; return( DialogBox( hInstance, "IDD_MAIN", NULL, mDialogMain ) ); //調(diào)用主對(duì)話框 }
//主對(duì)話框事件 LRESULT CALLBACK mDialogMain( HWND hDialog, UINT uMessage, WPARAM wParam, LPARAM lParam ) { switch( uMessage ) { case WM_INITDIALOG: //初始化 { if(CmdFile != NULL) SetDlgItemText(hDialog,IDC_EDIT_FILE,CmdFile); } return ( TRUE ); break; case WM_COMMAND: switch ( LOWORD( wParam ) ) { case IDC_CMD1: // 以十六進(jìn)制輸入3個(gè)字符共12位到演示板上的CH451芯片 { UCHAR pFileN[100]; // 需要使用DLL則需要先加載 if ( LoadLibrary( "CH375DLL.DLL" ) == NULL ) // 加載DLL失敗,可能未安裝到系統(tǒng)中 { MessageBox( hDialog, "無法加載CH372/CH375的DLL", mCaptionInform, MB_ICONSTOP | MB_OK ); EndDialog( hDialog, 0x81 ); // 關(guān)閉對(duì)話框 return( TRUE ); } if ( CH375OpenDevice( mIndex ) == INVALID_HANDLE_VALUE ) /* 使用之前必須打開設(shè)備 */ { MessageBox( hDialog, "無法打開CH372/CH375設(shè)備", mCaptionInform, MB_ICONSTOP | MB_OK ); EndDialog( hDialog, 0x81 ); // 關(guān)閉對(duì)話框 return( TRUE ); } CH375SetTimeout( mIndex, 5000, 5000 ); // 設(shè)置USB數(shù)據(jù)讀寫的超時(shí),超過3000mS未完成讀寫將強(qiáng)制返回,避免一直等待下去 CH375SetExclusive( mIndex, TRUE ); // 設(shè)置獨(dú)占使用當(dāng)前CH375設(shè)備,在此之后其它應(yīng)用程序不能同時(shí)使用當(dāng)前這個(gè)CH375設(shè)備 mSaveDialogMain = hDialog; if(CmdFile == "") { GetDlgItemText(hDialog,IDC_EDIT_FILE,pFileN,100); }
else { strcpy(pFileN,""); memcpy(pFileN,CmdFile,strlen(CmdFile)); pFileN[strlen(CmdFile)]='\0'; } if(pFileN =="") MessageBox(hDialog,"請(qǐng)輸入文件名!",mCaptionInform,MB_OK|MB_ICONSTOP); else { hFile=OpencmdFile(hDialog,pFileN); if((hFile != 0) && (hFile != NULL)) { TransFile(hDialog); CloseHandle(hFile); } } CH375CloseDevice( mIndex ); // 退出程序前必須關(guān)閉CH375設(shè)備 } return( TRUE ); break; case IDC_BUTTON_OPEN: { OPENFILENAME mOpenFile; TCHAR szFile[260] = "\0"; // Fill in the OPENFILENAME structure to support a template and hook. mOpenFile.lStructSize = sizeof(OPENFILENAME); mOpenFile.hwndOwner = hDialog; mOpenFile.hInstance = mSaveInstance; mOpenFile.lpstrFilter = "目標(biāo)程序 *.*\x0*.*\x0\x0"; mOpenFile.lpstrCustomFilter = NULL; mOpenFile.nMaxCustFilter = 0; mOpenFile.nFilterIndex = 0; mOpenFile.lpstrFile = szFile; mOpenFile.nMaxFile = sizeof(szFile); mOpenFile.lpstrFileTitle = NULL; mOpenFile.nMaxFileTitle = 0; mOpenFile.lpstrInitialDir = NULL; mOpenFile.lpstrTitle = "選擇將下載的目標(biāo)程序文件"; mOpenFile.nFileOffset = 0; mOpenFile.nFileExtension = 0; mOpenFile.lpstrDefExt = NULL; mOpenFile.lCustData = 0; mOpenFile.lpfnHook = NULL; mOpenFile.lpTemplateName = NULL; mOpenFile.Flags = OFN_SHOWHELP | OFN_EXPLORER | OFN_READONLY | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&mOpenFile)) { memset(CmdFile,0x00,sizeof(CmdFile)); memcpy(CmdFile,mOpenFile.lpstrFile,strlen(mOpenFile.lpstrFile)); SetDlgItemText(hDialog,IDC_EDIT_FILE,CmdFile); } else { return (TRUE); } } return (TRUE); break; case IDC_BUTTON_CLOSE: case WM_DESTROY: // 退出 EndDialog( hDialog, 1 ); return( TRUE ); break; } break; } return ( FALSE ); }
HANDLE OpencmdFile(HWND hDialog,UCHAR *FileName) { TCHAR szFile[260] = "\0"; USHORT len = 0; HANDLE hf; //打開存在的文件 if ((hf = CreateFile((LPCTSTR)FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL)) == (HANDLE)-1) { MessageBox( hDialog, "文件打開失敗", mCaptionInform, MB_OK ); return 0; } return hf; }
BOOL TransFile(HWND hDialog) { UCHAR BufFile[1024]; ULONG Trb; UCHAR ustatu[10]; ULONG ReadLen; ULONG mLen=0; UCHAR by=0; ULONG bylen=0; SetDlgItemText(hDialog,IDC_STATIC_STATUS,"0K"); Trb=0; CH375WriteData( mIndex, &by, &bylen ); //發(fā)零字節(jié) while(1) { ReadLen=1024; ReadFile(hFile,&BufFile[0],ReadLen,&ReadLen,NULL); if(ReadLen < 1024) { mLen = ReadLen; if ( CH375WriteData( mIndex, &BufFile[0], &mLen ) ) // 通過CH375發(fā)送命令數(shù)據(jù),成功 { sprintf(ustatu,"%d",Trb); strcat(ustatu,"K"); if(mLen != 0); Trb=Trb++; sprintf(ustatu,"%d",Trb); strcat(ustatu,"K"); SetDlgItemText(hDialog,IDC_STATIC_STATUS,ustatu); MessageBox( hDialog, "寫結(jié)束!", mCaptionInform, MB_OK ); break; } else { MessageBox( hDialog, "CH375WriteData 失敗", mCaptionInform, MB_OK | MB_ICONERROR ); break; } } else { mLen = ReadLen; if ( CH375WriteData( mIndex, &BufFile[0], &mLen ) ) // 通過CH375發(fā)送命令數(shù)據(jù),成功 { sprintf(ustatu,"%d",Trb); strcat(ustatu,"K"); if(mLen != 0); Trb++; sprintf(ustatu,"%d",Trb); strcat(ustatu,"K"); SetDlgItemText(hDialog,IDC_STATIC_STATUS,ustatu); } else { MessageBox( hDialog, "CH375WriteData 失敗", mCaptionInform, MB_OK | MB_ICONERROR ); break; } } } return FALSE; } 我想知道,生成的DOWNFILE.EXE應(yīng)用程序,需要打開文件才能下傳,因?yàn)槲覍?duì)上位機(jī),應(yīng)用程序不是很懂,它要下傳的文件是什么格式,怎么寫呢? 另外:直接輸入數(shù)也可以下傳是嗎?