📄 nettocommdlg.cpp
字号:
/******************************************************************************
* 功能:检测网口是否有命令发送过来,如果有就将该命令转化成相应的阅读器串口命令,
* 然后发送给阅读器,然后在读取阅读器的响应数据,将响应数据再通过网口发送出去
******************************************************************************/
#include "stdafx.h"
#include "NetToComm.h"
#include "NetToCommDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CNetToCommDlg dialog
CNetToCommDlg::CNetToCommDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNetToCommDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNetToCommDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hCommDll = NULL;
m_hCRCDll = NULL;
m_iTag = 0;
m_bThreadFlag = FALSE;
}
CNetToCommDlg::~CNetToCommDlg()
{
if (m_hCommDll != NULL)
{
typedef void (*CLOSE)(void);
CLOSE ComClose = (CLOSE)GetProcAddress(m_hCommDll, _T("ComClose"));
ComClose();
FreeLibrary(m_hCommDll);
m_hCommDll = NULL;
}
if (m_hCRCDll != NULL)
{
FreeLibrary(m_hCRCDll);
m_hCRCDll = NULL;
}
}
void CNetToCommDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNetToCommDlg)
DDX_Control(pDX, IDC_LISTSEND, m_listSend);
DDX_Control(pDX, IDC_LISTRECV, m_listRecv);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNetToCommDlg, CDialog)
//{{AFX_MSG_MAP(CNetToCommDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDEXIT, OnExit)
ON_BN_CLICKED(IDC_READ, OnRead)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CLIENT_READ, ClientRead)
ON_MESSAGE(WM_CLIENT_ACCEPT, ClientAccept)
END_MESSAGE_MAP()
BOOL CNetToCommDlg::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
m_hCommDll = LoadLibrary(_T("./ComDll.dll"));
m_hCRCDll = LoadLibrary(_T("./CRCDll.dll"));
BOOL bRet;
typedef BOOL (*OPENCOM)(LPCTSTR);
OPENCOM ComOpen = (OPENCOM)GetProcAddress(m_hCommDll, _T("ComOpen"));
bRet = ComOpen(_T("COM1"));
if (FALSE == bRet)
{
AfxMessageBox("打开串口失败");
return FALSE;
}
StartListen();
/*
CString strTemp = "00ff33dssee";
for (int i = 0; i < 8; i++)
{
m_listRecv.AddString(strTemp);
m_listSend.AddString(strTemp);
}
*/
return TRUE; // return TRUE unless you set the focus to a control
}
void CNetToCommDlg::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 CNetToCommDlg::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 CNetToCommDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CNetToCommDlg::OnCancel()
{
CDialog::OnCancel();
}
void CNetToCommDlg::OnExit()
{
int nRead = 0, nWrite = 0;
BYTE8 aucWrite[8] = {0xFF, 0x40};
// 往串口发送指令
nWrite = WriteData(aucWrite, 2);
if (-1 == nWrite)
{
TRACE(_T("发送关闭指令失败"));
}
}
void CNetToCommDlg::OnRead()
{
int nWrite = 0;
BYTE8 aucWrite[8] = {0xFF, 0x41, 0x01, 0x88, 0x13, 0x00, 0x00, 0x03};
// 往串口发送指令
nWrite = WriteData(aucWrite, 8);
if (-1 == nWrite)
{
return;
}
if (FALSE == m_bThreadFlag)
{
AfxBeginThread(ReadThread, this);
}
}
int CNetToCommDlg::GetWriteLen(BYTE8* pucInput, int iLen)
{
int iNewLen = iLen;
for (int i = 0; i < iLen; i++)
{
if (pucInput[i] == 0x7e || pucInput[i] == 0x7d)
{
iNewLen++;
}
}
return iNewLen;
}
int CNetToCommDlg::GetReadLen(BYTE8* pucInput, int iLen)
{
int iNewLen = iLen;
for (int i = 0; i < iLen; i++)
{
if ((pucInput[i] == 0x7d) && (pucInput[i + 1] == 0x5e))
{
iNewLen--;
}
else if ((pucInput[i] == 0x7d) && (pucInput[i + 1] == 0x5d))
{
iNewLen--;
}
}
return iNewLen;
}
int CNetToCommDlg::CheckWriteData(BYTE8* pucInput, BYTE8* pucOutput, int iLen)
{
int iNewLen = 0;
for (int i = 0; i < iLen; i++)
{
if (pucInput[i] == 0x7e)
{
pucOutput[iNewLen++] = 0x7d;
pucOutput[iNewLen++] = 0x5e;
}
else if (pucInput[i] == 0x7d)
{
pucOutput[iNewLen++] = 0x7d;
pucOutput[iNewLen++] = 0x5d;
}
else
{
pucOutput[iNewLen++] = pucInput[i];
}
}
return iNewLen;
}
int CNetToCommDlg::CheckReadData(BYTE8* pucInput, BYTE8* pucOutput, int iLen)
{
int iNewLen = 0;
for (int i = 0; i < iLen; i++)
{
if ((pucInput[i] == 0x7d) && (pucInput[i + 1] == 0x5e))
{
pucOutput[iNewLen++] = 0x7e;
i++;
}
else if ((pucInput[i] == 0x7d) && (pucInput[i + 1] == 0x5d))
{
pucOutput[iNewLen++] = 0x7d;
i++;
}
else
{
pucOutput[iNewLen++] = pucInput[i];
}
}
return iNewLen;
}
// 向串口写数据,也即是写入命令码
int CNetToCommDlg::WriteData(BYTE8* pucWriteData, int iLen)
{
// 检测命令码里是否有需要替换的字符
int iNewLen = GetWriteLen(pucWriteData, iLen);
BYTE8* pucCRCData = new BYTE8[iNewLen + 2];
if (NULL == pucCRCData)
{
TRACE("new variable in WriteData function failed\n");
return -1;
}
memset(pucCRCData, 0, sizeof(BYTE8)*(iNewLen + 2));
// 如果没有要替换的字符
if (iNewLen == iLen)
{
for (int i = 0; i < iNewLen; i++)
{
pucCRCData[i] = pucWriteData[i];
}
}
else
{
CheckWriteData(pucWriteData, pucCRCData, iLen);
}
//进行CRC效验
typedef char (*CRCCREATE)(BYTE8*, int);
CRCCREATE CRCCreate = (CRCCREATE)GetProcAddress(m_hCRCDll, _T("CRCCreate"));
CRCCreate(pucCRCData, (iNewLen + 2)); //////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -