串列埠通訊(檔案傳輸)流控制
出處:http://blog.sina.com.cn/s/blog_6c617ee30100u943.html
#include <afx.h>//send.cpp
#include <iostream>
#include <windows.h>
#include <process.h>
#include <string.h>
using namespace std;
HANDLE g_hCommDev=0;//串列埠控制代碼
HANDLE g_hEvent=0;
char filename[MAX_PATH];//檔名
char *wbuff;//指向輸出緩衝區,後面new
FILE *wf;
DWORD z=0;//檔案大小
void DiretorySearch(const char *dirPath);
void CommWatchProc(LPVOID pParam);
void CommLive(LPVOID pParam);
bool SetupSynCom()
{
DCB dcb;
COMMTIMEOUTS timeouts;
if((g_hCommDev=CreateFile("COM1",GENERIC_WRITE,
0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL))
==INVALID_HANDLE_VALUE)
return false;
timeouts.ReadIntervalTimeout=1000;
timeouts.ReadTotalTimeoutConstant=1000;
timeouts.ReadTotalTimeoutMultiplier=1000;
timeouts.WriteTotalTimeoutConstant=1000;
timeouts.WriteTotalTimeoutMultiplier=1000;
SetCommTimeouts(g_hCommDev,&timeouts);
SetupComm(g_hCommDev,1,102400);
GetCommState(g_hCommDev,&dcb);
dcb.BaudRate=CBR_115200 ;
dcb.fParity=NOPARITY;
dcb.ByteSize=8;
dcb.StopBits=ONESTOPBIT;
dcb.fOutxCtsFlow=true;
dcb.fRtsControl=1;
SetCommState(g_hCommDev,&dcb);
g_hEvent=CreateEvent(NULL,FALSE,true,"WatchEvevt");
char pathName[MAX_PATH];
char szPathTemp[MAX_PATH];
GetModuleFileName(NULL, szPathTemp, MAX_PATH);
for (int i=strlen(szPathTemp); i>=0; i--)
{
if (szPathTemp[i] == '\\')
{
break;
}
szPathTemp[i]='\0';
}
strcat(szPathTemp,"liuhansend.ini");
CString myhere;
::GetPrivateProfileString("StudentInfo","Name",NULL,myhere.GetBuffer(MAX_PATH),MAX_PATH,szPathTemp);
strcpy(pathName,myhere);
cout<<"檔案目錄位於:"<<pathName<<endl;
cout<<"配置檔案位於"<<szPathTemp<<endl;
_beginthread(&CommWatchProc,0,pathName);
_beginthread(&CommLive,0,NULL);
return true;
}
bool WriteComm(char *lpSneBuffer,DWORD dwBytesToWrite)
{
bool bWriteState;
DWORD dwBytesWritten=0;
bWriteState=WriteFile(g_hCommDev,lpSneBuffer,dwBytesToWrite,
&dwBytesWritten,NULL);
z=z-(int)dwBytesWritten;
cout<<"傳送了"<<dwBytesWritten<<"位元組資料,本次還剩"<<z<<"位元組資料沒有傳送\n";
if(!bWriteState || dwBytesWritten!=dwBytesToWrite)
{
cout<<"\n\n傳送失敗!"<<endl;
Sleep(-1);
return false;
}
else return true;
}
void CloseSynComm()
{
SetCommMask(g_hCommDev,0);
//WaitForSingleObject(hEvent,INFINITE);
//CloseHandle(hEvent);
PurgeComm(g_hCommDev,PURGE_TXCLEAR|PURGE_RXCLEAR);
//CloseHandle(g_hCommDev);
}
void CommWatchProc(LPVOID pParam)
{
char *p=(char *)pParam;
wbuff=new char[102400];
while(true)
{
DiretorySearch(p);
}
delete wbuff;
}
void CommLive(LPVOID pParam)
{
while(true){
DWORD CTSUP=0;
GetCommModemStatus(g_hCommDev,&CTSUP);
if((CTSUP & MS_CTS_ON) != MS_CTS_ON)
{
cout<<"請檢查串列埠線是否插好!\n";
int i=0;
for(;i<10;i++)
{ CTSUP=0;
GetCommModemStatus(g_hCommDev,&CTSUP);
if((CTSUP & MS_CTS_ON) != MS_CTS_ON)
{
Sleep(1000);
}
else break;
}
if(i>9)
{
cout<<"等待超時,請重啟兩邊程式!\n";
exit(0);
}
}
else {
Sleep(3000);
}
}
}
//遞迴遍歷指定資料夾下的所有資料夾和檔案
void DiretorySearch(const char *dirPath)
{
WIN32_FIND_DATAA lpFindFileData;
char dirPathTemp[MAX_PATH];
char dirCodeTemp[MAX_PATH];
strcpy(dirPathTemp, dirPath);
strcpy(dirCodeTemp, dirPath);
const char *pChar = strrchr(dirPath, '\\');
if(pChar != NULL && strlen(pChar) == 1)
strcat(dirCodeTemp, "*");
else
{
strcat(dirCodeTemp, "\\*");
strcat(dirPathTemp, "\");
}
HANDLE handle = FindFirstFileA(dirCodeTemp, &lpFindFileData);
if(handle == INVALID_HANDLE_VALUE)
{
cout<<dirPathTemp<<"檢索失敗!"<<endl;
return;
}
if((lpFindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) //判斷檔案屬性
{
ZeroMemory(filename,MAX_PATH);
strcpy(filename,dirPathTemp);
int filelong=(lpFindFileData.nFileSizeHigh * (MAXDWORD+1) + lpFindFileData.nFileSizeLow);
filename[strlen(filename)]='?';//檔名結束符,後面新增檔案大小,好一起傳輸
sprintf((filename+strlen(filename)),"%d",filelong);
if(filelong==0)
{
if(remove(dirPathTemp)==0)
cout<<dirPathTemp<<"為空檔案,直接刪除了\n\n";
}
else{
DWORD dwBytesWritten1=0;
WriteFile(g_hCommDev,filename,MAX_PATH,
&dwBytesWritten1,NULL);
PurgeComm(g_hCommDev,PURGE_TXCLEAR|PURGE_RXCLEAR);//清空緩衝區
Sleep(50);
cout<<"準備傳出檔案:"<<filename<<endl;
wf=fopen(dirPathTemp,"rb+");
z=filelong;
ZeroMemory(wbuff,102400);
if(z<=102400)
{
if(z>0)
{
fread(wbuff,z,1,wf);
WriteComm(wbuff,z);
}
}
else{
fread(wbuff,102400,1,wf);
WriteComm(wbuff,102400);
while(true)
{
ZeroMemory(wbuff,102400);
if(z<=102400)
{
if(z>0)
{
fread(wbuff,z,1,wf);
WriteComm(wbuff,z);
}
break;
}
else{