|
// McDemoDlg.cpp : ʵÏÖÎļþ
|
//
|
|
#include "stdafx.h"
|
#include "McDemo.h"
|
#include "McDemoDlg.h"
|
#include "afxdialogex.h"
|
|
#ifdef _DEBUG
|
#define new DEBUG_NEW
|
#endif
|
|
|
#define MAX_MESSAGE_LENGTH 1024
|
#define CHANNEL_NAME _T("Test1")
|
|
// ÓÃÓÚÓ¦ÓóÌÐò¡°¹ØÓÚ¡±²Ëµ¥ÏîµÄ CAboutDlg ¶Ô»°¿ò
|
|
class CAboutDlg : public CDialogEx
|
{
|
public:
|
CAboutDlg();
|
|
// ¶Ô»°¿òÊý¾Ý
|
#ifdef AFX_DESIGN_TIME
|
enum { IDD = IDD_ABOUTBOX };
|
#endif
|
|
protected:
|
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV Ö§³Ö
|
|
// ʵÏÖ
|
protected:
|
DECLARE_MESSAGE_MAP()
|
};
|
|
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
|
{
|
}
|
|
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
|
{
|
CDialogEx::DoDataExchange(pDX);
|
}
|
|
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
|
END_MESSAGE_MAP()
|
|
|
// CMcDemoDlg ¶Ô»°¿ò
|
|
|
|
CMcDemoDlg::CMcDemoDlg(CWnd* pParent /*=NULL*/)
|
: CDialogEx(IDD_MCDEMO_DIALOG, pParent)
|
{
|
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
|
m_pChannel = NULL;
|
}
|
|
void CMcDemoDlg::DoDataExchange(CDataExchange* pDX)
|
{
|
CDialogEx::DoDataExchange(pDX);
|
}
|
|
BEGIN_MESSAGE_MAP(CMcDemoDlg, CDialogEx)
|
ON_WM_SYSCOMMAND()
|
ON_WM_PAINT()
|
ON_WM_QUERYDRAGICON()
|
ON_BN_CLICKED(IDC_BUTTON_CREATE, &CMcDemoDlg::OnBnClickedButtonCreate)
|
ON_BN_CLICKED(IDC_BUTTON_SEND, &CMcDemoDlg::OnBnClickedButtonSend)
|
ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CMcDemoDlg::OnBnClickedButtonClose)
|
ON_BN_CLICKED(IDC_BUTTON_DESTROY, &CMcDemoDlg::OnBnClickedButtonDestroy)
|
ON_BN_CLICKED(IDC_BUTTON_READ, &CMcDemoDlg::OnBnClickedButtonRead)
|
ON_BN_CLICKED(IDC_BUTTON_RAWDATA, &CMcDemoDlg::OnBnClickedButtonRawdata)
|
ON_BN_CLICKED(IDC_BUTTON_WRITE, &CMcDemoDlg::OnBnClickedButtonWrite)
|
ON_BN_CLICKED(IDC_BUTTON_READ_BIT, &CMcDemoDlg::OnBnClickedButtonReadBit)
|
ON_BN_CLICKED(IDC_CHECK_WRITE_BIT_VALUE, &CMcDemoDlg::OnBnClickedCheckWriteBitValue)
|
ON_BN_CLICKED(IDC_BUTTON_WRITE_BIT, &CMcDemoDlg::OnBnClickedButtonWriteBit)
|
ON_BN_CLICKED(IDC_BUTTON_READ_DWORD, &CMcDemoDlg::OnBnClickedButtonReadDword)
|
ON_BN_CLICKED(IDC_BUTTON_WRITE_DWORD, &CMcDemoDlg::OnBnClickedButtonWriteDword)
|
ON_WM_TIMER()
|
END_MESSAGE_MAP()
|
|
|
// CMcDemoDlg ÏûÏ¢´¦Àí³ÌÐò
|
|
BOOL CMcDemoDlg::OnInitDialog()
|
{
|
CDialogEx::OnInitDialog();
|
|
// ½«¡°¹ØÓÚ...¡±²Ëµ¥ÏîÌí¼Óµ½ÏµÍ³²Ëµ¥ÖС£
|
|
// IDM_ABOUTBOX ±ØÐëÔÚϵͳÃüÁΧÄÚ¡£
|
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
|
ASSERT(IDM_ABOUTBOX < 0xF000);
|
|
CMenu* pSysMenu = GetSystemMenu(FALSE);
|
if (pSysMenu != NULL)
|
{
|
BOOL bNameValid;
|
CString strAboutMenu;
|
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
|
ASSERT(bNameValid);
|
if (!strAboutMenu.IsEmpty())
|
{
|
pSysMenu->AppendMenu(MF_SEPARATOR);
|
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
|
}
|
}
|
|
// ÉèÖô˶Ի°¿òµÄͼ±ê¡£ µ±Ó¦ÓóÌÐòÖ÷´°¿Ú²»ÊǶԻ°¿òʱ£¬¿ò¼Ü½«×Ô¶¯
|
// Ö´Ðд˲Ù×÷
|
SetIcon(m_hIcon, TRUE); // ÉèÖôóͼ±ê
|
SetIcon(m_hIcon, FALSE); // ÉèÖÃСͼ±ê
|
|
// TODO: ÔÚ´ËÌí¼Ó¶îÍâµÄ³õʼ»¯´úÂë
|
|
|
CIPAddressCtrl* pIpAddressCtrl = (CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS_SERVER);
|
pIpAddressCtrl->SetAddress(192, 168, 90, 17);
|
// pIpAddressCtrl->SetAddress(127, 0, 0, 1);
|
SetDlgItemInt(IDC_EDIT_SERVER_PORT, 8080);
|
|
((CButton*)GetDlgItem(IDC_RADIO2))->SetCheck(1);
|
//CString strText = _T("50 00 00 FF FF 03 00 0C 00 10 00 01 04 00 00 64 00 00 A8 01 00");
|
//SetDlgItemText(IDC_EDIT_RAW, strText);
|
|
|
// ÈíÔª¼þ
|
std::vector<std::string> softComponents;
|
int softComponentsSize = MCL_GetSoftComponents(softComponents);
|
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM);
|
CComboBox* pComboBox2 = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM2);
|
for (auto item : softComponents) {
|
pComboBox->InsertString(pComboBox->GetCount(), item.c_str());
|
pComboBox2->InsertString(pComboBox2->GetCount(), item.c_str());
|
}
|
if (softComponentsSize > 0) {
|
pComboBox->SetCurSel(0);
|
pComboBox2->SetCurSel(0);
|
}
|
|
|
// ÃüÁî
|
std::vector<std::string> cmds;
|
int cmdsSize = MCL_GetCmds(cmds);
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_CMD);
|
for (auto item : cmds) {
|
pComboBox->InsertString(pComboBox->GetCount(), item.c_str());
|
}
|
if (cmdsSize > 0) {
|
pComboBox->SetCurSel(0);
|
}
|
|
|
|
// ×ÓÃüÁî
|
std::vector<std::string> subCmds;
|
int subCmdsSize = MCL_GetSubCmds(subCmds);
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SUBCMD);
|
for (auto item : subCmds) {
|
pComboBox->InsertString(pComboBox->GetCount(), item.c_str());
|
}
|
if (cmdsSize > 0) {
|
pComboBox->SetCurSel(0);
|
}
|
|
|
SetDlgItemInt(IDC_EDIT_HEAD_ADDR, 100);
|
SetDlgItemInt(IDC_EDIT_READ_COUNT, 1);
|
UpdateUISecsion1();
|
UpdateUISecsion2();
|
UpdateUISecsion3();
|
|
return TRUE; // ³ý·Ç½«½¹µãÉèÖõ½¿Ø¼þ£¬·ñÔò·µ»Ø TRUE
|
}
|
|
void CMcDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
|
{
|
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
|
{
|
CAboutDlg dlgAbout;
|
dlgAbout.DoModal();
|
}
|
else
|
{
|
CDialogEx::OnSysCommand(nID, lParam);
|
}
|
}
|
|
// Èç¹ûÏò¶Ô»°¿òÌí¼Ó×îС»¯°´Å¥£¬ÔòÐèÒªÏÂÃæµÄ´úÂë
|
// À´»æÖƸÃͼ±ê¡£ ¶ÔÓÚʹÓÃÎĵµ/ÊÓͼģÐ굀 MFC Ó¦ÓóÌÐò£¬
|
// Õ⽫ÓÉ¿ò¼Ü×Ô¶¯Íê³É¡£
|
|
void CMcDemoDlg::OnPaint()
|
{
|
if (IsIconic())
|
{
|
CPaintDC dc(this); // ÓÃÓÚ»æÖƵÄÉ豸ÉÏÏÂÎÄ
|
|
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
|
|
// ʹͼ±êÔÚ¹¤×÷Çø¾ØÐÎÖоÓÖÐ
|
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;
|
|
// »æÖÆÍ¼±ê
|
dc.DrawIcon(x, y, m_hIcon);
|
}
|
else
|
{
|
CDialogEx::OnPaint();
|
}
|
}
|
|
//µ±Óû§Í϶¯×îС»¯´°¿Úʱϵͳµ÷Óô˺¯ÊýÈ¡µÃ¹â±ê
|
//ÏÔʾ¡£
|
HCURSOR CMcDemoDlg::OnQueryDragIcon()
|
{
|
return static_cast<HCURSOR>(m_hIcon);
|
}
|
|
|
|
void CMcDemoDlg::OnBnClickedButtonCreate()
|
{
|
m_mcChannellistener.funOnConnected = [&](IMcChannel* pChannel, int nErrorCode) -> void {
|
CString strText;
|
strText.Format(_T(">> funOnConnected, %sÁ¬½Ó½á¹û<code = %d>\r\n"), pChannel->getName().c_str(), nErrorCode);
|
AppendLog(strText);
|
|
UpdateUISecsion1();
|
UpdateUISecsion2();
|
UpdateUISecsion3();
|
};
|
m_mcChannellistener.funOnClose = [&](IMcChannel* pChannel) -> void {
|
AppendLog(">> funOnClose, %sÒѹرÕ\r\n", pChannel->getName().c_str());
|
UpdateUISecsion1();
|
UpdateUISecsion2();
|
UpdateUISecsion3();
|
};
|
m_mcChannellistener.funOnClosing = [&](IMcChannel* pChannel) -> void {
|
AppendLog(">> funOnClosing, ÕýÔÚ¹Ø±Õ %s\r\n", pChannel->getName().c_str());
|
UpdateUISecsion1();
|
UpdateUISecsion2();
|
UpdateUISecsion3();
|
};
|
m_mcChannellistener.funOnRead = [&](IMcChannel* pChannel, char* pData, unsigned int nDataSize, int nDecodeRet) -> void {
|
CString strText;
|
DataToHexString(pData, nDataSize, strText);
|
AppendLog(">> funOnRead:[%s]\r\n", strText);
|
};
|
m_mcChannellistener.funOnWrite = [&](IMcChannel* pChannel) -> void {
|
AppendLog(">> funOnWrite\r\n");
|
};
|
|
|
CIPAddressCtrl* pIpAddressCtrl = (CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS_SERVER);
|
BYTE b1, b2, b3, b4;
|
pIpAddressCtrl->GetAddress(b1, b2, b3, b4);
|
CString strIP;
|
strIP.Format("%d.%d.%d.%d", b1, b2, b3, b4);
|
UINT port = GetDlgItemInt(IDC_EDIT_SERVER_PORT);
|
|
|
m_pChannel = NULL;
|
if (0 == MCL_CreateChannel(m_pChannel, CHANNEL_NAME, (LPTSTR)(LPCTSTR)strIP, port, 0) && m_pChannel != NULL) {
|
m_pChannel->setChannelListener(&m_mcChannellistener);
|
CString strText;
|
strText.Format(_T(">> ´´½¨Í¨µÀ(%s)³É¹¦\r\n"), m_pChannel->getName().c_str()),
|
AppendLog(strText);
|
//m_pChannel->connect();
|
}
|
else if(m_pChannel != NULL){
|
m_pChannel->setChannelListener(&m_mcChannellistener);
|
AppendLog(">> ÕÒµ½ÏÖÓÐͨµÀ(%s).\r\n", m_pChannel->getName().c_str());
|
}
|
|
UpdateUISecsion1();
|
UpdateUISecsion2();
|
UpdateUISecsion3();
|
}
|
|
void CMcDemoDlg::OnBnClickedButtonSend()
|
{
|
|
if (m_pChannel != NULL && m_pChannel->isConnected()) {
|
CString strText;
|
GetDlgItemText(IDC_EDIT_RAW, strText);
|
CButton* pButton = (CButton*)GetDlgItem(IDC_RADIO2);
|
if (pButton->GetCheck() == 1) {
|
char szBuffer[1024];
|
int nLen = HexStringToData(strText, szBuffer, 1024);
|
if (nLen > 0) {
|
m_pChannel->sendRawData(szBuffer, nLen);
|
}
|
}
|
else {
|
m_pChannel->sendRawData((LPTSTR)(LPCTSTR)strText, strText.GetLength());
|
}
|
}
|
}
|
|
void CMcDemoDlg::OnBnClickedButtonClose()
|
{
|
if (m_pChannel != NULL) {
|
if (m_pChannel->isConnected()) {
|
m_pChannel->close();
|
}
|
else {
|
AppendLog(_T(">> ÕýÔÚÁ¬½Ó %s, %s:%d\r\n"), m_pChannel->getName().c_str(), m_pChannel->getIp().c_str(), m_pChannel->getServerPort());
|
m_pChannel->connect();
|
}
|
|
UpdateUISecsion1();
|
UpdateUISecsion2();
|
UpdateUISecsion3();
|
}
|
|
/*
|
IMcChannel* pChannel = NULL;
|
if (0 == MCL_GetChannel(pChannel, CHANNEL_NAME) && pChannel != NULL) {
|
pChannel->close();
|
}*/
|
}
|
|
|
void CMcDemoDlg::OnBnClickedButtonDestroy()
|
{
|
if (m_pChannel != NULL) {
|
MCL_DestroyChannel(m_pChannel->getName().c_str());
|
m_pChannel = NULL;
|
UpdateUISecsion1();
|
UpdateUISecsion2();
|
UpdateUISecsion3();
|
}
|
}
|
|
void CMcDemoDlg::AppendLog(CString strText)
|
{
|
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT_LOG);
|
if (pEdit == NULL) {
|
return;
|
}
|
|
|
// ³¬¹ýÖ¸¶¨ÐÐÔòɾ³ý×îÇ°ÃæÒ»ÐÐ
|
int nLine = pEdit->GetLineCount();
|
if (nLine > 100) {
|
CString strOldText;
|
pEdit->GetWindowText(strOldText);
|
|
int nIndex = strOldText.Find(_T("\r\n"), 0);
|
for (int i = 0; i < 1111; i++) {
|
nIndex = strOldText.Find(_T("\r\n"), nIndex + 2);
|
}
|
pEdit->SetSel(0, nIndex);
|
pEdit->ReplaceSel(_T(""));
|
}
|
|
|
// ×·¼Óµ½×îºó
|
int length = pEdit->GetWindowTextLength();
|
pEdit->SetSel(length, length);
|
pEdit->ReplaceSel(strText);
|
pEdit->PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
|
}
|
|
void CMcDemoDlg::AppendLog(char* szMessage, ...)
|
{
|
if (!::IsWindow(GetSafeHwnd())) {
|
return;
|
}
|
|
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT_LOG);
|
if (pEdit == NULL) {
|
return;
|
}
|
|
char szFormatMessage[MAX_MESSAGE_LENGTH];
|
va_list ap;
|
va_start(ap, szMessage);
|
_vsnprintf_s(szFormatMessage, MAX_MESSAGE_LENGTH, szMessage, ap);
|
va_end(ap);
|
|
// ³¬¹ýÖ¸¶¨ÐÐÔòɾ³ý×îÇ°ÃæÒ»ÐÐ
|
int nLine = pEdit->GetLineCount();
|
if (nLine > 100) {
|
CString strOldText;
|
pEdit->GetWindowText(strOldText);
|
|
int nIndex = strOldText.Find(_T("\r\n"), 0);
|
for (int i = 0; i < 1111; i++) {
|
nIndex = strOldText.Find(_T("\r\n"), nIndex + 2);
|
}
|
pEdit->SetSel(0, nIndex);
|
pEdit->ReplaceSel(_T(""));
|
}
|
|
|
// ×·¼Óµ½×îºó
|
int length = pEdit->GetWindowTextLength();
|
pEdit->SetSel(length, length);
|
pEdit->ReplaceSel(szFormatMessage);
|
pEdit->PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
|
}
|
|
void CMcDemoDlg::DataToHexString(const char* pData, const int size, CString& strOut)
|
{
|
strOut.Empty();
|
for (int i = 0; i < size; i++) {
|
if (i < size - 1) {
|
strOut.AppendFormat(_T("%02X "), (BYTE)pData[i]);
|
}
|
else {
|
strOut.AppendFormat(_T("%02X"), (BYTE)pData[i]);
|
}
|
}
|
}
|
|
int CMcDemoDlg::HexStringToData(CString& strHex, char* pszBuffer, const int nBufferSize)
|
{
|
int index = 0;
|
int nLength = strHex.GetLength();
|
CString strItem;
|
for (int i = 0; i < nLength; i++) {
|
if (strHex.GetAt(i) == ' ') {
|
if (!strItem.IsEmpty()) {
|
BYTE b = (BYTE)strtol(strItem, NULL, 16);
|
pszBuffer[index] = b;
|
index++;
|
if (index >= nBufferSize) {
|
break;
|
}
|
strItem.Empty();
|
}
|
continue;
|
}
|
|
strItem.AppendChar(strHex.GetAt(i));
|
if (strItem.GetLength() == 2) {
|
BYTE b = (BYTE)strtol(strItem, NULL, 16);
|
pszBuffer[index] = b;
|
index++;
|
if (index >= nBufferSize) {
|
break;
|
}
|
strItem.Empty();
|
}
|
}
|
|
return index;
|
}
|
|
void CMcDemoDlg::UpdateUISecsion1()
|
{
|
if (!::IsWindow(GetSafeHwnd())) {
|
return;
|
}
|
|
GetDlgItem(IDC_BUTTON_CREATE)->EnableWindow(m_pChannel == NULL);
|
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(m_pChannel != NULL);
|
SetDlgItemText(IDC_BUTTON_CLOSE, (m_pChannel != NULL && m_pChannel->isConnected()) ? _T("Close") : _T("Open"));
|
GetDlgItem(IDC_BUTTON_DESTROY)->EnableWindow(m_pChannel != NULL);
|
}
|
|
void CMcDemoDlg::UpdateUISecsion2()
|
{
|
if (!::IsWindow(GetSafeHwnd())) {
|
return;
|
}
|
|
BOOL bEnable = (m_pChannel != NULL && m_pChannel->isConnected());
|
GetDlgItem(IDC_RADIO1)->EnableWindow(bEnable);
|
GetDlgItem(IDC_RADIO2)->EnableWindow(bEnable);
|
GetDlgItem(IDC_EDIT_RAW)->EnableWindow(bEnable);
|
GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(bEnable);
|
GetDlgItem(IDC_COMBO_CMD)->EnableWindow(bEnable);
|
GetDlgItem(IDC_COMBO_SUBCMD)->EnableWindow(bEnable);
|
GetDlgItem(IDC_COMBO_SOFTITEM)->EnableWindow(bEnable);
|
GetDlgItem(IDC_EDIT_HEAD_ADDR)->EnableWindow(bEnable);
|
GetDlgItem(IDC_EDIT_READ_COUNT)->EnableWindow(bEnable);
|
GetDlgItem(IDC_BUTTON_RAWDATA)->EnableWindow(bEnable);
|
}
|
|
void CMcDemoDlg::UpdateUISecsion3()
|
{
|
if (!::IsWindow(GetSafeHwnd())) {
|
return;
|
}
|
|
GetDlgItem(IDC_COMBO_SOFTITEM2)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
GetDlgItem(IDC_EDIT_HEAD_ADDR2)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
GetDlgItem(IDC_BUTTON_READ_DWORD)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
GetDlgItem(IDC_BUTTON_READ)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
GetDlgItem(IDC_BUTTON_READ_BIT)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
GetDlgItem(IDC_EDIT_WRITE_VALUE)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
GetDlgItem(IDC_CHECK_WRITE_BIT_VALUE)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
GetDlgItem(IDC_BUTTON_WRITE_DWORD)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
GetDlgItem(IDC_BUTTON_WRITE)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
GetDlgItem(IDC_BUTTON_WRITE_BIT)->EnableWindow((m_pChannel != NULL && m_pChannel->isConnected()));
|
}
|
|
void CMcDemoDlg::OnBnClickedButtonRawdata()
|
{
|
/*
|
char szBuffer[] = {0x50, 0x00, 0x00, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x00, 0x10, 0x00, 0x01,
|
0x04, 0x00, 0x00, 0x64, 0x00, 0x00, 0xa8, 0x01, 0x00};
|
|
|
// ÈíÔªÀàÐÍ
|
static char szSoftItem[] = {0x90, 0xa8, 0x9c, 0x9d, 0xb0};
|
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM);
|
szBuffer[18] = szSoftItem[pComboBox->GetCurSel()];
|
|
|
// Ê×µØÖ·
|
int nHeadAddr = GetDlgItemInt(IDC_EDIT_HEAD_ADDR);
|
szBuffer[15] = nHeadAddr & 0xff;
|
szBuffer[16] = (nHeadAddr & 0xff00) >> 8;
|
szBuffer[17] = (nHeadAddr & 0xff0000) >> 16;
|
|
|
// ÊýÁ¿
|
int nReadCount = GetDlgItemInt(IDC_EDIT_READ_COUNT);
|
szBuffer[19] = nReadCount & 0xff;
|
szBuffer[20] = (nReadCount & 0xff00) >> 8;
|
|
|
// ÃüÁî
|
int nCmd = ((CComboBox*)GetDlgItem(IDC_COMBO_CMD))->GetCurSel();
|
if (nCmd == 0) {
|
// ³ÉÅú¶Á
|
szBuffer[11] = 0x01;
|
szBuffer[12] = 0x04;
|
}
|
else if (nCmd == 1) {
|
// ³ÉÅúд
|
szBuffer[11] = 0x01;
|
szBuffer[12] = 0x14;
|
}
|
else if (nCmd == 2) {
|
// Ëæ»ú¶Á
|
szBuffer[11] = 0x03;
|
szBuffer[12] = 0x04;
|
}
|
else if (nCmd == 3) {
|
// ²âÊÔ(Ëæ»úд)
|
szBuffer[11] = 0x02;
|
szBuffer[12] = 0x14;
|
}
|
else if (nCmd == 4) {
|
// ¼àÊӵǼÊý¾Ý
|
szBuffer[11] = 0x01;
|
szBuffer[12] = 0x08;
|
}
|
else if (nCmd == 5) {
|
// ¼àÊÓ
|
szBuffer[11] = 0x02;
|
szBuffer[12] = 0x08;
|
}
|
|
|
// ×ÓÃüÁî
|
int nSubCmd = ((CComboBox*)GetDlgItem(IDC_COMBO_SUBCMD))->GetCurSel();
|
if (nSubCmd == 0) {
|
// °´×Ö
|
szBuffer[13] = 0x00;
|
szBuffer[14] = 0x00;
|
}
|
else if (nSubCmd == 1) {
|
// °´Î»
|
szBuffer[13] = 0x00;
|
szBuffer[14] = 0x01;
|
}
|
|
|
|
|
CString strRawData;
|
DataToHexString(szBuffer, sizeof(szBuffer), strRawData);
|
SetDlgItemText(IDC_EDIT_RAW, strRawData);
|
*/
|
|
|
// з½·¨
|
CComboBox* pComboBox = NULL;
|
CString strLbText;
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_CMD);
|
pComboBox->GetLBText(pComboBox->GetCurSel(), strLbText);
|
std::string strCmd = (LPTSTR)(LPCTSTR)strLbText;
|
MC::CMD cmd;
|
MCL_GetCmd(strCmd, cmd);
|
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SUBCMD);
|
pComboBox->GetLBText(pComboBox->GetCurSel(), strLbText);
|
std::string strSubCmd = (LPTSTR)(LPCTSTR)strLbText;
|
MC::SUBCMD subCmd;
|
MCL_GetSubCmd(strSubCmd, subCmd);
|
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM);
|
pComboBox->GetLBText(pComboBox->GetCurSel(), strLbText);
|
std::string strSoftComponent = (LPTSTR)(LPCTSTR)strLbText;
|
MC::SOFT_COMPONENT softComponent;
|
MCL_GetSoftComponent(strSoftComponent, softComponent);
|
|
int nFirstAddr = GetDlgItemInt(IDC_EDIT_HEAD_ADDR);
|
int nOperateCount = GetDlgItemInt(IDC_EDIT_READ_COUNT);
|
char szRawData[1024];
|
char szAttach[4] = { 0x10, 0x11, 0x12, 0x13 };
|
int nDataSize = MCL_GetRawData(cmd, subCmd, softComponent, nFirstAddr, nOperateCount, NULL, 0, szRawData, sizeof(szRawData));
|
|
CString strRawData2;
|
DataToHexString(szRawData, nDataSize, strRawData2);
|
SetDlgItemText(IDC_EDIT_RAW, strRawData2);
|
// AfxMessageBox(strRawData2);
|
}
|
|
void CMcDemoDlg::OnBnClickedButtonRead()
|
{
|
CComboBox* pComboBox;
|
CString strLbText;
|
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM2);
|
pComboBox->GetLBText(pComboBox->GetCurSel(), strLbText);
|
std::string strSoftComponent = (LPTSTR)(LPCTSTR)strLbText;
|
MC::SOFT_COMPONENT softComponent;
|
MCL_GetSoftComponent(strSoftComponent, softComponent);
|
|
int nAddr = GetDlgItemInt(IDC_EDIT_HEAD_ADDR2);
|
|
if (m_pChannel != NULL && m_pChannel->isConnected()) {
|
auto funOnRead = [&](IMcChannel* pChannel, int addr, DWORD value, int flag) -> void {
|
AppendLog(">> funOnRead:[%d]\r\n", value);
|
CString strText;
|
strText.Format(_T("Read Word(%d)"), value);
|
SetDlgItemText(IDC_BUTTON_READ, strText);
|
};
|
m_pChannel->readWord(softComponent, nAddr, funOnRead);
|
}
|
}
|
|
|
void CMcDemoDlg::OnBnClickedButtonReadBit()
|
{
|
CComboBox* pComboBox;
|
CString strLbText;
|
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM2);
|
pComboBox->GetLBText(pComboBox->GetCurSel(), strLbText);
|
std::string strSoftComponent = (LPTSTR)(LPCTSTR)strLbText;
|
MC::SOFT_COMPONENT softComponent;
|
MCL_GetSoftComponent(strSoftComponent, softComponent);
|
|
int nAddr = GetDlgItemInt(IDC_EDIT_HEAD_ADDR2);
|
|
if (m_pChannel != NULL && m_pChannel->isConnected()) {
|
auto funOnRead = [&](IMcChannel* pChannel, int addr, DWORD value, int flag) -> void {
|
AppendLog(">> funOnRead:[%s]\r\n", value != 0 ? "ON" : "OFF");
|
CString strText;
|
strText.Format(_T("Read Bit(%s)"), value != 0 ? "ON" : "OFF");
|
SetDlgItemText(IDC_BUTTON_READ_BIT, strText);
|
TRACE("MC Read Bit(%s)\n", value != 0 ? "ON" : "OFF");
|
};
|
m_pChannel->readBit(softComponent, nAddr, funOnRead);
|
}
|
}
|
|
void CMcDemoDlg::OnBnClickedButtonWrite()
|
{
|
CComboBox* pComboBox;
|
CString strLbText;
|
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM2);
|
pComboBox->GetLBText(pComboBox->GetCurSel(), strLbText);
|
std::string strSoftComponent = (LPTSTR)(LPCTSTR)strLbText;
|
MC::SOFT_COMPONENT softComponent;
|
MCL_GetSoftComponent(strSoftComponent, softComponent);
|
|
int nAddr = GetDlgItemInt(IDC_EDIT_HEAD_ADDR2);
|
int nValue = GetDlgItemInt(IDC_EDIT_WRITE_VALUE);
|
|
if (m_pChannel != NULL && m_pChannel->isConnected()) {
|
auto funOnWrite = [&](IMcChannel* pChannel, int addr, DWORD value, int flag) -> void {
|
AppendLog(">> funOnWrite:[%d]\r\n", value);
|
};
|
m_pChannel->writeWord(softComponent, nAddr, nValue, funOnWrite);
|
}
|
}
|
|
void CMcDemoDlg::OnBnClickedButtonReadDword()
|
{
|
CComboBox* pComboBox;
|
CString strLbText;
|
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM2);
|
pComboBox->GetLBText(pComboBox->GetCurSel(), strLbText);
|
std::string strSoftComponent = (LPTSTR)(LPCTSTR)strLbText;
|
MC::SOFT_COMPONENT softComponent;
|
MCL_GetSoftComponent(strSoftComponent, softComponent);
|
|
int nAddr = GetDlgItemInt(IDC_EDIT_HEAD_ADDR2);
|
|
if (m_pChannel != NULL && m_pChannel->isConnected()) {
|
auto funOnRead = [&](IMcChannel* pChannel, int addr, DWORD value, int flag) -> void {
|
AppendLog(">> funOnRead:[%d]\r\n", value);
|
CString strText;
|
strText.Format(_T("Read DWORD(%d)"), value);
|
SetDlgItemText(IDC_BUTTON_READ_DWORD, strText);
|
};
|
m_pChannel->readDWord(softComponent, nAddr, funOnRead);
|
}
|
|
|
// SetTimer(1, 500, NULL);
|
}
|
|
void CMcDemoDlg::OnBnClickedButtonWriteDword()
|
{
|
CComboBox* pComboBox;
|
CString strLbText;
|
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM2);
|
pComboBox->GetLBText(pComboBox->GetCurSel(), strLbText);
|
std::string strSoftComponent = (LPTSTR)(LPCTSTR)strLbText;
|
MC::SOFT_COMPONENT softComponent;
|
MCL_GetSoftComponent(strSoftComponent, softComponent);
|
|
int nAddr = GetDlgItemInt(IDC_EDIT_HEAD_ADDR2);
|
int nValue = GetDlgItemInt(IDC_EDIT_WRITE_VALUE);
|
|
if (m_pChannel != NULL && m_pChannel->isConnected()) {
|
auto funOnWrite = [&](IMcChannel* pChannel, int addr, DWORD value, int flag) -> void {
|
AppendLog(">> funOnWrite:[%d]\r\n", value);
|
};
|
m_pChannel->writeDWord(softComponent, nAddr, nValue, funOnWrite);
|
}
|
}
|
|
void CMcDemoDlg::OnBnClickedButtonWriteBit()
|
{
|
CComboBox* pComboBox;
|
CString strLbText;
|
|
pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_SOFTITEM2);
|
pComboBox->GetLBText(pComboBox->GetCurSel(), strLbText);
|
std::string strSoftComponent = (LPTSTR)(LPCTSTR)strLbText;
|
MC::SOFT_COMPONENT softComponent;
|
MCL_GetSoftComponent(strSoftComponent, softComponent);
|
|
int nAddr = GetDlgItemInt(IDC_EDIT_HEAD_ADDR2);
|
CButton* pButton = (CButton*)GetDlgItem(IDC_CHECK_WRITE_BIT_VALUE);
|
BOOL bValue = pButton->GetCheck();;
|
|
if (m_pChannel != NULL && m_pChannel->isConnected()) {
|
auto funOnWrite = [&](IMcChannel* pChannel, int addr, DWORD value, int flag) -> void {
|
AppendLog(">> funOnWrite:[%s]\r\n", value != 0 ? "ON" : "OFF");
|
};
|
m_pChannel->writeBit(softComponent, nAddr, bValue, funOnWrite);
|
}
|
}
|
|
void CMcDemoDlg::OnBnClickedCheckWriteBitValue()
|
{
|
CButton* pButton = (CButton*)GetDlgItem(IDC_CHECK_WRITE_BIT_VALUE);
|
pButton->SetWindowText(pButton->GetCheck() ? "ON" : "OFF");
|
}
|
|
|
|
void CMcDemoDlg::OnTimer(UINT_PTR nIDEvent)
|
{
|
// TODO: ÔÚ´ËÌí¼ÓÏûÏ¢´¦Àí³ÌÐò´úÂëºÍ/»òµ÷ÓÃĬÈÏÖµ
|
if (1 == nIDEvent) {
|
auto funOnReadData = [&](IMcChannel* pChannel, int addr, char *pData, unsigned int nDataSize, int flag) -> void {
|
CString s = "Packer funOnReadData [";
|
for (int i = 0; i < nDataSize; i++) {
|
s.AppendFormat(" %x", (BYTE)pData[i]);
|
}
|
TRACE(s);
|
};
|
TRACE("Packer readData");
|
m_pChannel->readData(MC::SOFT_COMPONENT::B, 0x2000, 5,
|
funOnReadData);
|
}
|
|
CDialogEx::OnTimer(nIDEvent);
|
}
|