Приклад програми
// laba1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "laba1.h"
#include "laba1Dlg.h"
#include "ComPort.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLaba1Dlg dialog
CLaba1Dlg::CLaba1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CLaba1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLaba1Dlg)
m_Edit = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLaba1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLaba1Dlg)
DDX_Text(pDX, IDC_EDIT1, m_Edit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLaba1Dlg, CDialog)
//{{AFX_MSG_MAP(CLaba1Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLaba1Dlg message handlers
BOOL CLaba1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLaba1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLaba1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int CLaba1Dlg::WriteString(string Str)
{
PurgeComm(hPort,PURGE_TXCLEAR | PURGE_RXCLEAR);
Str = Str + "\r";
DWORD dwError , dwNumBytesWritten;
WriteFile(
hPort,
Str.c_str(),
Str.length(),
&dwNumBytesWritten,
NULL);
return (int)dwNumBytesWritten;
}
int CLaba1Dlg ::Open()
{
hPort = CreateFile(
ComPortName.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if(hPort == INVALID_HANDLE_VALUE)
{
return 0;
}
DCB PortDCB;
PortDCB.DCBlength = sizeof(DCB);
GetCommState(hPort , &PortDCB);
PortDCB.BaudRate = 9600;
PortDCB.fBinary = true; //!
PortDCB.fParity = true;
PortDCB.fOutxCtsFlow = false;
PortDCB.fOutxDsrFlow = false;
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
PortDCB.fDsrSensitivity = false;
PortDCB.fTXContinueOnXoff = false;
PortDCB.fOutX = false;
PortDCB.fInX = false;
PortDCB.fErrorChar = true;
PortDCB.fNull = false;
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
PortDCB.fAbortOnError = false;
PortDCB.ByteSize = 8;
PortDCB.Parity = NOPARITY;
PortDCB.StopBits = ONESTOPBIT;
// PortDCB.EofChar = '!';
if(!SetCommState(hPort , &PortDCB))
{
return 0;
// ShowMessage("Unable to configure the serial port");
}
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(hPort,&CommTimeouts);
CommTimeouts.ReadIntervalTimeout = 50;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.ReadTotalTimeoutConstant = 50;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;
CommTimeouts.WriteTotalTimeoutConstant = 50;
SetCommTimeouts(hPort,&CommTimeouts);
FlushFileBuffers(hPort);
Opened = true;
return 1;
}
int CLaba1Dlg ::Close()
{
if(Opened)
{
CloseHandle(hPort);
Opened = false;
return 1;
}
else
return 0;
}
void CLaba1Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
Open();
WriteString(m_Edit.LoadString);
Close();
}