📄 serialportapidlg.cpp
字号:
// SerialPortAPIDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SerialPortAPI.h"
#include "SerialPortAPIDlg.h"
#include "SetPortDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define COM_RECVDATA WM_USER+1000//自定义消息
HANDLE hCom; //全局变量,串口句柄
HANDLE hCommThread; //全局变量,串口线程
extern DWORD WINAPI SerialPort1ThreadProcess(HWND hWnd);
int InputLen();
//自定义变量
const int MAX_MESSAGE=101;
int GetSendLen=0,RecvLen=0;
CString RecvStr;
DCB wdcb;
int ButtonCheck=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()
/////////////////////////////////////////////////////////////////////////////
// CSerialPortAPIDlg dialog
CSerialPortAPIDlg::CSerialPortAPIDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialPortAPIDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialPortAPIDlg)
m_recv = _T("");
m_send = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerialPortAPIDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialPortAPIDlg)
DDX_Text(pDX, IDC_RECV_EDIT, m_recv);
DDX_Text(pDX, IDC_SEND_EDIT, m_send);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialPortAPIDlg, CDialog)
//{{AFX_MSG_MAP(CSerialPortAPIDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPENPORT, OnOpenport)
ON_BN_CLICKED(IDC_CLOSEPORT, OnCloseport)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_SETPORT, OnSetport)
ON_MESSAGE(COM_RECVDATA, OnRecvData)
ON_BN_CLICKED(IDC_HDX_SHOW, OnHdxShow)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialPortAPIDlg message handlers
BOOL CSerialPortAPIDlg::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
DWORD threadID;
hCommThread = ::CreateThread((LPSECURITY_ATTRIBUTES)NULL, 0,
(LPTHREAD_START_ROUTINE)SerialPort1ThreadProcess,
AfxGetMainWnd()->m_hWnd, 0, &threadID);
if (hCommThread == NULL)
{
::AfxMessageBox("创建串口1处理线程失败");
::PostQuitMessage(0);
}
((CButton*)GetDlgItem(IDC_HDX_SHOW))->SetCheck(ButtonCheck);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSerialPortAPIDlg::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 CSerialPortAPIDlg::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 CSerialPortAPIDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSerialPortAPIDlg::OnOpenport()
{
// TODO: Add your control notification handler code here
//打开并设置com1
hCom=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE,
0,NULL , OPEN_EXISTING, 0, NULL);
if (hCom==(HANDLE)-1)
{
AfxMessageBox("打开COM1失败");
}
else
{
GetCommState(hCom, &wdcb);
m_lBaudRate=wdcb.BaudRate;
m_nByteSize=wdcb.ByteSize;
m_nStopBits=wdcb.StopBits;
wdcb.BaudRate=9600;//波特率:9600,其他:不变
SetCommState (hCom, &wdcb);
PurgeComm(hCom, PURGE_TXCLEAR);
COMMTIMEOUTS comTimeOut;
comTimeOut.ReadIntervalTimeout = MAXDWORD;
comTimeOut.ReadTotalTimeoutMultiplier = 0;
comTimeOut.ReadTotalTimeoutConstant = 0;
comTimeOut.WriteTotalTimeoutMultiplier = 50;
comTimeOut.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(hCom,&comTimeOut);//设置超时
GetDlgItem(IDC_OPENPORT)->EnableWindow(FALSE);
GetDlgItem(IDC_CLOSEPORT)->EnableWindow(TRUE);
}
}
void CSerialPortAPIDlg::OnCloseport()
{
// TODO: Add your control notification handler code here
CloseHandle(hCom);
GetDlgItem(IDC_CLOSEPORT)->EnableWindow(FALSE);
GetDlgItem(IDC_OPENPORT)->EnableWindow(TRUE);
AfxMessageBox("COM1 close!");
}
void CSerialPortAPIDlg::OnSend()
{
// TODO: Add your control notification handler code here
UpdateData(true);
DWORD wCount = 0;
if(m_send.GetLength()>0)
{
if(WriteFile(hCom, m_send, m_send.GetLength(), &wCount, NULL)!=0)//发送数据
{
GetSendLen = m_send.GetLength();
char s[10];
wsprintf(s,"%d char",GetSendLen);
SetDlgItemText(IDC_DATA,s);
}
else
{
SetDlgItemText(IDC_DATA,"NULL");
AfxMessageBox("This port is not open");
}
}
}
DWORD WINAPI SerialPort1ThreadProcess( HWND hWnd)//主窗口句柄
{
//获得类对象CSerialPortAPIDlg *pApp = (CSerialPortAPIDlg*)AfxGetApp();
CString outPut;
CSerialPortAPIDlg gsl;
char str[MAX_MESSAGE];
char str2[MAX_MESSAGE];
DWORD wCount; //读取的字节数
while(1)
{
ReadFile(hCom,str, GetSendLen, &wCount, NULL);
if(wCount > 0) //收到数据
{
str[wCount] =NULL;
RecvLen=RecvLen+wCount;
if(ButtonCheck==1)
{
for(DWORD i=0; i<wCount;i++)
{
wsprintf(str2,"%x",str[i]);
RecvStr=RecvStr+strupr((char*)str2)+" ";
}
}
else
{
RecvStr+=(char*)str;
}
::PostMessage(hWnd, COM_RECVDATA, (unsigned int)str, wCount);
//发送消息给对话框主窗口,以进行接收内容的显示
}
}
return TRUE;
}
void CSerialPortAPIDlg::OnRecvData(WPARAM wParam,LPARAM lParam)
{
//CString recvStr((char *)wParam);
m_recv = RecvStr;
char s[10];
wsprintf(s,"%d char",RecvLen);
SetDlgItemText(IDC_TOLDATA,s);
char t[10];
wsprintf(t,"%d char",m_recv.GetLength());
SetDlgItemText(IDC_TOTOL,t);
UpdateData(false);
}
void CSerialPortAPIDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_send="";
UpdateData(FALSE);
CString ss="61 7a";
char str1[10];
char sq[10];
char *str="0x63";
char *stop;
/*
strcpy(temp,"123");
短整型(int)
i = atoi(temp);
长整型(long)
l = atol(temp);
浮点(double)
d = atof(temp);
*/
char x[10];
strtol(str,&stop,8);
wsprintf(x,"%d",str);
//x=str.Left(4);
//char xx[]=x;
wsprintf(str1,"%c",stop);
MessageBox(str1);
}
void CSerialPortAPIDlg::OnSetport()
{
// TODO: Add your control notification handler code here
SetPortDlg spd;
if (spd.DoModal()==IDOK)
{
GetCommState(hCom,&wdcb);
//long baudrate;
//wdcb.BaudRate=spd.m_nBaudRate;
//wdcb.ByteSize=spd.m_nByteSize;
//wdcb.StopBits=spd.m_strStopBits;
SetCommState(hCom,&wdcb);
}
}
void CSerialPortAPIDlg::OnHdxShow()
{
// TODO: Add your control notification handler code here
ButtonCheck=((CButton*)GetDlgItem(IDC_HDX_SHOW))->GetCheck();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -