📄 argoxtestdlg.cpp
字号:
// argoxtestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "argoxtest.h"
#include "argoxtestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CArgoxtestDlg dialog
HANDLE hRendEvent = INVALID_HANDLE_VALUE;
HANDLE hReadThread = INVALID_HANDLE_VALUE;
CArgoxtestDlg::CArgoxtestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CArgoxtestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CArgoxtestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CArgoxtestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CArgoxtestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CArgoxtestDlg, CDialog)
//{{AFX_MSG_MAP(CArgoxtestDlg)
ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CArgoxtestDlg message handlers
BOOL CArgoxtestDlg::OnInitDialog()
{
HANDLE hWnd;
DWORD dwTStat;
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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
Argox_Init(_T("COM3:"),CBR_9600);
hWnd=::FindWindow(NULL,_T("argoxtest"));
hReadEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
//Create write thread.Read thread created when port opened.
hReadThread = CreateThread(NULL,0,ReadThread,hWnd,0,&dwTStat);
if(hReadThread)
CloseHandle(hReadThread);
return TRUE; // return TRUE unless you set the focus to a control
}
BOOL CArgoxtestDlg::Argox_Init(CString port,int BaudRate)
{
DCB dcb;
hCom=CreateFile(port,//COM3口
GENERIC_READ|GENERIC_WRITE, //允许读和写
0, //独占方式
NULL,
OPEN_EXISTING, //打开而不是创建
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, //重叠方式
NULL);
if(hCom==(HANDLE)-1)
{
AfxMessageBox(_T("打开COM失败!"));
return FALSE;
}
SetupComm(hCom,512,512); //输入缓冲区和输出缓冲区的大小都是512
COMMTIMEOUTS TimeOuts;
//设定读超时
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
//在读一次输入缓冲区的内容后读操作就立即返回,
//而不管是否读入了要求的字符。
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier=100;
TimeOuts.WriteTotalTimeoutConstant=500;
SetCommTimeouts(hCom,&TimeOuts); //设置超时
GetCommState(hCom,&dcb);
dcb.BaudRate=9600; //波特率为9600
dcb.ByteSize=8; //每个字节有8位
dcb.Parity=NOPARITY; //无奇偶校验位
dcb.StopBits=ONESTOPBIT; //两个停止位
dcb.fOutxCtsFlow = TRUE; //串行端口的输出由CTS线控制
dcb.fOutxDsrFlow = FALSE; //关闭串行端口的DSR流控制
dcb.fDtrControl = DTR_CONTROL_ENABLE; //启用DTR线
//dcb.fDsrSensitivity = FALSE; //如果设为TRUE将忽略任何输入的字节,除非DSR线被启用
dcb.fTXContinueOnXoff = FALSE; //当为TRUE时,如果接收缓冲区已满且驱动程序已传送XOFF字符,将使驱动程序停止传输字符
dcb.fTXContinueOnXoff = FALSE;
dcb.fOutX = FALSE; //设为TRUE指定XON/XOFF控制被用于控制串行输出
dcb.fInX = FALSE; //设为TRUE指定XON/XOFF控制被用于控制串行输入
dcb.fErrorChar = FALSE; //WINCE串行驱动程序的默认执行将忽略这个字段
dcb.fNull = FALSE; //设为TRUE将使串行驱动程序忽略收到的空字节
dcb.fRtsControl = RTS_CONTROL_ENABLE; //启用RTS线
SetCommState(hCom,&dcb);
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
return TRUE;
}
void CArgoxtestDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
SendMessage(WM_Close,0,0);
}
DWORD ReadThread(PVOID pArg)
{
HWND hWnd;
INT cBytes, i;
BYTE szText[256], *pPtr;
TCHAR tch;
hWnd = (HWND)pArg;
while (1) {
tch = 0;
pPtr = szText;
for (i = 0; i < sizeof (szText)-sizeof (TCHAR); i++) {
while (!ReadFile (hCom, pPtr, 1, &cBytes, 0))
if (hCom == INVALID_HANDLE_VALUE)
return 0;
// This syncs the proper byte order for Unicode.
tch = (tch << 8) & 0xff00;
tch |= *pPtr++;
if (tch == TEXT ('\n'))
break;
}
*pPtr++ = 0; // Avoid alignment probs by addressing as bytes.
*pPtr++ = 0;
// If out of byte sync, move bytes down one.
if (i % 2) {
pPtr = szText;
while (*pPtr || *(pPtr+1)) {
*pPtr = *(pPtr+1);
pPtr++;
}
*pPtr = 0;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -