📄 serialcommdlg.cpp
字号:
// SerialCommDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SerialComm.h"
#include "SerialCommDlg.h"
#include "stdio.h"
#include "conio.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
unsigned int baseAdd;
int Receiver(void);
HANDLE hThread;
DWORD tid;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CSerialCommDlg dialog
CSerialCommDlg::CSerialCommDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialCommDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialCommDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerialCommDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialCommDlg)
DDX_Control(pDX, IDC_RECEIVE_EDIT, m_recEdit);
DDX_Control(pDX, IDC_STOPBITS_COMBO, m_stopbits);
DDX_Control(pDX, IDC_SET_BUTTON, m_setbutton);
DDX_Control(pDX, IDC_SEND_EDIT, m_send);
DDX_Control(pDX, IDC_SEND_BUTTON, m_sendbutton);
DDX_Control(pDX, IDC_PORT_COMBO, m_port);
DDX_Control(pDX, IDC_PARITY_COMBO, m_parity);
DDX_Control(pDX, IDC_DATABITS_COMBO, m_databits);
DDX_Control(pDX, IDC_BAUDRATE_COMBO, m_baudrate);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialCommDlg, CDialog)
//{{AFX_MSG_MAP(CSerialCommDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SET_BUTTON, OnSetButton)
ON_BN_CLICKED(IDC_SEND_BUTTON, OnSendButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialCommDlg message handlers
BOOL CSerialCommDlg::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 CSerialCommDlg::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 CSerialCommDlg::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 CSerialCommDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSerialCommDlg::OnSetButton()
{
// TODO: Add your control notification handler code here
CString stopBits,port,parity,dataBits,baudRate;
unsigned int LSB,MSB,dataFormat,temp;
m_stopbits.GetWindowText(stopBits);
m_port.GetWindowText(port);
m_parity.GetWindowText(parity);
m_databits.GetWindowText(dataBits);
m_baudrate.GetWindowText(baudRate);
if((stopBits == "")||(port == "")||(parity == "")||(dataBits == "")||(baudRate==""))
{
AfxMessageBox("All the 5 Parameters are Necessary");
return;
}
if(port == "1")
{
AfxMessageBox("Comm 1");
baseAdd = 0x3f8;
}
else
{
AfxMessageBox("Comm 2");
baseAdd = 0x2f8;
}
if(baudRate == "4800")
{
LSB = 0x18;
MSB = 0x00;
}
else if(baudRate == "9600")
{
LSB = 0x0c;
MSB = 0x00;
}
else if(baudRate == "19200")
{
LSB = 0x06;
MSB = 0x00;
}
if((dataBits == "8")&&(stopBits == "1")&&(parity == "NONE"))
dataFormat = 0x03;
else if((dataBits == "8")&&(stopBits == "1")&&(parity == "EVEN"))
dataFormat = 0x1b;
else if((dataBits == "8")&&(stopBits == "1")&&(parity == "ODD"))
dataFormat = 0x0b;
else if((dataBits == "8")&&(stopBits == "2")&&(parity == "NONE"))
dataFormat = 0x07;
else if((dataBits == "8")&&(stopBits == "2")&&(parity == "EVEN"))
dataFormat = 0x1f;
else if((dataBits == "8")&&(stopBits == "2")&&(parity == "ODD"))
dataFormat = 0x0f;
else if((dataBits == "7")&&(stopBits == "1")&&(parity == "NONE"))
dataFormat = 0x02;
else if((dataBits == "7")&&(stopBits == "1")&&(parity == "EVEN"))
dataFormat = 0x1a;
else if((dataBits == "7")&&(stopBits == "1")&&(parity == "ODD"))
dataFormat = 0x0a;
else if((dataBits == "7")&&(stopBits == "2")&&(parity == "NONE"))
dataFormat = 0x06;
else if((dataBits == "7")&&(stopBits == "2")&&(parity == "EVEN"))
dataFormat = 0x1e;
else if((dataBits == "7")&&(stopBits == "2")&&(parity == "ODD"))
dataFormat = 0x0e;
_outp(baseAdd+3,dataFormat); // setting dataformat register
temp = _inp(baseAdd+3);
temp = temp | 0x10;
_outp(baseAdd+3,temp); //setting DLAb bit to 1
_outp(baseAdd,LSB); // setting Baud Rate
_outp(baseAdd+1,MSB);
temp = _inp(baseAdd+3);
temp = temp & 0x7f;
_outp(baseAdd+3,temp); //Resetting DLAb bit to 1
m_stopbits.EnableWindow(FALSE);
m_port.EnableWindow(FALSE);
m_parity.EnableWindow(FALSE);
m_databits.EnableWindow(FALSE);
m_baudrate.EnableWindow(FALSE);
m_setbutton.EnableWindow(FALSE);
m_send.EnableWindow(TRUE);
m_sendbutton.EnableWindow(TRUE);
m_recEdit.EnableWindow(TRUE);
/*hThread = CreateThread(0,
0,
(LPTHREAD_START_ROUTINE) Receiver,
0,
0,
&tid);
SetThreadPriority(hThread,21);*/
AfxBeginThread((AFX_THREADPROC)Receiver,NULL);
}
void CSerialCommDlg::OnSendButton()
{
// TODO: Add your control notification handler code here
CString sendMsg;
int i = 0;
unsigned int temp;
m_send.GetWindowText(sendMsg);
for(i=0;i<sendMsg.GetLength();i++)
{
while(!( (_inp(baseAdd+5)&0x40) && (_inp(baseAdd+5)&0x20)) );
//Sleep(50);
_outp(baseAdd,sendMsg[i]);
}
m_send.SetWindowText("");
}
int Receiver()
{
CEdit *recEdit = (CEdit*)AfxGetMainWnd()->GetDlgItem(IDC_RECEIVE_EDIT);
char ch;
CString str,str1;
while(1)
{
while(!(_inp(baseAdd+5)&0x01));
ch = _inp(baseAdd);
str.Format("%c",ch);
//AfxMessageBox(str);
recEdit->GetWindowText(str1);
//AfxMessageBox(str1);
str = str1 + str;
recEdit->SetWindowText("");
recEdit->SetWindowText(str);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -