📄 commdlg.cpp
字号:
// commDlg.cpp : implementation file
//
#include "stdafx.h"
#include "comm.h"
#include "commDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int DBrefStatus;
char buf[255];
BOOL DbConn=false;//conn:是否进行了连接;DbConn:是否连接成功
CString driver,dbname,server,dbuser,dbpassward,tablename;
CString strConn;
int linkflag=0;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCommDlg dialog
CCommDlg::CCommDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCommDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCommDlg)
m_re = _T("");
m_se = _T("");
m_port = 1;
m_renum = 0;
m_senum = 0;
m_log = TRUE;
m_database = _T("xnt_op");
m_password = _T("");
m_tablename = _T("TABLE1");
m_usename = _T("sa");
m_servername = _T("kugui");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCommDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCommDlg)
DDX_Text(pDX, IDC_EDIT2, m_re);
DDX_Text(pDX, IDC_EDIT1, m_se);
DDX_Text(pDX, IDC_portnum, m_port);
DDV_MinMaxInt(pDX, m_port, 1, 8);
DDX_Text(pDX, IDC_RE, m_renum);
DDX_Text(pDX, IDC_SE, m_senum);
DDX_Check(pDX, IDC_CHECK1, m_log);
DDX_Text(pDX, IDC_database, m_database);
DDX_Text(pDX, IDC_password, m_password);
DDX_Text(pDX, IDC_tablename, m_tablename);
DDX_Text(pDX, IDC_usename, m_usename);
DDX_Text(pDX, IDC_server, m_servername);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCommDlg, CDialog)
//{{AFX_MSG_MAP(CCommDlg)
ON_WM_SYSCOMMAND()
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_open, Onopen)
ON_BN_CLICKED(IDC_send, Onsend)
ON_BN_CLICKED(IDC_stop, Onstop)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCommDlg message handlers
BOOL CCommDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
}
void CCommDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CCommDlg::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 CCommDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCommDlg::Onopen()
{
// TODO: Add your control notification handler code here
memset(&buf, 0, sizeof(buf));
UpdateData (TRUE);
if (m_Ports[m_port-1].InitPort(this, m_port, 57600))
{ m_Ports[m_port-1].StartMonitoring();
SetTimer (1,100,NULL);
}
else
{
AfxMessageBox ("port not found!");
}
}
void CCommDlg::Onsend()
{
// TODO: Add your control notification handler code here//@01RD17#@01RD17#R4#
GetDlgItemText(IDC_EDIT1, buf, sizeof(buf));
if (buf[0] == 0)
return;
//for (int i=0;i<253;i++)
// buf[i] =103;
buf[strlen(buf)] = 13;
buf[strlen(buf)+1]=0x00;
m_Ports[m_port-1].WriteToPort(buf);
m_senum++;
UpdateData (FALSE);
}
LONG CCommDlg::OnCommunication(WPARAM ch, LPARAM port)//侦听接收字符
{
if (port <= 0 || port > 2)
return -1;
if (ch == 13 )//判断是否串结束,如果是串尾则送显处理并清空接收串
{
m_re.Empty ();
UpdateData (FALSE);
m_re=m_strReceived[port-1];
m_renum++;
m_se=m_strReceived[port-1];
m_strReceived[port-1].Empty();
UpdateData (FALSE);
Onsend();
}
else
m_strReceived[port-1] += (char)ch;
return 0;
}
void CCommDlg::Onstop()
{
// TODO: Add your control notification handler code here
KillTimer (1);
for (int i = 0; i < 1; i++)
{
if (m_Ports[m_port-1].InitPort(this, i + 1, 57600))
m_Ports[m_port-1].StopMonitoring();
else
{
AfxMessageBox ("port not found!");
}
}
}
void CCommDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
//if (nIDEvent==1)
// Onsend();
CDialog::OnTimer(nIDEvent);
}
void CCommDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
void CCommDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CCommDlg::Onconnect()
{
// TODO: Add your control notification handler code here
HRESULT hr;
int log=m_log;//log==1时,仅WINDOWS验证,==0时WIN和SQL验证
driver = "sqloledb";
UpdateData (TRUE);
server=m_servername;
dbname=m_database;
dbuser=m_usename;
dbpassward=m_password;
tablename=m_tablename;
linkflag=1;
if(log)
strConn = "Provider=" + driver + //Persist Security Info=False;
"; Trusted_Connection=yes;Data Source=" + server +
"; Initial Catalog=" + dbname +
"; User Id=" + dbuser +
"; Password=" + dbpassward;
else
strConn = "Provider=" + driver +
"; Data Source=" + server +
"; Initial Catalog=" + dbname +
"; User Id=" + dbuser +
"; Password=" + dbpassward;
try//连接数据库
{
hr = m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
cmdA.CreateInstance(__uuidof(Command));
if(SUCCEEDED(hr))
m_pConnection->ConnectionString = (_bstr_t)strConn;
m_pConnection->ConnectionTimeout = 20;
if (m_pConnection->State)
m_pConnection->Close();
m_pConnection->Open( "", "", "",adConnectUnspecified );
cmdA->ActiveConnection=m_pConnection;
DBrefStatus=1;
DbConn=true;
}
catch(_com_error e)
{
DbConn=false;
CString mm;
mm.Format("连接数据库失败! %s",e.ErrorMessage());
AfxMessageBox(mm);
}
catch (...)
{
DbConn=false;
AfxMessageBox("连接数据库时发生未知错误,请与管理员联系");
}
if (DbConn)
AfxMessageBox("连接数据库成功,请进行后续操作!");
}
void CCommDlg::OnButton1()
{
// TODO: Add your control notification handler code here
Onconnect();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -