LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes
DWORD dwCreationDistribution, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to copy
);
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// Define may be used at same time,
// extern TForm2 *Form2;
HANDLE hcom;
HANDLE xModemThreadHandle;
DWORD idxModemThread;
// Define one extern thread and used serier port,
// extern void xModemThread();
//---------------------------------------------------------------------------
bool __fastcall TForm1::InitPort(AnsiString PortName)
{
_DCB myDCB;
COMMTIMEOUTS myTMO;
bool Result;
// simply define communication handle,if existing and closing
// renew to create,
if(hcom != 0/*INVALID_HANDLE_VALUE*/)
CloseHandle(hcom);
//create communication handle,
hcom=CreateFile(PortName.c_str(), // port name
GENERIC_READ¦GENERIC_WRITE, // RW access mode
0, // exclusive mode
NULL, // no security attributes
OPEN_EXISTING, // open existing port
0, // not overlapped
0);
Result = (hcom!=INVALID_HANDLE_VALUE);
if(!Result)
MessageDlg("not open port:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0);
else
{
//set communlcation mask,set of events to be monitored,
if(!SetCommMask(hcom,EV_RXFLAG))
{
if(MessageDlg("not set mask:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//initializes the communications parameters,
if(!SetupComm(hcom,4096,4096))//4096 is specifies send and recvieve size,
{
if(MessageDlg("not setup communiation:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//get the communications state and DCB struct,
if(!GetCommState(hcom,&myDCB))
{
if(MessageDlg("not get state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set dcb state,include baudrate/bytebit/stopbit/parity,
myDCB = GetDCBParater(myDCB);
// if(!BuildCommDCB("COM1:baud=1200 parity=N data=8 stop=1",&myDCB));
// { 以上个的函数用法同下 }
if(!SetCommState(hcom,&myDCB))
{
//unsigned long TTT = GetLastError();
if(MessageDlg("not set state:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
if(!GetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not get out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//set out time function and coeff.
myTMO.ReadIntervalTimeout = MAXDWORD;
myTMO.ReadTotalTimeoutMultiplier = 0;
myTMO.ReadTotalTimeoutConstant = 0;
myTMO.WriteTotalTimeoutMultiplier =0;
myTMO.WriteTotalTimeoutConstant =1000;
if(!SetCommTimeouts(hcom,&myTMO))
{
if(MessageDlg("not set out time:"+PortName,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
{
CloseHandle(hcom);
Result=false;
exit(1);
}
else{
CloseHandle(hcom);
Result=true;
exit(1);
}
}
//API_Function: clear send buffer,
PurgeComm(hcom,PURGE_TXCLEAR);
//API-Function: clear recvive buffer,
PurgeComm(hcom,PURGE_RXCLEAR);
}
return Result;
}