|
// MultiTerminal1Dlg.cpp: 实现文件
|
//
|
|
#include "pch.h"
|
#include "framework.h"
|
#include "MultiTerminal1.h"
|
#include "MultiTerminal1Dlg.h"
|
#include "afxdialogex.h"
|
#include <devguid.h>
|
#include <SetupAPI.h>
|
#include "../MyLib/Functions.hpp"
|
#include "../MyLib/UI_Func/UI_Func.hpp"
|
#include "CAnsiParser.h"
|
#include "CDataParser1.h"
|
#include "KLinkProtocol.h"
|
#include "../MyLib/SerialCom/SerialCom.hpp"
|
#include <Dbt.h>
|
#include "CDialogCommSet1.h"
|
#include "CDialogSysRegSet.h"
|
#include "CDialogStatusShow.h"
|
#include "CDialogInfoDisplay.h"
|
#include "CDialogCommTest.h"
|
|
#include "CDialogCoilMon.h"
|
#include "CDialogDataMon.h"
|
|
#include "CDialogProgress.h"
|
#include "CDialogEventLog.h"
|
#include "CDialogDateTime.h"
|
#include "CDialogForceIO.h"
|
#include "HvSerialPort.h"
|
|
#ifdef _DEBUG
|
#define new DEBUG_NEW
|
#endif
|
|
#pragma comment(lib,"setupapi.lib")
|
|
stCfgValCtrl myCfgValCtrls[] = {
|
"AutoStart", vtbool, &bAutoStart, IDC_CHECK_AUTO_START, ctCheckBox,
|
"LastComPort", vtInt, &nComPort, IDC_COMBO_COMPORT, ctComboBoxIndex,
|
"Baud", vtInt, &nBaud, IDC_COMBO_COM_BAUD, ctComboBoxString,
|
"Ref1", NULL, nullptr, IDC_EDIT_REF1, ctEdit,
|
};
|
|
|
int nMarkConfigs = sizeof(myCfgValCtrls) / sizeof(stCfgValCtrl);
|
|
int nTest1 = 1;
|
float nTest2 = 2;
|
double nTest3 = 3;
|
|
int MonitorTotalCount = 0;
|
int MonitorSuccessCount = 0;
|
int MonitorFailCount = 0;
|
int MonitorSpeed = 0;
|
int LastMonitorTotalCount = 0;
|
|
stCfgValCtrl myCfgTest1[] = {
|
"Test1", vtInt, &nTest1, 0,0,
|
"Test2", vtFloat, &nTest2, 0,0,
|
"Test3", vtDouble, &nTest3, 0,0,
|
};
|
int nCfgTestCount = sizeof(myCfgTest1) / sizeof(stCfgValCtrl);
|
|
unsigned char send1[40000];
|
unsigned char recv1[40000];
|
|
volatile int nDataToSend = 0;
|
|
volatile int BulkSendTest = 0;
|
|
unsigned char SDTbuf[512];
|
unsigned char WYbuf[256];
|
// 用于应用程序“关于”菜单项的 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()
|
|
|
// CMultiTerminal1Dlg 对话框
|
|
|
|
CMultiTerminal1Dlg::CMultiTerminal1Dlg(CWnd* pParent /*=nullptr*/)
|
: CDialogEx(IDD_MULTITERMINAL1_DIALOG, pParent)
|
{
|
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
|
}
|
|
void CMultiTerminal1Dlg::DoDataExchange(CDataExchange* pDX)
|
{
|
CDialogEx::DoDataExchange(pDX);
|
DDX_Control(pDX, IDC_COMBO_COMPORT, m_combo_comport);
|
DDX_Control(pDX, IDC_COMBO_COM_BAUD, m_combo_com_baud);
|
DDX_Control(pDX, IDC_EDIT_DISPLAY, m_edit_display);
|
DDX_Control(pDX, IDC_COMBO_INPUT, m_combo_input);
|
DDX_Control(pDX, IDC_TREE1, m_treectrl1);
|
DDX_Control(pDX, IDC_EDIT_MON, m_edit_mon1);
|
DDX_Control(pDX, IDC_EDIT_MON2, m_edit_mon2);
|
DDX_Control(pDX, IDC_STATIC_CONNECT, m_static_connect);
|
}
|
|
BEGIN_MESSAGE_MAP(CMultiTerminal1Dlg, CDialogEx)
|
ON_WM_SYSCOMMAND()
|
ON_WM_PAINT()
|
ON_WM_QUERYDRAGICON()
|
ON_WM_CLOSE()
|
ON_WM_SIZE()
|
ON_WM_TIMER()
|
ON_BN_CLICKED(IDC_BUTTON_START, &CMultiTerminal1Dlg::OnBnClickedButtonStart)
|
ON_BN_CLICKED(IDC_BUTTON_STOP, &CMultiTerminal1Dlg::OnBnClickedButtonStop)
|
ON_BN_CLICKED(IDC_BUTTON_CLRSCR, &CMultiTerminal1Dlg::OnBnClickedButtonClrscr)
|
ON_BN_CLICKED(IDC_BUTTON_SEND, &CMultiTerminal1Dlg::OnBnClickedButtonSend)
|
ON_CBN_EDITCHANGE(IDC_COMBO_INPUT, &CMultiTerminal1Dlg::OnCbnEditchangeComboInput)
|
ON_CBN_SELCHANGE(IDC_COMBO_RES, &CMultiTerminal1Dlg::OnCbnSelchangeComboRes)
|
ON_BN_CLICKED(IDC_BUTTON_DO, &CMultiTerminal1Dlg::OnBnClickedButtonDo)
|
ON_CBN_SELCHANGE(IDC_COMBO_CMD_TYPE, &CMultiTerminal1Dlg::OnCbnSelchangeComboCmdType)
|
ON_CBN_SELCHANGE(IDC_COMBO_DCOUNT, &CMultiTerminal1Dlg::OnCbnSelchangeComboDcount)
|
ON_BN_CLICKED(IDC_BUTTON_START_BULK_TEST, &CMultiTerminal1Dlg::OnBnClickedButtonStartBulkTest)
|
ON_BN_CLICKED(IDC_BUTTON_STOP_BULK_TEST, &CMultiTerminal1Dlg::OnBnClickedButtonStopBulkTest)
|
ON_WM_DEVICECHANGE()
|
ON_WM_INITMENUPOPUP()
|
ON_COMMAND(ID_MENU_DOWNLOAD, &CMultiTerminal1Dlg::OnMenuDownload)
|
ON_COMMAND(ID_MENU_UPLOAD, &CMultiTerminal1Dlg::OnMenuUpload)
|
ON_COMMAND(ID_MENU_ONLINE, &CMultiTerminal1Dlg::OnMenuOnline)
|
ON_COMMAND(ID_MENU_OFFLINE, &CMultiTerminal1Dlg::OnMenuOffline)
|
ON_COMMAND(ID_MENU_MONITOR, &CMultiTerminal1Dlg::OnMenuMonitor)
|
ON_COMMAND(ID_MENU_RUN, &CMultiTerminal1Dlg::OnMenuRun)
|
ON_UPDATE_COMMAND_UI(ID_MENU_RUN, &CMultiTerminal1Dlg::OnUpdateMenuRun)
|
ON_UPDATE_COMMAND_UI(ID_MENU_MONITOR, &CMultiTerminal1Dlg::OnUpdateMenuMonitor)
|
ON_UPDATE_COMMAND_UI(ID_MENU_OFFLINE, &CMultiTerminal1Dlg::OnUpdateMenuOffline)
|
ON_UPDATE_COMMAND_UI(ID_MENU_ONLINE, &CMultiTerminal1Dlg::OnUpdateMenuOnline)
|
ON_COMMAND(ID_MENU_COMMSET, &CMultiTerminal1Dlg::OnMenuCommset)
|
ON_COMMAND(ID_MENU_SYSREG_SET, &CMultiTerminal1Dlg::OnMenuSysregSet)
|
ON_COMMAND(ID_MENU_STATUS_SHOW, &CMultiTerminal1Dlg::OnMenuStatusShow)
|
ON_COMMAND(ID_MENU_INFO_DISPLAY, &CMultiTerminal1Dlg::OnMenuInfoDisplay)
|
ON_COMMAND(ID_MENU_COIL_MON, &CMultiTerminal1Dlg::OnMenuCoilMon)
|
ON_COMMAND(ID_MENU_DATA_MON, &CMultiTerminal1Dlg::OnMenuDataMon)
|
ON_COMMAND(ID_MENU_COMM_TOOL, &CMultiTerminal1Dlg::OnMenuCommTool)
|
ON_WM_MOUSEWHEEL()
|
ON_BN_CLICKED(IDC_BUTTON_CLEAR_STAT, &CMultiTerminal1Dlg::OnBnClickedButtonClearStat)
|
ON_BN_CLICKED(IDC_BUTTON_READ, &CMultiTerminal1Dlg::OnBnClickedButtonRead)
|
ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CMultiTerminal1Dlg::OnBnClickedButtonConnect)
|
ON_COMMAND(ID_MENU_EVENT_LOG, &CMultiTerminal1Dlg::OnMenuEventLog)
|
ON_COMMAND(ID_MENU_COMM_TEST, &CMultiTerminal1Dlg::OnMenuCommTest)
|
ON_COMMAND(ID_MENU_FORCE_IO, &CMultiTerminal1Dlg::OnMenuForceIo)
|
ON_COMMAND(ID_MENU_DATETIME, &CMultiTerminal1Dlg::OnMenuDatetime)
|
ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, &CMultiTerminal1Dlg::OnBnClickedButtonDisconnect)
|
ON_BN_CLICKED(IDC_BUTTON11, &CMultiTerminal1Dlg::OnBnClickedButton11)
|
ON_BN_CLICKED(IDC_BUTTON12, &CMultiTerminal1Dlg::OnBnClickedButton12)
|
ON_BN_CLICKED(IDC_BUTTON13, &CMultiTerminal1Dlg::OnBnClickedButton13)
|
END_MESSAGE_MAP()
|
|
|
// CMultiTerminal1Dlg 消息处理程序
|
void CMultiTerminal1Dlg::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex, BOOL bSysMenu)
|
{
|
//显示toolbar
|
ASSERT(pPopupMenu != NULL);
|
// Check the enabled state of various menu items.
|
CCmdUI state;
|
state.m_pMenu = pPopupMenu;
|
ASSERT(state.m_pOther == NULL);
|
ASSERT(state.m_pParentMenu == NULL);
|
// Determine if menu is popup in top-level menu and set m_pOther to
|
// it if so (m_pParentMenu == NULL indicates that it is secondary popup).
|
HMENU hParentMenu;
|
if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
|
{
|
state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
|
}
|
else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
|
{
|
CWnd* pParent = this;
|
// Child windows don't have menus--need to go to the top!
|
if (pParent != NULL && (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
|
{
|
int nIndexMax = ::GetMenuItemCount(hParentMenu);
|
for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
|
{
|
if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
|
{
|
// When popup is found, m_pParentMenu is containing menu.
|
state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
|
break;
|
}
|
}
|
}
|
}
|
state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
|
for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; state.m_nIndex++)
|
{
|
state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
|
if (state.m_nID == 0)
|
{
|
continue; // Menu separator or invalid cmd - ignore it.
|
}
|
ASSERT(state.m_pOther == NULL);
|
ASSERT(state.m_pMenu != NULL);
|
if (state.m_nID == (UINT)-1)
|
{
|
// Possibly a popup menu, route to first item of that popup.
|
state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
|
if (state.m_pSubMenu == NULL
|
|| (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0
|
|| state.m_nID == (UINT)-1)
|
{
|
continue; // First item of popup can't be routed to.
|
}
|
state.DoUpdate(this, TRUE); // Popups are never auto disabled.
|
}
|
else
|
{
|
// Normal menu item.
|
// Auto enable/disable if frame window has m_bAutoMenuEnable
|
// set and command is _not_ a system command.
|
state.m_pSubMenu = NULL;
|
state.DoUpdate(this, FALSE);
|
}
|
// Adjust for menu deletions and additions.
|
UINT nCount = pPopupMenu->GetMenuItemCount();
|
if (nCount < state.m_nIndexMax)
|
{
|
state.m_nIndex -= (state.m_nIndexMax - nCount);
|
while (state.m_nIndex < nCount
|
&& pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
|
{
|
state.m_nIndex++;
|
}
|
}
|
state.m_nIndexMax = nCount;
|
}
|
|
}
|
|
BOOL CMultiTerminal1Dlg::OnInitDialog()
|
{
|
CDialogEx::OnInitDialog();
|
|
// 将“关于...”菜单项添加到系统菜单中。
|
|
// IDM_ABOUTBOX 必须在系统命令范围内。
|
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
|
ASSERT(IDM_ABOUTBOX < 0xF000);
|
|
CMenu* pSysMenu = GetSystemMenu(FALSE);
|
if (pSysMenu != nullptr)
|
{
|
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); // 设置小图标
|
|
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT,
|
WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS,
|
CRect(4, 4, 0, 0)) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
|
{
|
TRACE0("未能创建工具栏\n");
|
return -1;
|
}
|
m_wndToolBar.SetButtonStyle(7, TBBS_CHECKBOX);
|
m_wndToolBar.SetButtonStyle(17, TBBS_CHECKBOX);
|
m_wndToolBar.SetButtonStyle(18, TBBS_CHECKBOX);
|
m_wndToolBar.SetButtonStyle(19, TBBS_CHECKBOX);
|
m_wndToolBar.SetButtonStyle(20, TBBS_CHECKBOX);
|
m_wndToolBar.SetButtonStyle(26, TBBS_CHECKBOX);
|
|
m_wndToolBar.ShowWindow(SW_SHOW);
|
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
|
|
|
// TODO: 在此添加额外的初始化代码
|
|
CString VersionStr = _T("V1.02");
|
CString BuildStr = _T("20210728");
|
|
MyLogger1.AttachWnd(GetDlgItem(IDC_EDIT_LOG1)->m_hWnd);
|
MyLogger1.bShowLog[0] = 1;
|
MyLogger1.bShowThreadId = 0;
|
MyLogger1.SetLogPathName(_T("D:\\Logs\\MultiTerm1"), _T("MultiTerm1"));
|
MyLogger1.bSaveLog[0] = 1;
|
MyLogger1.bShowDate = 1;
|
|
SysLog(_T(" Start ") + VersionStr + _T(" ") + BuildStr + _T("\r\n"));
|
MyLogger1.bShowDate = 0;
|
CString s1;
|
GetWindowText(s1);
|
s1.Append(_T(" ") + VersionStr + _T(" ") + BuildStr);
|
SetWindowText(s1);
|
m_bResourceOpened = 0;
|
|
LoadResourceList();
|
DisplayParams();
|
|
LoadModel();
|
|
// m_treectrl1.SetExtendedStyle(TVS_EX_DRAWIMAGEASYNC,TVS_EX_DRAWIMAGEASYNC);
|
m_ColorTreeImages.Create(IDB_FILE_VIEW, 16, 0, RGB(255, 0, 255));
|
// m_ColorTreeImages.Create(IDB_PNG1,220,0,RGB(255,0,255));
|
|
m_treectrl1.SetImageList(&m_ColorTreeImages, TVSIL_NORMAL);
|
|
HTREEITEM hItem, hGroupItem, hSubItem;
|
hItem = m_treectrl1.InsertItem(_T("Parent1"),0,0, TVI_ROOT); // 在根结点上添加Parent1
|
hGroupItem = m_treectrl1.InsertItem(_T("Group1_1"), 1, 1, hItem);//在Parent1上添加一个子结点
|
|
hSubItem = m_treectrl1.InsertItem(_T("Child1_1"),2,2, hGroupItem);//在Parent1上添加一个子结点
|
hSubItem = m_treectrl1.InsertItem(_T("Child1_2"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面
|
hSubItem = m_treectrl1.InsertItem(_T("Child1_3"), 2, 2, hGroupItem, hSubItem);
|
|
hGroupItem = m_treectrl1.InsertItem(_T("Group1_2"), 1, 1, hItem, hGroupItem);//在Parent1上添加一个子结点
|
hSubItem = m_treectrl1.InsertItem(_T("Child1_4"), 2, 2, hGroupItem);//在Parent1上添加一个子结点
|
hSubItem = m_treectrl1.InsertItem(_T("Child1_5"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面
|
hSubItem = m_treectrl1.InsertItem(_T("Child1_6"), 2, 2, hGroupItem, hSubItem);
|
|
hItem = m_treectrl1.InsertItem(_T("Parent2"), TVI_ROOT, hItem);
|
hGroupItem = m_treectrl1.InsertItem(_T("Group2_1"), 1, 1, hItem);//在Parent1上添加一个子结点
|
|
hSubItem = m_treectrl1.InsertItem(_T("Child2_1"), 2, 2, hGroupItem);//在Parent1上添加一个子结点
|
hSubItem = m_treectrl1.InsertItem(_T("Child2_2"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面
|
hSubItem = m_treectrl1.InsertItem(_T("Child2_3"), 2, 2, hGroupItem, hSubItem);
|
|
hGroupItem = m_treectrl1.InsertItem(_T("Group2_2"), 1, 1, hItem, hGroupItem);//在Parent1上添加一个子结点
|
hSubItem = m_treectrl1.InsertItem(_T("Child2_4"), 2, 2, hGroupItem);//在Parent1上添加一个子结点
|
hSubItem = m_treectrl1.InsertItem(_T("Child2_5"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面
|
hSubItem = m_treectrl1.InsertItem(_T("Child2_6"), 2, 2, hGroupItem, hSubItem);
|
|
hItem = m_treectrl1.InsertItem(_T("Parent3"), TVI_ROOT, hItem);
|
hGroupItem = m_treectrl1.InsertItem(_T("Group3_1"), 1, 1, hItem);//在Parent1上添加一个子结点
|
|
hSubItem = m_treectrl1.InsertItem(_T("Child3_1"), 2, 2, hGroupItem);//在Parent1上添加一个子结点
|
hSubItem = m_treectrl1.InsertItem(_T("Child3_2"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面
|
hSubItem = m_treectrl1.InsertItem(_T("Child3_3"), 2, 2, hGroupItem, hSubItem);
|
|
hGroupItem = m_treectrl1.InsertItem(_T("Group3_2"), 1, 1, hItem, hGroupItem);//在Parent1上添加一个子结点
|
hSubItem = m_treectrl1.InsertItem(_T("Child3_4"), 2, 2, hGroupItem);//在Parent1上添加一个子结点
|
hSubItem = m_treectrl1.InsertItem(_T("Child3_5"), 2, 2, hGroupItem, hSubItem);//在Parent1上添加一个子结点,排在Child1_1后面
|
hSubItem = m_treectrl1.InsertItem(_T("Child3_6"), 2, 2, hGroupItem, hSubItem);
|
|
// TVINSERTSTRUCT TVI_ROOT
|
// m_treectrl1.SetItemData(htree15,15);
|
|
// m_status_Cusp_Size.SetCtlColor(RGB(0xff, 0x40, 0x40));
|
// m_status_Hz.SetCtlColor(RGB(0xff, 0x40, 0x40));
|
// m_status_db.SetCtlColor(RGB(0xff, 0x40, 0x40));
|
|
SetTimer(0, 50, NULL);//delayinit
|
SetTimer(2, 100, NULL);//display
|
|
m_static_connect.SetCtlColor(RGB(0, 255, 0));
|
|
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
|
}
|
|
int CMultiTerminal1Dlg::DelayInit()
|
{
|
// InitCamera();
|
// InitPlcComm();
|
// InitAnalyzer();
|
SetTimer(1, 10, NULL); //plc
|
SetTimer(3, 300, NULL); //Analyzer
|
SetTimer(4, 1000, NULL); //Speed;
|
m_static_connect.SetCtlColor(RGB(255, 0, 0));
|
// m_static_connect.RedrawWindow();
|
return 0;
|
}
|
|
int CMultiTerminal1Dlg::Clear_COM_Stats()
|
{
|
// TODO: 在此处添加实现代码.
|
MyHvSerialPort1.TotalRecvBytes = 0;
|
MyHvSerialPort1.TotalRecvBytes = 0;
|
|
MonitorTotalCount = 0;
|
MonitorSuccessCount = 0;
|
MonitorFailCount = 0;
|
return 0;
|
}
|
|
int CMultiTerminal1Dlg::UpdateDataDisplay()
|
{
|
if (!m_bOnline) return 0;
|
static int stepCount = 0;
|
CString s1, s2;
|
MyHvSerialPort1.CalSpeed();
|
s1.Format(_T("Send %8d %5d Bps \r\nRecv %8d %5d Bps\r\n"), MyHvSerialPort1.TotalSendBytes, MyHvSerialPort1.SendSpeed, MyHvSerialPort1.TotalRecvBytes, MyHvSerialPort1.RecvSpeed);
|
|
// s1.AppendFormat(_T("Total S %8d \r\nTotal R %8d \r\n"), MySerialCom1.TotalSendBytes, MySerialCom1.TotalRecvBytes);
|
s1 += intToString(MyHvSerialPort1.TotalRecvBytes) + _T("\r\n");
|
// stepCount++;
|
static double LastCalTime = GetTimeMs();
|
double thisTime = GetTimeMs();
|
if (thisTime - LastCalTime > 500)
|
{
|
MonitorSpeed = (MonitorTotalCount - LastMonitorTotalCount)*1000/(thisTime- LastCalTime);
|
LastMonitorTotalCount = MonitorTotalCount;
|
LastCalTime = thisTime;
|
// stepCount = 0;
|
}
|
s1.AppendFormat(_T("MT %6d Seq %d/%d %d/S \r\nOK %6d NG %d "),
|
MonitorTotalCount, MyKLProtocol1.m_nSeq,MyKLProtocol1.m_nRSeq, MonitorSpeed,
|
MonitorSuccessCount, MonitorFailCount );
|
SetDlgItemText(IDC_STATIC_SPEED, s1);
|
|
|
int nCount = 32;
|
s2.Empty();// s2.AppendFormat(_T("%d "), MonitorSuccessCount);
|
nCount = 16;
|
s2.Append(_T("WX "));
|
for (int i = 0; i < (nCount + 15) / 16; i++)
|
{
|
//s2.AppendFormat(_T("%02X: "), i * 16);
|
for (int j = 0; j < 8; j++)
|
{
|
s2.AppendFormat(_T("%04X"), MyKLProtocol1.MEM.WX[i * 8 + j]);
|
if (j == 3) { s2.Append(_T(" ")); }
|
else { s2.Append(_T(" ")); }
|
}
|
s2.Append(_T("\r\n"));
|
}
|
nCount = 16;
|
s2.Append(_T("WY "));
|
for (int i = 0; i < (nCount + 15) / 16; i++)
|
{
|
// s2.AppendFormat(_T("%02X: "), i * 16);
|
for (int j = 0; j < 8; j++)
|
{
|
s2.AppendFormat(_T("%04X"), MyKLProtocol1.MEM.WY[i * 8 + j]);
|
if (j == 3) { s2.Append(_T(" ")); }
|
else { s2.Append(_T(" ")); }
|
}
|
s2.Append(_T("\r\n"));
|
}
|
nCount = 32;
|
s2.Append(_T(" WR\r\n"));
|
for (int i = 0; i < (nCount + 15) / 16; i++)
|
{
|
s2.AppendFormat(_T("%02X: "), i * 16);
|
for (int j = 0; j < 8; j++)
|
{
|
s2.AppendFormat(_T("%04X"), MyKLProtocol1.MEM.WR[i * 8 + j]);
|
if (j == 3) { s2.Append(_T(" ")); }
|
else { s2.Append(_T(" ")); }
|
}
|
s2.Append(_T("\r\n"));
|
}
|
|
s2.Append(_T(" DT\r\n"));
|
for (int i = 0; i < (nCount + 15) / 16; i++)
|
{
|
s2.AppendFormat(_T("%02X: "), i * 16);
|
for (int j = 0; j < 8; j++)
|
{
|
s2.AppendFormat(_T("%04X"), MyKLProtocol1.MEM.DT[i * 8 + j]);
|
if (j == 3) { s2.Append(_T(" ")); }
|
else { s2.Append(_T(" ")); }
|
}
|
s2.Append(_T("\r\n"));
|
}
|
nCount = 256;
|
s2.Append(_T(" SDT\r\n"));
|
for (int i = 0; i < (nCount + 15) / 16; i++)
|
{
|
s2.AppendFormat(_T("%3X: "), i * 16);
|
for (int j = 0; j < 8; j++)
|
{
|
s2.AppendFormat(_T("%04X"), MyKLProtocol1.MEM.SDT[i * 8 + j]);
|
if (j == 3) { s2.Append(_T(" ")); }
|
else { s2.Append(_T(" ")); }
|
}
|
s2.Append(_T("\r\n"));
|
}
|
// s2.AppendFormat(_T("%d\r\n"), MyKLProtocol1.MEM.SDD[5]);
|
|
nCount = 32;
|
//SetRedraw(FALSE);
|
if (m_bMonitoring) {
|
SetDlgItemText(IDC_EDIT_MON, s2);
|
}
|
|
s2.Empty();
|
if (m_bMonitoring) {
|
s1.Empty();
|
s1.Format(_T("主机\r\n"));
|
|
s1.AppendFormat(_T("跳线 %02X %s 当前 %02X\r\n"), MyKLProtocol1.MEM.SDD[0], intToBinString(MyKLProtocol1.MEM.SDD[0]), MyKLProtocol1.MEM.SDD[1]);
|
// s1.AppendFormat(_T("当前跳线 %02X %s\r\n"), MyKLProtocol1.MEM.SDD[1], intToBinString(MyKLProtocol1.MEM.SDD[1]));
|
s1.AppendFormat(_T("Tick计数 %u \r\n"), MyKLProtocol1.MEM.SDD[2]);
|
s1.AppendFormat(_T("03 nRunCount %u \r\n"), MyKLProtocol1.MEM.SDD[3]);
|
s1.AppendFormat(_T("04 RunStat %d \r\n"), MyKLProtocol1.MEM.SDD[4]);
|
s1.AppendFormat(_T("05 ErrStat %d \r\n"), MyKLProtocol1.MEM.SDD[5]);
|
s1.AppendFormat(_T("06 PwrOnCount %d \r\n"), MyKLProtocol1.MEM.SDD[6]);
|
s1.AppendFormat(_T("07 ThisRunTime %d \r\n"), MyKLProtocol1.MEM.SDD[7]);
|
|
int nTime1 = MyKLProtocol1.MEM.SDD[7];
|
s1.AppendFormat(_T("%dd %02d:%02d:%02d \r\n"), nTime1 / 86400, nTime1 / 3600 % 24, nTime1 / 60 % 60, nTime1 % 60);
|
s1.AppendFormat(_T("08 TotalTime %d \r\n"), MyKLProtocol1.MEM.SDD[8]);
|
nTime1 = MyKLProtocol1.MEM.SDD[8];
|
s1.AppendFormat(_T("%dd %02d:%02d:%02d \r\n"), nTime1 / 86400, nTime1 / 3600 % 24, nTime1 / 60 % 60, nTime1 % 60);
|
s1.AppendFormat(_T("09 CurTime %d \r\n"), MyKLProtocol1.MEM.SDD[9]);
|
|
nTime1 = MyKLProtocol1.MEM.SDD[9];
|
__time32_t time1=nTime1;
|
CString s3;
|
//_tctime32_s(s3.GetBuffer(1024),1024, &time1);
|
//s3.ReleaseBuffer();
|
|
CTime ctime1=time1;
|
if (nTime1>=0) s3=ctime1.Format(_T("%Y-%m-%d %H:%M:%S"));
|
s1.Append(s3 + _T("\r\n"));
|
// s1.AppendFormat(_T("%04d-%02d-%02d %02d:%02d:%02d \r\n"), nTime1 / 86400, nTime1 / 3600 % 24, nTime1 / 60 % 60, nTime1 % 60);
|
s1.AppendFormat(_T("10 PwrFailCount %d \r\n"), MyKLProtocol1.MEM.SDD[10]);
|
s1.AppendFormat(_T("11 LastPwrFailTime %d \r\n"), MyKLProtocol1.MEM.SDD[11]);
|
nTime1 = MyKLProtocol1.MEM.SDD[11];
|
time1 = nTime1;
|
_tctime32_s(s3.GetBuffer(1024), 1024, &time1);
|
s3.ReleaseBuffer();
|
ctime1 = time1;
|
ctime1 += (30 * 365 + 7 ) * 24 * 3600 ;
|
ctime1 = CStringToCTime(_T("2000-01-01 08:00:00"));
|
ctime1 += nTime1;
|
s3 = ctime1.Format(_T("%Y-%m-%d %H:%M:%S"));
|
s1.Append(s3 + _T("\r\n"));
|
|
s1.AppendFormat(_T("12 LastScanTime uS %d \r\n"), MyKLProtocol1.MEM.SDD[12]);
|
s1.AppendFormat(_T("13 ScanTime uS %d \r\n"), MyKLProtocol1.MEM.SDD[13]);
|
s1.AppendFormat(_T("14 MinScanTime uS %d \r\n"), MyKLProtocol1.MEM.SDD[14]);
|
s1.AppendFormat(_T("15 %d \r\n"), MyKLProtocol1.MEM.SDD[15]);
|
s1.AppendFormat(_T("16 %d \r\n"), MyKLProtocol1.MEM.SDD[16]);
|
s1.AppendFormat(_T("17 %d \r\n"), MyKLProtocol1.MEM.SDD[17]);
|
s1.AppendFormat(_T("18 %d \r\n"), MyKLProtocol1.MEM.SDD[18]);
|
s1.AppendFormat(_T("19 %d \r\n"), MyKLProtocol1.MEM.SDD[19]);
|
s1.AppendFormat(_T("20 %d \r\n"), MyKLProtocol1.MEM.SDD[20]);
|
s1.AppendFormat(_T("21 %d \r\n"), MyKLProtocol1.MEM.SDD[21]);
|
s1.AppendFormat(_T("22 %d \r\n"), MyKLProtocol1.MEM.SDD[22]);
|
s1.AppendFormat(_T("23 %d \r\n"), MyKLProtocol1.MEM.SDD[23]);
|
|
float Vref = 1.215f;
|
float V33 = Vref * 4096 / MyKLProtocol1.MEM.SDT[55];
|
|
s1.AppendFormat(_T("24 %d %.3f \r\n"), MyKLProtocol1.MEM.SDT[48], MyKLProtocol1.MEM.SDT[48] * V33 /4096 *11);
|
s1.AppendFormat(_T("25 %d \r\n"), MyKLProtocol1.MEM.SDT[49]);
|
s1.AppendFormat(_T("26 %d %.3f \r\n"), MyKLProtocol1.MEM.SDT[50], MyKLProtocol1.MEM.SDT[50] * V33 /4096 *2);
|
s1.AppendFormat(_T("27 %d \r\n"), MyKLProtocol1.MEM.SDT[51]);
|
s1.AppendFormat(_T("28 %d \r\n"), MyKLProtocol1.MEM.SDT[52]);
|
s1.AppendFormat(_T("29 %d \r\n"), MyKLProtocol1.MEM.SDT[53]);
|
s1.AppendFormat(_T("30 %d \r\n"), MyKLProtocol1.MEM.SDT[54]);
|
s1.AppendFormat(_T("31 %d %.3f \r\n"), MyKLProtocol1.MEM.SDT[55],V33);
|
|
|
|
//型号
|
//版本
|
//协议版本
|
//生产日期
|
//生产批号
|
//序列号
|
|
//开机次数
|
//总开机时间
|
//最大连续开机时间
|
//本次开机时间
|
//电源闪断次数
|
//总看门狗动作次数
|
//本次看门狗动作次数
|
//电源电压
|
//核心温度
|
|
//输入输出
|
//IO状态,开路,短路,熔断.....
|
|
//包顺序错误
|
//令牌 广播形式, 环路形式
|
//
|
|
s2 += s1;
|
s2.AppendFormat(_T("\r\n"));
|
int nOffset = 32;
|
s1.Empty();
|
s1.Format(_T("子机1\r\n"));
|
KLinkProtocol::pChnStat pchnstat1 = (KLinkProtocol::pChnStat)&MyKLProtocol1.MEM.SDD[nOffset+0];
|
unsigned int nSendPackets = pchnstat1->SendPackets;
|
s1.AppendFormat(_T("当前状态: %04X\r\n"), pchnstat1->Stat);
|
s1.AppendFormat(_T("发收包数:%6d/%6d(%.2f%%)\r\n发送时间 %10u (uS)\r\n"),
|
pchnstat1->SendPackets,
|
pchnstat1->RecvPackets, pchnstat1->RecvPackets *100.0/ nSendPackets,
|
pchnstat1->LastSentTimeuS);
|
|
s1.AppendFormat(_T("丢包计数:%d(%.2f%%) 连续 %d(Max:%d)\r\n"),
|
pchnstat1->LostPackets, pchnstat1->LostPackets *100.0 / nSendPackets,
|
pchnstat1->CtnLstPkts,
|
pchnstat1->MaxCtnLstPkts);
|
|
s1.AppendFormat(_T("返回延迟 %d(max:%d) uS\r\n"),
|
pchnstat1->Delay, pchnstat1->MaxDelay);
|
s1.AppendFormat(_T("循环时间 %d uS\r\n"),
|
pchnstat1->SendTimeInterval);
|
|
s1.AppendFormat(_T("子机收发 %6d/%6d \r\n"),
|
pchnstat1->ClientRecvPkts,
|
pchnstat1->ClientSendPkts);
|
|
s2 += s1;
|
s2.AppendFormat(_T("\r\n"));
|
|
nOffset = 32 + 24;
|
s1.Format(_T("子机2\r\n"));
|
pchnstat1 = (KLinkProtocol::pChnStat)&MyKLProtocol1.MEM.SDD[nOffset + 0];
|
nSendPackets = pchnstat1->SendPackets;
|
s1.AppendFormat(_T("当前状态: %04X\r\n"), pchnstat1->Stat);
|
s1.AppendFormat(_T("发收包数:%6d/%6d(%.2f%%)\r\n发送时间 %10u (uS)\r\n"),
|
pchnstat1->SendPackets,
|
pchnstat1->RecvPackets, pchnstat1->RecvPackets *100.0 / nSendPackets,
|
pchnstat1->LastSentTimeuS);
|
|
s1.AppendFormat(_T("丢包计数:%d(%.2f%%) 连续 %d(Max:%d)\r\n"),
|
pchnstat1->LostPackets, pchnstat1->LostPackets *100.0 / nSendPackets,
|
pchnstat1->CtnLstPkts,
|
pchnstat1->MaxCtnLstPkts);
|
|
s1.AppendFormat(_T("返回延迟 %d(max:%d) uS\r\n"),
|
pchnstat1->Delay, pchnstat1->MaxDelay);
|
s1.AppendFormat(_T("循环时间 %d uS\r\n"),
|
pchnstat1->SendTimeInterval);
|
|
s1.AppendFormat(_T("子机收发 %6d/%6d \r\n"),
|
pchnstat1->ClientRecvPkts,
|
pchnstat1->ClientSendPkts);
|
|
s2 += s1;
|
//m_edit_mon2.SetSel(0, -1);
|
//m_edit_mon2.ReplaceSel(s2);
|
SetDlgItemText(IDC_EDIT_MON2, s2);
|
}
|
//SetRedraw(TRUE);
|
//s1.Append(s2);
|
//s1.Append(_T("\r\n"));
|
|
return 0;
|
}
|
|
void CMultiTerminal1Dlg::OnTimer(UINT_PTR nIDEvent)
|
{
|
// TODO: 在此添加消息处理程序代码和/或调用默认值
|
CString s1,s2;
|
|
if (nIDEvent == 0)
|
{
|
KillTimer(0);
|
DelayInit();
|
}
|
else if (nIDEvent == 1)
|
{
|
//ProcessPLC();
|
MonitorPLC();
|
}
|
else if (nIDEvent == 2)
|
{
|
// ProcessInput();
|
//DrawPic1();
|
MyLogger1.UpdateLogDisplay(0);
|
UpdateDataDisplay();
|
}
|
else if (nIDEvent == 3)
|
{
|
if (m_bResourceOpened && !m_bPause)
|
{
|
// int n=AcqureDatas(m_nChannel);
|
int n = 0;
|
if (n > 0)
|
{
|
}
|
}
|
}
|
else if (nIDEvent == 4) { }
|
else { }
|
CDialogEx::OnTimer(nIDEvent);
|
}
|
|
void CMultiTerminal1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
|
{
|
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
|
{
|
CAboutDlg dlgAbout;
|
dlgAbout.DoModal();
|
}
|
else
|
{
|
CDialogEx::OnSysCommand(nID, lParam);
|
}
|
}
|
|
// 如果向对话框添加最小化按钮,则需要下面的代码
|
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
|
// 这将由框架自动完成。
|
|
void CMultiTerminal1Dlg::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 CMultiTerminal1Dlg::OnQueryDragIcon()
|
{
|
return static_cast<HCURSOR>(m_hIcon);
|
}
|
|
BOOL CMultiTerminal1Dlg::PreTranslateMessage(MSG* pMsg)
|
{
|
// TODO: 在此添加专用代码和/或调用基类
|
CString s1;
|
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
|
{
|
return TRUE;
|
}
|
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
|
{
|
if (GetDlgItem(IDC_COMBO_INPUT) == GetFocus())
|
{
|
OnBnClickedButtonSend();
|
}
|
return FALSE;
|
}
|
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_BACK)
|
{
|
return FALSE;
|
}
|
if (pMsg->message == WM_KEYDOWN)
|
{
|
|
BOOL bCtrl = ::GetKeyState(VK_CONTROL) & 0x80;
|
switch (pMsg->wParam)
|
{
|
case 'a':
|
case 'A':
|
if (bCtrl)
|
{
|
CEdit* edit = (CEdit*)(GetDlgItem(IDC_EDIT_LOG1));
|
//edit->SetFocus();
|
edit->SetSel(0, -1);
|
return FALSE;
|
}
|
}
|
}
|
try
|
{
|
return CDialogEx::PreTranslateMessage(pMsg);
|
}
|
catch (CMemoryException* e)
|
{
|
CString Serror;
|
e->GetErrorMessage(Serror.GetBuffer(256), 256);
|
Serror.ReleaseBuffer();
|
s1.Format(_T("程序 发生 内存 错误 %s \r\n"), Serror);
|
PopupMessage(s1);
|
}
|
catch (CFileException* e)
|
{
|
CString Serror;
|
e->GetErrorMessage(Serror.GetBuffer(256), 256);
|
Serror.ReleaseBuffer();
|
s1.Format(_T("程序 发生 文件 错误 %s \r\n"), Serror);
|
PopupMessage(s1);
|
}
|
catch (SE_Exception & e)
|
{
|
s1.Format(_T("程序 发生 SE 错误 Code %08X Addr %08X \r\n%s\r\n"), e.getSeNumber(), e.getAddress(), e.getInfoStr());
|
PopupMessage(s1);
|
}
|
catch (CException* e)
|
{
|
CString Serror;
|
e->GetErrorMessage(Serror.GetBuffer(256), 256);
|
Serror.ReleaseBuffer();
|
s1.Format(_T("程序 发生 C++ 错误 %s \r\n"), Serror);
|
PopupMessage(s1);
|
}
|
catch (...)
|
{
|
s1.Format(_T("程序 发生未知错误 \r\n"));
|
PopupMessage(s1);
|
}
|
return TRUE;
|
|
return CDialogEx::PreTranslateMessage(pMsg);
|
}
|
|
void CMultiTerminal1Dlg::FindComPort()
|
{
|
CString s1, s2;
|
m_combo_comport.ResetContent();
|
for (int i = 1; i <= 16; i++)
|
{
|
s1.Format(_T("COM%d"), i);
|
if (get_com_name(s1, s2)) s1 = s2;
|
m_combo_comport.AddString(s1);
|
}
|
sComSelStr = _T("COM3");
|
m_combo_comport.SelectString(0, sComSelStr);
|
m_combo_comport.SetCurSel(2);
|
return;
|
HKEY hKey;
|
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Hardware\\DeviceMap\\SerialComm"), NULL, KEY_READ, &hKey) == ERROR_SUCCESS)
|
{
|
TCHAR szPortName[256], szComName[256];
|
DWORD dwLong, dwSize;
|
int nCount = 0;
|
|
m_combo_comport.ResetContent();
|
while (true)
|
{
|
dwLong = dwSize = 256;
|
if (RegEnumValue(hKey, nCount, szPortName, &dwLong, NULL, NULL, (PUCHAR)szComName, &dwSize) == ERROR_NO_MORE_ITEMS)
|
break;
|
|
m_combo_comport.InsertString(nCount, szComName);
|
nCount++;
|
}
|
RegCloseKey(hKey);
|
m_combo_comport.SetCurSel(0);
|
}
|
}
|
BOOL CMultiTerminal1Dlg::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
|
{
|
if (nEventType == DBT_DEVNODES_CHANGED)
|
{
|
FindComPort();
|
}
|
|
return TRUE;
|
}
|
|
int CMultiTerminal1Dlg::LoadResourceList()
|
{
|
Hash & ResourceCfg = MyCfg1["Resources"];
|
int nCount;
|
nCount = _tstoi(ResourceCfg["Count"]);
|
CString s1, s2;
|
for (int i = 0; i < nCount; i++)
|
{
|
s1.Format(_T("Addr%d"), i);
|
s2 = ResourceCfg[s1];
|
((CComboBox *)GetDlgItem(IDC_COMBO_RES))->AddString(s2);
|
}
|
s1 = ResourceCfg["LastAddr"];
|
((CComboBox *)GetDlgItem(IDC_COMBO_RES))->SetWindowText(s1);
|
return nCount;
|
}
|
|
int CMultiTerminal1Dlg::SaveResourceList()
|
{
|
Hash & ResourceCfg = MyCfg1["Resources"];
|
int nCount1 = _tstoi(ResourceCfg["Count"]);
|
CString s1, s2, s3;
|
((CComboBox *)GetDlgItem(IDC_COMBO_RES))->GetWindowText(s3);
|
|
int bFound = 0;
|
int nCount2 = ((CComboBox *)GetDlgItem(IDC_COMBO_RES))->GetCount();
|
for (int i = 0; i < nCount2; i++)
|
{
|
((CComboBox *)GetDlgItem(IDC_COMBO_RES))->GetLBText(i, s2);
|
if (s2 == s3) { bFound = 1; break; }
|
}
|
if (!bFound)
|
{
|
((CComboBox *)GetDlgItem(IDC_COMBO_RES))->AddString(s3);
|
s1.Format(_T("Addr%d"), nCount1);
|
ResourceCfg[s1] = s3;
|
nCount1 = nCount1 + 1;
|
s1.Format(_T("%d"), nCount1);
|
ResourceCfg["Count"] = s1;
|
}
|
ResourceCfg["LastAddr"] = s3;
|
|
return nCount1;
|
}
|
int SendPacket(void * pBuf, int Len)
|
{
|
return MyHvSerialPort1.Send((char *)pBuf, Len);
|
}
|
int RecvPacket(void * pBuf, int Len)
|
{
|
int j= MyHvSerialPort1.Recv((char *)pBuf, Len);
|
if (j<=0)
|
{
|
CString s1;
|
s1.Format(_T("Read %d R=%d %s"), Len, j, MyHvSerialPort1.m_strResult);
|
SysLog(s1);
|
}
|
return j;
|
}
|
|
int CMultiTerminal1Dlg::DisplayParams()
|
{
|
CString s1, s2;
|
Hash & ModelConfg = MyCfg1["Model"];
|
ModelConfg.SetCtrlList(this, myCfgValCtrls, nMarkConfigs);
|
|
for (int i = 1; i <= 16; i++)
|
{
|
s1.Format(_T("COM%d"), i);
|
if (get_com_name(s1, s2)) (s1 = s2);
|
m_combo_comport.AddString(s1);
|
|
}
|
sComSelStr = _T("COM3");
|
m_combo_comport.SelectString(0, sComSelStr);
|
m_combo_comport.SetCurSel(2);
|
|
m_combo_com_baud.AddString(_T("1200"));
|
m_combo_com_baud.AddString(_T("2400"));
|
m_combo_com_baud.AddString(_T("4800"));
|
m_combo_com_baud.AddString(_T("9600"));
|
m_combo_com_baud.AddString(_T("19200"));
|
m_combo_com_baud.AddString(_T("38400"));
|
m_combo_com_baud.AddString(_T("57600"));
|
m_combo_com_baud.AddString(_T("115200"));
|
m_combo_com_baud.AddString(_T("128000"));
|
m_combo_com_baud.AddString(_T("187500"));
|
m_combo_com_baud.AddString(_T("230400"));
|
m_combo_com_baud.AddString(_T("460800"));
|
m_combo_com_baud.AddString(_T("921600"));
|
m_combo_com_baud.AddString(_T("1000000"));
|
m_combo_com_baud.AddString(_T("2000000"));
|
m_combo_com_baud.AddString(_T("3000000"));
|
sBaudSelStr = _T("230400");
|
m_combo_com_baud.SelectString(0, sBaudSelStr);
|
|
MyThreadProc1ToRun = 0;
|
MyThreadProc1Running = 0;
|
|
((CComboBox *)GetDlgItem(IDC_COMBO_MADDR))->SetCurSel(0);
|
((CComboBox *)GetDlgItem(IDC_COMBO_CMD))->SetCurSel(2);
|
((CComboBox *)GetDlgItem(IDC_COMBO_CMD_TYPE))->SetCurSel(0);
|
((CComboBox *)GetDlgItem(IDC_COMBO_DADDR))->SetCurSel(0);
|
((CComboBox *)GetDlgItem(IDC_COMBO_DCOUNT))->SetCurSel(6);
|
|
|
MyKLProtocol1.SetSendCallBackFunc(&SendPacket);
|
MyKLProtocol1.SetRecvCallBackFunc(&RecvPacket);
|
|
return 0;
|
}
|
|
int CMultiTerminal1Dlg::GetParams()
|
{
|
CString s1, s2;
|
Hash & ModelConfg = MyCfg1["Model"];
|
ModelConfg.GetCtrlList(this, myCfgValCtrls, nMarkConfigs);
|
|
return 0;
|
}
|
|
int CMultiTerminal1Dlg::LoadModel()
|
{
|
CString s1;
|
|
Hash & ModelConfg = MyCfg1["Model"];
|
return 0;
|
}
|
|
int CMultiTerminal1Dlg::SaveModel()
|
{
|
CString s1, s2;
|
Hash & ModelConfg = MyCfg1["Model"];
|
return 0;
|
}
|
|
|
int CMultiTerminal1Dlg::OpenResource(CStringA ResourceStr)
|
{
|
CString s1;
|
m_combo_comport.GetWindowText(sComSelStr);
|
dComNum = m_combo_comport.GetCurSel();
|
m_static_connect.SetCtlColor(RGB(0, 255, 0));
|
|
s1.Format(_T("打开串口%s %d "), sComSelStr, dComNum + 1);
|
SysLog(s1);
|
|
sComSelStr.Format(_T("COM%d"), dComNum + 1);
|
|
CString baudstr;
|
m_combo_com_baud.GetWindowText(baudstr);
|
int nBaud = _tstoi(baudstr);
|
|
MyHvSerialPort1.m_nPort = dComNum + 1;
|
MyHvSerialPort1.m_nBaudRate = nBaud;
|
MyHvSerialPort1.m_Settings = "8,N,1";
|
int r = MyHvSerialPort1.Open();
|
s1.Format(_T("Open %s = %d"), MyHvSerialPort1.m_strResult, r);
|
SysLog(s1);
|
if (r==MyHvSerialPort1.R_OK)
|
{
|
m_bResourceOpened = true;
|
MyKLProtocol1.Open();
|
m_static_connect.SetCtlColor(RGB(0, 255, 0));
|
return true;
|
}
|
return false;
|
}
|
|
int CMultiTerminal1Dlg::CloseResource()
|
{
|
m_static_connect.SetCtlColor(RGB(80, 80, 80));
|
if (m_bResourceOpened)
|
{
|
MyHvSerialPort1.Close();
|
MyKLProtocol1.Close();
|
m_bResourceOpened = false;
|
}
|
return 0;
|
}
|
|
int CMultiTerminal1Dlg::LoadData(CString sFilePathName)
|
{
|
CFile file1;
|
CFileException e1;
|
CStringA s1a;
|
int nDataCount = 0 ;
|
int r = file1.Open(sFilePathName, CFile::modeRead, &e1);
|
if (r)
|
{
|
int len1 = (int)file1.GetLength();
|
file1.Read(s1a.GetBufferSetLength(len1 + 100), len1);
|
s1a.ReleaseBuffer(len1);
|
int k = s1a.Find("\n", 0);
|
int n = atoi(s1a.Left(k));
|
CStringA s2A;
|
CStringA resultstr[100];
|
for (int i = 0; i < n; i++)
|
{
|
int j = s1a.Find("\n", k + 1);
|
s2A = s1a.Mid(k + 1, j - k - 1);
|
Split(s2A, ",", resultstr);
|
float a = (float)atof(resultstr[0]);
|
float b = (float)atof(resultstr[1]);
|
float c = (float)atof(resultstr[2]);
|
k = j;
|
}
|
nDataCount = n;
|
}
|
return nDataCount;
|
}
|
|
int CMultiTerminal1Dlg::SaveData(CString sFilePathName)
|
{
|
CFile file1;
|
CFileException e1;
|
CStringA s1a;
|
int nDataCount = 0;
|
int r = file1.Open(sFilePathName, CFile::modeCreate | CFile::modeWrite, &e1);
|
if (r)
|
{
|
s1a.Format("%d\r\n", nDataCount);
|
for (int i = 0; i < nDataCount; i++)
|
{
|
s1a.AppendFormat("%f,%f,%f\r\n", 0, 0, 0);
|
}
|
file1.Write(s1a.GetBuffer(), s1a.GetLength());
|
file1.Close();
|
}
|
return 0;
|
}
|
|
void CMultiTerminal1Dlg::OnSize(UINT nType, int cx, int cy)
|
{
|
CDialogEx::OnSize(nType, cx, cy);
|
if (!this->IsWindowVisible() && !BInited) { return; }
|
CString s1;
|
s1.Format(_T("Dialog OnSize Type %d cx %d cy %d \r\n"), nType, cx, cy);
|
// SysLog(s1);
|
switch (nType)
|
{
|
case SIZE_MAXHIDE:
|
case SIZE_MAXSHOW:
|
case SIZE_MINIMIZED:
|
return;
|
}
|
if (nType == SIZE_MAXIMIZED || nType == SIZE_RESTORED)
|
{
|
// calLayerOuts();
|
// CalButtonsLayerOut();
|
}
|
// TODO: 在此处添加消息处理程序代码
|
}
|
|
int CMultiTerminal1Dlg::calLayerOuts()
|
{
|
GetClientRect(&MainClientRect);
|
MainClientRect.bottom -= 20;
|
int w = MainClientRect.Width();
|
int h = MainClientRect.Height();
|
DivRectX(MainClientRect, LeftRect, RightRect, 600, 0.7f);
|
|
DivRectY3(LeftRect, ItemRect, ButtonRect, LogRect, 0.7f, 0.1f, 0.2f);
|
|
CRect ItemRect1, ItemRect2;
|
DivRectX(ItemRect, ItemRect1, ItemRect2, 0.5f, 0.5f);
|
|
PicRect = RightRect;
|
|
DivRectY(PicRect, PicRectUp, PicRectDown, 24, 0.95f);
|
|
// dialogitemshow1.MoveWindow(&ItemRect1);
|
// dialogitemshow2.MoveWindow(&ItemRect2);
|
|
GetDlgItem(IDC_EDIT_DISPLAY)->MoveWindow(&PicRectUp);
|
GetDlgItem(IDC_COMBO_INPUT)->MoveWindow(&PicRectDown);
|
GetDlgItem(IDC_EDIT_LOG1)->MoveWindow(&LogRect);
|
|
//CalButtonsLayerOut();
|
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
|
|
|
return 0;
|
}
|
|
int CMultiTerminal1Dlg::MonitorPLC()
|
{
|
if (m_bMonitoring && MyHvSerialPort1.m_bOpened)
|
{
|
unsigned char DstAddr = 1;
|
unsigned char DataType = MyKLProtocol1.KLDataTypeSDT;
|
unsigned short DAddr = 0;
|
unsigned char DCount = 32;
|
// unsigned char Data1[256];
|
unsigned char ExtFrameLen = 0;
|
|
unsigned short nCount;
|
CString s1,s2;
|
MonitorTotalCount++;
|
int nThisSuccessCount = 0;
|
int nThisFailCount = 0;
|
int res;
|
static int nStep = 0;
|
// WX
|
///*
|
if (nStep == 0)
|
{
|
DataType = MyKLProtocol1.KLDataTypeWX;
|
DCount = 16;
|
res = MyKLProtocol1.ReadDataByte(DstAddr, DCount, DataType, DAddr, &MyKLProtocol1.MEM.WXB[DAddr]);
|
nCount = DCount;
|
if (res == MyKLProtocol1.KL_OK)
|
{
|
nThisSuccessCount++;
|
}
|
else
|
{
|
nThisFailCount++;
|
s1.Format(_T("R:= %d %s \r\n"), res, MyKLProtocol1.m_resultStr);
|
SysLog(s1);
|
}
|
|
}
|
//*/
|
|
///*
|
//WY
|
if (nStep == 1)
|
{
|
DataType = MyKLProtocol1.KLDataTypeWY;
|
DCount = 16;
|
res = MyKLProtocol1.ReadDataByte(DstAddr, DCount, DataType, DAddr, &MyKLProtocol1.MEM.WYB[DAddr]);
|
nCount = DCount;
|
if (res == MyKLProtocol1.KL_OK)
|
{
|
nThisSuccessCount++;
|
}
|
else
|
{
|
nThisFailCount++;
|
s1.Format(_T("R:= %d %s \r\n"), res, MyKLProtocol1.m_resultStr);
|
SysLog(s1);
|
}
|
//*/
|
}
|
/*
|
//WR
|
if (nStep==2)
|
{
|
DataType = MyKLProtocol1.KLDataTypeWR;
|
DCount = 32;
|
res = MyKLProtocol1.ReadDataByte(DstAddr, DCount, DataType, DAddr, &MyKLProtocol1.MEM.WRB[DAddr]);
|
nCount = DCount;
|
if (res == MyKLProtocol1.KL_OK)
|
{
|
nThisSuccessCount++;
|
}
|
else
|
{
|
nThisFailCount++;
|
s1.Format(_T("R:= %d %s \r\n"), res, MyKLProtocol1.m_resultStr);
|
SysLog(s1);
|
}
|
}
|
//*/
|
//DT
|
if (nStep == 3)
|
{
|
DataType = MyKLProtocol1.KLDataTypeDT;
|
DCount = 32;
|
res = MyKLProtocol1.ReadDataByte(DstAddr, DCount, DataType, DAddr, &MyKLProtocol1.MEM.DTB[DAddr]);
|
nCount = DCount;
|
if (res == MyKLProtocol1.KL_OK)
|
{
|
nThisSuccessCount++;
|
}
|
else
|
{
|
nThisFailCount++;
|
s1.Format(_T("R:= %d %s \r\n"), res, MyKLProtocol1.m_resultStr);
|
SysLog(s1);
|
}
|
|
}
|
int ByteAddr = 0;
|
//SDT
|
if (nStep >= 4 && nStep <=7)
|
{
|
int nBlock = nStep - 4;
|
DataType = MyKLProtocol1.KLDataTypeSDT;
|
ByteAddr = nBlock * 128;
|
DCount = 128;
|
res = MyKLProtocol1.ReadDataByte(DstAddr, DCount, DataType, ByteAddr, &MyKLProtocol1.MEM.SDB[ByteAddr]);
|
nCount = DCount;
|
if (res == MyKLProtocol1.KL_OK)
|
{
|
for (int i = 0; i < DCount; i++) { SDTbuf[ByteAddr + i] = MyKLProtocol1.MEM.SDB[ByteAddr + i]; }
|
nThisSuccessCount++;
|
}
|
else
|
{
|
nThisFailCount++;
|
s1.Format(_T("R:= %d %s \r\n"), res, MyKLProtocol1.m_resultStr);
|
SysLog(s1);
|
}
|
}
|
|
nStep++;
|
if (nStep == 2) { nStep = 3; }
|
if (nStep > 7) { nStep = 0; }
|
if (nThisFailCount == 0)
|
{
|
MonitorSuccessCount++;
|
|
}
|
else
|
{
|
MonitorFailCount++;
|
}
|
}
|
return 0;
|
}
|
void CMultiTerminal1Dlg::OnClose()
|
{
|
// TODO: 在此添加消息处理程序代码和/或调用默认值
|
// if (pHvCamera1 != nullptr) { delete pHvCamera1; pHvCamera1 = nullptr; }
|
MyThreadProc1ToRun = 0;
|
CloseResource();
|
GetParams();
|
SaveMyConfig();
|
Sleep(100);
|
|
CDialogEx::OnClose();
|
}
|
|
int DrawCross(HWND hWnd1, int handx, int handy)
|
{
|
///*
|
using namespace Gdiplus;
|
CRect rect1;
|
GetClientRect(hWnd1, &rect1);
|
|
int ClientWidth = rect1.Width();
|
int ClientHeight = rect1.Height();
|
int CenterX = ClientWidth / 2;
|
int CenterY = ClientHeight / 2;
|
|
|
int ZeroRage = 14;
|
int rZeroRage = ZeroRage * ClientWidth / 256;
|
Bitmap bmp1(ClientWidth, ClientHeight);
|
Graphics gr1(&bmp1);
|
|
gr1.Clear(Gdiplus::Color::White);
|
|
Pen pen1(Color::Gray, 1);
|
Pen pen2(Color::Black, 2);
|
//画大十字线
|
gr1.DrawLine(&pen1, 0, CenterY, ClientWidth, CenterY);
|
gr1.DrawLine(&pen1, CenterX, 0, CenterX, ClientHeight);
|
|
//画中心零点的范围
|
gr1.DrawRectangle(&pen1, CenterX - rZeroRage, CenterY - rZeroRage, rZeroRage *2, rZeroRage *2);
|
|
//画允许框的范围
|
gr1.DrawRectangle(&pen1, ClientWidth * 10 / 100, ClientHeight * 10 / 100, ClientWidth * 80 / 100, ClientHeight * 80 / 100);
|
|
//画小十字线
|
int curx = handx * ClientWidth / 256;
|
int cury = (255 - handy) *ClientHeight / 256;
|
|
if (curx < 0) { curx = 0; }
|
if (curx > ClientWidth) { curx = ClientWidth; }
|
if (cury < 0) { cury = 0; }
|
if (cury > ClientHeight) { cury = ClientHeight; }
|
|
gr1.DrawLine(&pen2, curx - 8, cury, curx + 8, cury);
|
gr1.DrawLine(&pen2, curx, cury - 8, curx, cury + 8);
|
|
HDC hdc1 = GetDC(hWnd1);
|
SetTextColor(hdc1, 0x00c0c0c0);
|
SetBkColor(hdc1, 0x00000000);
|
|
Graphics gr2(hdc1);
|
gr2.DrawImage(&bmp1, 0, 0);
|
ReleaseDC(hWnd1, hdc1);
|
|
return 0;
|
}
|
|
DWORD WINAPI CMultiTerminal1Dlg::MyThreadProc1(LPVOID pParam)
|
{
|
CString s1,s2;
|
SysLog(_T("线程开始运行\r\n"));
|
MyThreadProc1Running = 1;
|
|
|
/*
|
static OVERLAPPED ovlap1, ovlap2;
|
COMMTIMEOUTS ctimeout1;
|
|
|
ovlap1.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
ovlap2.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
memset(&ctimeout1, 0, sizeof(ctimeout1));
|
ctimeout1.ReadIntervalTimeout = 1;
|
ctimeout1.ReadTotalTimeoutMultiplier = 0;
|
ctimeout1.ReadTotalTimeoutConstant = 1;
|
ctimeout1.WriteTotalTimeoutConstant = 0;
|
ctimeout1.WriteTotalTimeoutMultiplier = 0;
|
*/
|
|
//s2.GetLength();
|
// memcpy(send1,s2,bytetosend1);
|
|
MyAnsiParser1.AttachWnd(m_edit_display.GetSafeHwnd());
|
MyAnsiParser1.SetScreenWH(150, 40);
|
|
// CClientDC hcurDC(this);
|
HWND hDrawXY = GetDlgItem(IDC_STATIC_DRAW_XY)->m_hWnd;
|
|
int handx = 0, handy = 0;
|
int oldhandx = 0, oldhandy = 0;
|
|
|
SysLog(_T("准备接收\r\n"));
|
DWORD dNumtoRead = 1000;
|
dNumtoRead = 64;
|
nDataToSend = 0;
|
DWORD wCount2 = 0;
|
DWORD dwSent = 0;
|
int nRunCount = 0;
|
int nTotalRecv = 0;
|
//计算接收速度;
|
|
int LastSpeedTime = (int)GetTickCountmS();
|
int nThisRecv = 0;
|
double AvgSpeed = 0;
|
double CurSpeed = 0;
|
long nCount1 = 0;
|
int LastSendLength = 0;
|
unsigned char databuf1[256];
|
for (; MyThreadProc1ToRun == 1;)
|
{
|
// AppendText("H");
|
nCount1++;
|
if (BulkSendTest == 1)
|
{
|
//nDataToSend += 400;
|
nDataToSend = LastSendLength;
|
// s1.Format(_T("BulkToSend %d Bytes"), nDataToSend);
|
// SysLog(s1);
|
dwSent = MyHvSerialPort1.Send((char *)send1, nDataToSend);
|
s1.Format(_T("S %d ->"), nDataToSend);
|
for (int i = 0; i < nDataToSend; i++)
|
{
|
s1.AppendFormat(_T("%02X "), send1[i]);
|
}
|
// SysLog(s1);
|
//WriteFile(hCom, send1, nDataToSend, &dwSent, NULL);
|
nDataToSend = 0;
|
//continue;
|
}
|
int SendAndRead = 0;
|
if (nDataToSend > 0)
|
{
|
s1.Format(_T("ToSend %d Bytes"), nDataToSend);
|
SysLog(s1);
|
dwSent = MyHvSerialPort1.Send((char *)send1, nDataToSend);
|
s1.Format(_T("S %d ->"), nDataToSend);
|
for (int i = 0; i < nDataToSend; i++)
|
{
|
s1.AppendFormat(_T("%02X "), send1[i]);
|
}
|
SysLog(s1);
|
SendAndRead = 1;
|
//WriteFile(hCom, send1, nDataToSend, &dwSent, NULL);
|
LastSendLength = nDataToSend;
|
nDataToSend = 0;
|
}
|
// ResetEvent(ovlap2.hEvent);
|
if (SendAndRead == 1)
|
{
|
s1.Format(_T("Start to Read %d "), dNumtoRead);
|
SysLog(s1);
|
}
|
//ReadFile(hCom, recv1, dNumtoRead, &wCount2, &ovlap2);
|
wCount2= MyHvSerialPort1.Recv((char *)recv1, dNumtoRead);
|
// WaitForSingleObject(ovlap2.hEvent,800);
|
// GetOverlappedResult(hCom,&ovlap2,&wCount2,TRUE);
|
if (wCount2 == 0) continue;
|
// continue;
|
int ThisTime = static_cast <int> ( GetTickCountmS() );
|
// s3.Format("%d %d ",recv2length,wCount2);AppendText(s3);
|
nTotalRecv += wCount2;
|
nThisRecv += wCount2;
|
s1.Format(_T("R %d ->"), wCount2);
|
for (DWORD i = 0; i < wCount2; i++)
|
{
|
s1.AppendFormat(_T("%02X "), recv1[i]);
|
}
|
unsigned char nCmd, nStatus;
|
unsigned short nCount;
|
|
s1.Append(_T("\r\n"));
|
// SysLog(s1);
|
//continue;
|
MyKLProtocol1.ParseRplyPacket(recv1, wCount2, &nCmd, &nStatus, &nCount, databuf1);
|
|
s1.Format(_T("R-> Cmd:%02X Status:%04X Count:%02d \r\nData: "), nCmd, nStatus, nCount);
|
s2.Empty();
|
for (int i = 0; i < nCount; i++)
|
{
|
s2.AppendFormat(_T("%02X "), databuf1[i]);
|
}
|
// SetDlgItemText(IDC_EDIT_DATA2, s2);
|
s1.Append(s2);
|
s1.Append(_T("\r\n"));
|
SysLog(s1);
|
int HasData = MyDataParser1.PutIn(recv1, wCount2);
|
CStringA sA1;
|
TToAnsi(s1, sA1);
|
// MyAnsiParser1.PutIn((unsigned char *)sA1.GetBuffer(), sA1.GetLength()); sA1.ReleaseBuffer();
|
// MyAnsiParser1.PutIn(recv1, wCount2);
|
if (HasData)
|
{
|
MyDataParser1.GetOutput(&handx, &handy);
|
if (oldhandx != handx || oldhandy != handy)
|
{
|
DrawCross(hDrawXY, handx, handy);
|
oldhandx = handx;
|
oldhandy = handy;
|
}
|
|
// m_edit2.SetSel(currenty*82+currentx,currenty*82+currentx+1,FALSE);
|
s1.Format(_T("X=%3d(%2d%%) Y=%3d(%2d%%)"), handx, handx * 100 / 256, handy, handy * 100 / 256);
|
SetDlgItemText(IDC_STATIC_STAT_XY, s1);
|
// GetWindowDlg(IDC_STATIC5)->SetWindowText("X: Y:");
|
}
|
nRunCount++;
|
|
if (ThisTime - LastSpeedTime > 500)
|
{
|
CurSpeed = (double)nThisRecv / (ThisTime - LastSpeedTime);
|
nThisRecv = 0;
|
LastSpeedTime = ThisTime;
|
}
|
s1.Format(_T("Runing %d \r %.3fkBps"), nRunCount,CurSpeed);
|
SetDlgItemText(IDC_STATIC_STATUS_CON, s1);
|
//SysLog(_T("."));
|
|
}
|
SysLog(_T("跳出接收循环\r\n"));
|
// hcurDC.SelectObject(def_font);
|
// CloseHandle(hCom);
|
SysLog(_T("线程结束运行\r\n\r\n"));
|
MyThreadProc1Running = 0;
|
return 0;
|
}
|
|
UINT CMultiTerminal1Dlg::MyJumper1(LPVOID pParam) //线程跳板
|
{
|
CMultiTerminal1Dlg *pInput = (CMultiTerminal1Dlg *)pParam; //反跳到真正的执行程序
|
return pInput->MyThreadProc1(0);
|
}
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonConnect()
|
{
|
OnMenuOnline();
|
if (!m_bResourceOpened)
|
{
|
return;
|
}
|
m_bMonitoring = false;
|
// TODO: 在此添加控件通知处理程序代码
|
}
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonDisconnect()
|
{
|
OnMenuOffline();
|
m_bMonitoring = false;
|
if (!m_bResourceOpened)
|
{
|
return;
|
}
|
// TODO: 在此添加控件通知处理程序代码
|
}
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonStart()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
OnMenuOnline();
|
if (! m_bResourceOpened)
|
{
|
return;
|
}
|
m_bMonitoring = false;
|
OnMenuMonitor();
|
return;
|
if (MyThreadProc1ToRun == 1 || MyThreadProc1Running == 1)
|
{
|
SysLog(_T("线程在运行中,不能再次运行\r\n"));
|
}
|
else
|
{
|
MyThreadProc1ToRun = 1;
|
AfxBeginThread(MyJumper1, (LPVOID)this);
|
}
|
|
}
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonStop()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
m_bMonitoring = true;
|
OnMenuMonitor();
|
// OnMenuOffline();
|
|
return;
|
if (MyThreadProc1Running)
|
{
|
if (MyThreadProc1ToRun)
|
{
|
SysLog(_T("停止线程运行\r\n"));
|
MyThreadProc1ToRun = 0;
|
}
|
else
|
{
|
SysLog(_T("线程停止中\r\n"));
|
}
|
}
|
else
|
{
|
SysLog(_T("线程未运行\r\n"));
|
}
|
MyHvSerialPort1.Close();
|
}
|
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonClrscr()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
CString s1;
|
s1.Format(_T("Clear Screen \r\n"));
|
SysLog(s1);
|
MyAnsiParser1.ClrScreen();
|
}
|
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonSend()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
|
CString s1,sInput;
|
//get text
|
m_combo_input.GetWindowText(sInput);
|
int j = m_combo_input.SelectString(0, sInput);
|
if (j == -1)
|
{
|
m_combo_input.InsertString(-1, sInput);
|
int k = m_combo_input.GetCount();
|
m_combo_input.SetCurSel(k-1);
|
}
|
else
|
{
|
m_combo_input.GetWindowText(s1);
|
if (s1 != sInput)
|
{
|
m_combo_input.InsertString(-1, sInput);
|
int k = m_combo_input.GetCount();
|
m_combo_input.SetCurSel(k - 1);
|
}
|
}
|
int res = 0;
|
|
// send text;
|
|
if (nDataToSend == 0)
|
{
|
CStringA sA1;
|
TToAnsi(sInput, sA1);
|
int k = sA1.GetLength();
|
memcpy(send1, sA1.GetBuffer(), k);
|
sA1.ReleaseBuffer();
|
nDataToSend = k;
|
res = 1;
|
}
|
else
|
{
|
|
}
|
s1.Format(_T("Send Cmd %s %d %d\r\n"),sInput,sInput.GetLength(),res);
|
SysLog(s1);
|
|
}
|
|
void CMultiTerminal1Dlg::OnCbnSelchangeComboRes()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
}
|
|
void CMultiTerminal1Dlg::OnCbnEditchangeComboInput()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
CString s1;
|
// s1.Format(_T("Combo_Input Edit Changed \r\n"));
|
// SysLog(s1);
|
}
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonDo()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
unsigned char buf1[256];
|
unsigned char buf2[256];
|
unsigned char Data1[256];
|
unsigned char Data2[256];
|
CString s1, sInput;
|
CString MAddrStr;
|
CString CMDstr;
|
CString TypeStr;
|
CString DAddrStr;
|
CString DCountStr;
|
CString Data1Str;
|
|
GetDlgItemText(IDC_COMBO_MADDR, MAddrStr);
|
GetDlgItemText(IDC_COMBO_CMD, CMDstr);
|
GetDlgItemText(IDC_COMBO_CMD_TYPE, TypeStr);
|
GetDlgItemText(IDC_COMBO_DADDR, DAddrStr);
|
GetDlgItemText(IDC_COMBO_DCOUNT, DCountStr);
|
GetDlgItemText(IDC_EDIT_DATA1, Data1Str);
|
|
int DstAddr = _tstoi(MAddrStr);
|
int Cmd = _tstoi(CMDstr);
|
int DataType = _tstoi(TypeStr);
|
int DAddr = _tstoi(DAddrStr);
|
int DCount = _tstoi(DCountStr);
|
|
CString Resultstrs[100];
|
int j = Split(Data1Str, _T(" "), Resultstrs);
|
for (int i = 0; i < j; i++)
|
{
|
Data1[i] = Xtoi(Resultstrs[i]);
|
}
|
|
|
int bExtData = ((CButton *)GetDlgItem(IDC_CHECK_EXT_DATA))->GetCheck();
|
CString ExtDataDstStr;
|
CString ExtDataTypeStr;
|
CString ExtDataLenStr;
|
CString ExtDataStr;
|
GetDlgItemText(IDC_COMBO_EXT_DST, ExtDataDstStr);
|
GetDlgItemText(IDC_COMBO_EXT_TYPE, ExtDataTypeStr);
|
GetDlgItemText(IDC_COMBO_EXT_DATA_LEN, ExtDataLenStr);
|
GetDlgItemText(IDC_EDIT_EXT_DATA1, ExtDataStr);
|
|
int ExtDataDst = _tstoi(ExtDataDstStr);
|
int ExtDataType = _tstoi(ExtDataTypeStr);
|
int ExtDataLen = _tstoi(ExtDataLenStr);
|
|
j = Split(ExtDataStr, _T(" "), Resultstrs);
|
for (int i = 0; i < j; i++)
|
{
|
Data2[i] = Xtoi(Resultstrs[i]);
|
}
|
|
int ExtFrameLen = MyKLProtocol1.MakeExtDataFrame(buf2, ExtDataDst, ExtDataType, ExtDataLen,Data2 );
|
|
KLinkProtocol::unKLStat nStat1 = { 0 };
|
nStat1.HasExt = bExtData;
|
nStat1.nSEQ = MyKLProtocol1.GetNextSeq();
|
// Cmd += 0x30; //KLinkProtocol::cmdRead
|
int len1=MyKLProtocol1.MakeReqPacketEx(buf1, DstAddr, nStat1.StatByte, Cmd , DataType, DAddr, DCount,Data1, ExtFrameLen, buf2);
|
s1.Format(_T("S %d ->"), len1);
|
int res = 0;
|
int SendType = 1;
|
if (SendType == 1)
|
{
|
res = MyHvSerialPort1.Send((char *)buf1, len1);
|
for (int i = 0; i < len1; i++)
|
{
|
s1.AppendFormat(_T("%02X "), buf1[i]);
|
}
|
|
}
|
else if (SendType == 2)
|
{
|
if (nDataToSend == 0)
|
{
|
int k = len1;
|
memcpy(send1, buf1, k);
|
nDataToSend = k;
|
res = 1;
|
}
|
}
|
s1.AppendFormat(_T(" = %d\r\n"), res);
|
SysLog(s1);
|
|
DWORD wCount2;
|
DWORD dNumtoRead = DCount + 6;;
|
int numToRead = dNumtoRead;
|
int len2 = 0;
|
int nTryCount = 0;
|
int nTryCount2 = 0;
|
// 在没有收到数据的情况向,连续重试10次,在收到数据后,又没有数据,再重试3次。
|
for (int i = 0; i < 10; i++)
|
{
|
nTryCount++;
|
int len = MyHvSerialPort1.Recv((char *)recv1 + len2, numToRead - len2);
|
len2 += len;
|
if (len2 >= numToRead) break;
|
if (len2 >0 ) {
|
nTryCount2++;
|
if (len > 0) { nTryCount2 = 0; }
|
if (nTryCount2 >= 1) break;
|
}
|
}
|
|
//wCount2 = MySerialCom1.Read((char *)recv1, dNumtoRead);
|
wCount2 = len2;
|
// WaitForSingleObject(ovlap2.hEvent,800);
|
// GetOverlappedResult(hCom,&ovlap2,&wCount2,TRUE);
|
//if (wCount2 == 0) continue;
|
// continue;
|
int ThisTime = static_cast <int> (GetTickCountmS());
|
// s3.Format("%d %d ",recv2length,wCount2);AppendText(s3);
|
|
s1.Format(_T("R %d(Try %d %d)=%d->"), dNumtoRead,nTryCount,nTryCount2,wCount2);
|
for (DWORD i = 0; i<8 && i < wCount2; i++)
|
{
|
s1.AppendFormat(_T("%02X "), recv1[i]);
|
}
|
s1.Append(_T("... \r\n"));
|
SysLog(s1);
|
unsigned char nCmd=0, nStatus=0;
|
unsigned short nCount=0;
|
|
|
//continue;
|
unsigned short databuf1[256];
|
MyKLProtocol1.ParseRplyPacket(recv1, wCount2, &nCmd, &nStatus, &nCount, databuf1);
|
|
s1.Format(_T("R: Cmd:%02X St:%02X N:%02d D:\r\n"), nCmd, nStatus, nCount);
|
CString s2;
|
s2.Empty();
|
|
for (int i = 0; i < (nCount + 15) / 16; i++)
|
{
|
s2.AppendFormat(_T("%02X: "), i * 16);
|
for (int j = 0; j < 8 && i * 8 + j < nCount/2 ; j++)
|
{
|
s2.AppendFormat(_T("%04X"), databuf1[i * 8 + j]);
|
if (j == 3) { s2.Append(_T(" ")); }
|
else { s2.Append(_T(" ")); }
|
}
|
s2.Append(_T("\r\n"));
|
}
|
|
// for (int i = 0; i < nCount; i++)
|
// {
|
// s2.AppendFormat(_T("%02X "), databuf1[i]);
|
// }
|
// SetDlgItemText(IDC_EDIT_DATA2, s2);
|
s1.Append(s2);
|
// s1.Append(_T("\r\n"));
|
SysLog(s1);
|
|
}
|
|
void CMultiTerminal1Dlg::OnCbnSelchangeComboCmdType()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
}
|
|
|
void CMultiTerminal1Dlg::OnCbnSelchangeComboDcount()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
}
|
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonStartBulkTest()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
BulkSendTest = 1;
|
|
}
|
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonStopBulkTest()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
BulkSendTest = 0;
|
}
|
|
void CMultiTerminal1Dlg::OnMenuCommset()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogCommSet1 dialog1;
|
dialog1.DoModal();
|
|
}
|
|
void CMultiTerminal1Dlg::OnMenuDownload()
|
{
|
// TODO: 在此添加命令处理程序代码
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuUpload()
|
{
|
// TODO: 在此添加命令处理程序代码
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuOnline()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CString s1;
|
s1.Format(_T("切换在线"));
|
SysLog(s1);
|
if (m_bOnline)
|
{
|
SysLog(_T("已经在线"));
|
return;
|
}
|
int r = OpenResource("");
|
if (r) {
|
m_bOnline = true;
|
m_wndToolBar.GetToolBarCtrl().CheckDlgButton(ID_MENU_ONLINE, m_bOnline);
|
// ((CButton *)m_wndToolBar.GetDlgItem(ID_MENU_ONLINE))->SetCheck(m_bOnline);
|
|
if (m_bOnline) {
|
m_wndToolBar.SetButtonStyle(17, TBBS_CHECKBOX);
|
m_wndToolBar.SetButtonStyle(18, TBBS_CHECKED);
|
}
|
|
}
|
else {
|
m_wndToolBar.SetButtonStyle(17, TBBS_CHECKED);
|
m_wndToolBar.SetButtonStyle(18, TBBS_CHECKBOX);
|
}
|
|
return;
|
}
|
|
void CMultiTerminal1Dlg::OnUpdateMenuOnline(CCmdUI *pCmdUI)
|
{
|
// TODO: 在此添加命令更新用户界面处理程序代码
|
pCmdUI->SetCheck(m_bOnline);
|
m_wndToolBar.GetToolBarCtrl().CheckDlgButton(ID_MENU_ONLINE, m_bOnline);
|
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuOffline()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CString s1;
|
m_bOnline = false;
|
m_wndToolBar.CheckDlgButton(ID_MENU_ONLINE, m_bOnline);
|
s1.Format(_T("切换离线"));
|
SysLog(s1);
|
CloseResource();
|
if (m_bOnline) {
|
m_wndToolBar.SetButtonStyle(17, TBBS_CHECKBOX);
|
m_wndToolBar.SetButtonStyle(18, TBBS_CHECKED);
|
}
|
else {
|
m_wndToolBar.SetButtonStyle(17, TBBS_CHECKED);
|
m_wndToolBar.SetButtonStyle(18, TBBS_CHECKBOX);
|
}
|
return;
|
|
if (MyThreadProc1Running)
|
{
|
if (MyThreadProc1ToRun)
|
{
|
SysLog(_T("停止线程运行\r\n"));
|
MyThreadProc1ToRun = 0;
|
}
|
else
|
{
|
SysLog(_T("线程停止中\r\n"));
|
}
|
}
|
else
|
{
|
SysLog(_T("线程未运行\r\n"));
|
}
|
MyHvSerialPort1.Close();
|
}
|
|
void CMultiTerminal1Dlg::OnUpdateMenuOffline(CCmdUI *pCmdUI)
|
{
|
// TODO: 在此添加命令更新用户界面处理程序代码
|
pCmdUI->SetCheck(!m_bOnline);
|
}
|
|
void CMultiTerminal1Dlg::OnMenuMonitor()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CString s1;
|
m_bMonitoring = !m_bMonitoring;
|
s1.Format(_T("切换监控 -> %d"), m_bMonitoring);
|
SysLog(s1);
|
if (m_bMonitoring) m_wndToolBar.SetButtonStyle(20, TBBS_CHECKED);
|
else m_wndToolBar.SetButtonStyle(20, TBBS_CHECKBOX);
|
// m_wndToolBar.CheckDlgButton(nID, m_bMonitoring);
|
}
|
|
void CMultiTerminal1Dlg::OnUpdateMenuMonitor(CCmdUI *pCmdUI)
|
{
|
// TODO: 在此添加命令更新用户界面处理程序代码
|
CString s1;
|
pCmdUI->SetCheck(m_bMonitoring);
|
}
|
|
void CMultiTerminal1Dlg::OnMenuRun()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CString s1;
|
m_bRunning = !m_bRunning;
|
s1.Format(_T("切换运行 -> %d "), m_bRunning);
|
SysLog(s1);
|
if (m_bRunning) {
|
m_wndToolBar.SetButtonStyle(19, TBBS_CHECKED);
|
}
|
else {
|
m_wndToolBar.SetButtonStyle(19, TBBS_CHECKBOX);
|
}
|
}
|
|
|
void CMultiTerminal1Dlg::OnUpdateMenuRun(CCmdUI *pCmdUI)
|
{
|
// TODO: 在此添加命令更新用户界面处理程序代码
|
pCmdUI->SetCheck(m_bRunning);
|
}
|
|
|
|
void CMultiTerminal1Dlg::OnMenuSysregSet()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogSysRegSet dialog1;
|
|
INT_PTR r=dialog1.DoModal();
|
|
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuStatusShow()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogStatusShow dialog1;
|
INT_PTR r = dialog1.DoModal();
|
|
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuInfoDisplay()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogInfoDisplay dialog1;
|
INT_PTR r = dialog1.DoModal();
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuCoilMon()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogCoilMon dialog1;
|
INT_PTR r = dialog1.DoModal();
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuDataMon()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogDataMon dialog1;
|
INT_PTR r = dialog1.DoModal();
|
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuCommTool()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogCommTest dialog1;
|
INT_PTR r = dialog1.DoModal();
|
|
}
|
|
|
BOOL CMultiTerminal1Dlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
|
{
|
// TODO: 在此添加消息处理程序代码和/或调用默认值
|
|
return CDialogEx::OnMouseWheel(nFlags, zDelta, pt);
|
}
|
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonClearStat()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
Clear_COM_Stats();
|
}
|
|
void CMultiTerminal1Dlg::OnBnClickedButtonRead()
|
{
|
//CDialogProgress dialog1;
|
//INT_PTR r = dialog1.DoModal();
|
|
CDialogProgress * pdialog1 = new CDialogProgress();
|
pdialog1->Create(IDD_DIALOG_PROGRESS, this);
|
pdialog1->ShowWindow(SW_SHOW);
|
pdialog1->UpdateWindow();
|
for (int i = 0; i <= 200; i++)
|
{
|
pdialog1->SetPos(i / 2);
|
// pdialog1->UpdateWindow();
|
// DoEvents();
|
Sleep(50);
|
|
}
|
// pdialog1->SetPos(99);
|
// pdialog1->UpdateWindow();
|
for (int i = 0; i < 10; i++)
|
{
|
DoEvents();
|
Sleep(10);
|
}
|
|
pdialog1->DestroyWindow();
|
delete pdialog1; pdialog1 = NULL;
|
|
|
// TODO: 在此添加控件通知处理程序代码
|
}
|
|
|
|
|
void CMultiTerminal1Dlg::OnMenuEventLog()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogEventLog dialog1;
|
INT_PTR r = dialog1.DoModal();
|
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuCommTest()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogCommTest dialog1;
|
INT_PTR r = dialog1.DoModal();
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuForceIo()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogForceIO dialog1;
|
INT_PTR r = dialog1.DoModal();
|
}
|
|
|
void CMultiTerminal1Dlg::OnMenuDatetime()
|
{
|
// TODO: 在此添加命令处理程序代码
|
CDialogDateTime dialog1;
|
INT_PTR r = dialog1.DoModal();
|
}
|
|
typedef unsigned char uint8_t;
|
typedef unsigned short uint16_t;
|
typedef unsigned int uint32_t;
|
typedef int int32_t;
|
|
const uint16_t polynom = 0xA001;
|
|
uint16_t crc16bitbybit(uint8_t *ptr, uint16_t len)
|
{
|
uint8_t i;
|
uint16_t crc = 0xffff;
|
|
if (len == 0) {
|
len = 1;
|
}
|
while (len--) {
|
crc ^= *ptr;
|
for (i = 0; i < 8; i++)
|
{
|
if (crc & 1) {
|
crc >>= 1;
|
crc ^= polynom;
|
}
|
else {
|
crc >>= 1;
|
}
|
}
|
ptr++;
|
}
|
return(crc);
|
}
|
|
/* Table of CRC values for high–order byte */
|
const uint8_t crctablehi[] = {
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
|
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
|
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
|
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
|
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
|
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
|
0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
|
0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
|
0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
|
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
|
0x40
|
};
|
/* Table of CRC values for low–order byte */
|
const uint8_t crctablelo[] = {
|
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
|
0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
|
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
|
0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
|
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
|
0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
|
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
|
0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
|
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
|
0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
|
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
|
0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
|
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
|
0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
|
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
|
0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
|
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
|
0x40
|
};
|
|
uint16_t crc16table(uint8_t *ptr, uint16_t len)
|
{
|
uint8_t crchi = 0xff;
|
uint8_t crclo = 0xff;
|
uint16_t index;
|
while (len--)
|
{
|
index = crclo ^ *ptr++;
|
crclo = crchi ^ crctablehi[index];
|
crchi = crctablelo[index];
|
}
|
return (crchi << 8 | crclo);
|
}
|
|
const uint16_t crctalbeabs[] = {
|
0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
|
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
|
};
|
|
uint16_t crc16tablefast(uint8_t *ptr, uint16_t len)
|
{
|
uint16_t crc = 0xffff;
|
uint16_t i;
|
uint8_t ch;
|
|
for (i = 0; i < len; i++) {
|
ch = *ptr++;
|
crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4);
|
crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
|
}
|
|
return crc;
|
}
|
|
void modbuscrc16test()
|
{
|
printf("\n");
|
printf(" Modbus CRC16 tester\n");
|
printf("-----------------------------------------------------------------------\n");
|
uint8_t crc16_data[] = { 0x01, 0x04, 0x04, 0x43, 0x6b, 0x58, 0x0e }; // expected crc value 0xD825.
|
printf(" modbus crc16table test, expected value : 0xd825, calculate value : 0x%x\n", crc16table(crc16_data, sizeof(crc16_data)));
|
printf(" modbus crc16tablefast test, expected value : 0xd825, calculate value : 0x%x\n", crc16tablefast(crc16_data, sizeof(crc16_data)));
|
printf(" modbus crc16bitbybit test, expected value : 0xd825, calculate value : 0x%x\n", crc16bitbybit(crc16_data, sizeof(crc16_data)));
|
}
|
|
|
void CMultiTerminal1Dlg::OnBnClickedButton11()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
uint8_t crc16_data[] = { 0x01, 0x04, 0x04, 0x43, 0x6b, 0x58, 0x0e }; // expected crc value 0xD825.
|
|
uint16_t crc1,crc2,crc3;
|
double time1 = GetTimeMs();
|
for (int i = 0; i < 10000; i++)
|
crc1 = crc16table(crc16_data, sizeof(crc16_data));
|
double time2 = GetTimeMs();
|
for (int i = 0; i < 10000; i++)
|
crc2=crc16tablefast(crc16_data, sizeof(crc16_data));
|
double time3 = GetTimeMs();
|
for (int i = 0; i < 10000; i++)
|
crc3=crc16bitbybit(crc16_data, sizeof(crc16_data));
|
double time4 = GetTimeMs();
|
CString s1;
|
s1.Format(_T("crc results \r\n"));
|
s1.AppendFormat(_T("crc result1 %x time %f \r\n"), crc1, time2 - time1);
|
s1.AppendFormat(_T("crc result2 %x time %f \r\n"), crc2, time3 - time2);
|
s1.AppendFormat(_T("crc result3 %x time %f \r\n"), crc3, time4 - time3);
|
SysLog(s1);
|
}
|
|
|
void CMultiTerminal1Dlg::OnBnClickedButton12()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
CString s1;
|
MyHvSerialPort1.SetParams(1, 115200, _T("8-N-1"));
|
int r = MyHvSerialPort1.Open();
|
s1.Format(_T("%d %s"), r, MyHvSerialPort1.m_strResult);
|
SysLog(s1);
|
}
|
|
|
void CMultiTerminal1Dlg::OnBnClickedButton13()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
CString s1;
|
int r = MyHvSerialPort1.Close();
|
s1.Format(_T("%d %s"), r, MyHvSerialPort1.m_strResult);
|
SysLog(s1);
|
}
|