Click to See Complete Forum and Search --> : File transfer using COM ports


janinho
October 30th, 2005, 01:15 PM
Hi everyone, I have tried to create a program that sends a file to a comport and also reads data from a comport and writes it as a file.

When finished, this program is supposed to be used as a Bluetooth file transfer tool between two PC's connected to each other via Bluetooth and using virtual COM ports. COM8 is the Serial Port A of my USB bluetooth dongle so that's why I'm using it in this program.

I'm somewhat of a newbie especially in programming serial connections so I might have some stupid errors - So far I have just tried to make the program write a file to a comport and then instantly read it from the comport and save to a different file. However the latter part of the program doesn't work, it just crashes my pc.

Any help would be greatly appreciated!

Here's what I have managed to do so far:

#include <windows.h>
#include <iostream>
#include <cstdlib>

using namespace std;

HANDLE OpenSequentialFile(LPCTSTR Path,DWORD OpenMode) // an helper function
{
return CreateFile(
Path
,OpenMode,FILE_SHARE_READ
,NULL
,OPEN_EXISTING
,FILE_FLAG_SEQUENTIAL_SCAN
,NULL);
}



void CopyFileToFile(HANDLE SrcFile,HANDLE DestFile)
{
static const size_t BufferSize=16384;
BYTE *Buffer=new BYTE[BufferSize];
DWORD cBytesRead;
while(ReadFile(SrcFile,Buffer,BufferSize*sizeof(BYTE),&cBytesRead,NULL) && cBytesRead>0)
{
DWORD cBytesWritten;
WriteFile(DestFile,Buffer,cBytesRead,&cBytesWritten,NULL);
}
delete[] Buffer;
}

int WriteFileToCOM(LPCTSTR FilePath)
{
HANDLE hFile=OpenSequentialFile(FilePath,GENERIC_READ);
if (hFile==INVALID_HANDLE_VALUE) return 1;

HANDLE hComm=CreateFile
(
TEXT("COM8"),
GENERIC_READ | GENERIC_WRITE,0,0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
if (hComm==INVALID_HANDLE_VALUE) {CloseHandle(hFile);return 2;}
COMMCONFIG CommConfig;
DWORD SizeOfCommConfig = sizeof(CommConfig);
ZeroMemory( &CommConfig, SizeOfCommConfig );
if ( GetDefaultCommConfig( TEXT("COM8"), &CommConfig, &SizeOfCommConfig ) )
if ( ! SetCommConfig( hComm, &CommConfig, SizeOfCommConfig ) )
{CloseHandle(hComm);CloseHandle(hFile);return 3;}

CopyFileToFile(hFile,hComm);
CloseHandle(hComm);
CloseHandle(hFile);
return 0;
}
int WriteCOMToFile(LPCTSTR FilePath)
{
HANDLE hFile=CreateFile
(
FilePath,
GENERIC_READ | GENERIC_WRITE,0,0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
HANDLE hComm=OpenSequentialFile(TEXT("COM8"),GENERIC_READ);

COMMCONFIG CommConfig;
DWORD SizeOfCommConfig = sizeof(CommConfig);
ZeroMemory( &CommConfig, SizeOfCommConfig );
if ( GetDefaultCommConfig( TEXT("COM8"), &CommConfig, &SizeOfCommConfig ) )
if ( ! SetCommConfig( hComm, &CommConfig, SizeOfCommConfig ) )
{CloseHandle(hComm);CloseHandle(hFile);return 3;}

CopyFileToFile(hComm,hFile);
CloseHandle(hFile);
CloseHandle(hComm);
return 0;
}


int main()
{
int choice ;

cout << "Enter choice:";
cin >> choice;


switch (choice)
{
case 1:
cout << "Opening port, press power shortly" << endl;
WriteFileToCOM("c:\\file1.txt");
WriteCOMToFile("c:\\file2.txt");

break;

case 2:
cout << "Opening port, press power shortly" << endl;
break;

case 3:
cout << "Opening port, press power shortly" << endl;
break;

default:
cout<<"Invalid option!" <<endl;
break;

}

cin.get();

system("PAUSE");

return 0;

}