📄 workdlg.cpp
字号:
// workDlg.cpp : implementation file
//
#include "stdafx.h"
#include "work.h"
#include "workDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define _UNICODE
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CWorkDlg dialog
CWorkDlg::CWorkDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWorkDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWorkDlg)
m_editsend = _T("");
m_editaccept = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
count=0;
strposition=0;
}
void CWorkDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWorkDlg)
DDX_Control(pDX, IDC_MSCOMM1, m_mscomm);
DDX_Text(pDX, IDC_EDIT_SENDDATA, m_editsend);
DDX_Text(pDX, IDC_EDIT_ACCEPTDATA, m_editaccept);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWorkDlg, CDialog)
//{{AFX_MSG_MAP(CWorkDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_INITIALIZE, OnButtonInitialize)
ON_BN_CLICKED(IDC_BUTTON_CLEARDATA, OnButtonCleardata)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_INQUIRE, OnButtonInquire)
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWorkDlg message handlers
BOOL CWorkDlg::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
if(m_mscomm.GetPortOpen())
m_mscomm.SetPortOpen(FALSE);
m_mscomm.SetCommPort(1); //选择com1
if( !m_mscomm.GetPortOpen())
m_mscomm.SetPortOpen(TRUE);//打开串口
else
AfxMessageBox("cannot open serial port");
m_mscomm.SetSettings("57600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位
m_mscomm.SetInputMode(1);
m_mscomm.SetRThreshold(1);
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
m_mscomm.SetInputLen(0);//设置当前接收区数据长度为0
m_mscomm.GetInput();//
// m_mscomm.SetCTSHolding(true);
return TRUE; // return TRUE unless you set the focus to a control
}
void CWorkDlg::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 CWorkDlg::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 CWorkDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWorkDlg::OnButtonSend()
{
int length;
CByteArray hexdata;
UpdateData(true);
length=m_editsend.GetLength();
CString str0,str;
if(!linkhandle.IsEmpty())
{
str=linkhandle[2];
str+=linkhandle[3];
}
str0="02"+str+"20";
int len=String2Hex(str0,hexdata);
hexdata.Add(length+4);
// int th=hexdata.GetSize();
// TRACE("th=%d\n",th);
hexdata.Add(0);
hexdata.Add(length);
hexdata.Add(0);
hexdata.Add(0);
hexdata.Add(0);
for(int i=0;i<length;i++)
{
int k=m_editsend[i];
hexdata.Add(k);
}
// CString str("02 01 20 0a 00 06 00 00 00 68 65 6c 6c 6f 21");
m_mscomm.SetOutput(COleVariant(hexdata));//发送数据
// for(int k=0;k<hexdata.GetSize();k++)
// TRACE("hexdata=%d ",hexdata(k));
}
int CWorkDlg::ConvertHexChar(char ch)
{
if((ch>='0')&&(ch<='9'))
return ch-0x30;
else if((ch>='A')&&(ch<='F'))
return ch-'A'+10;
else if((ch>='a')&&(ch<='f'))
return ch-'a'+10;
else
return (-1);
}
int CWorkDlg::String2Hex(CString str, CByteArray &senddata)
{
int hexdata,lowhexdata;
int hexdatalen=0;
int len=str.GetLength();
senddata.SetSize(len/2);
for(int i=0;i<len;)
{
char lstr,hstr=str[i];
if(hstr==' ')
{
i++;
continue;
}
i++;
if(i>=len)
break;
lstr=str[i];
hexdata=ConvertHexChar(hstr);
lowhexdata=ConvertHexChar(lstr);
if((hexdata==16)||(lowhexdata==16))
break;
else
hexdata=hexdata*16+lowhexdata;
i++;
senddata[hexdatalen]=hexdata;
// TRACE("senddata=%d ",senddata[hexdatalen]);
hexdatalen++;
}
senddata.SetSize(hexdatalen);
return hexdatalen;
}
BEGIN_EVENTSINK_MAP(CWorkDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CWorkDlg)
ON_EVENT(CWorkDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CWorkDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
CString strtemp,str,stradd;
if(m_mscomm.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{ ////////以下你可以根据自己的通信协议加入处理代码
safearray_inp.Clear();
variant_inp=m_mscomm.GetInput(); //读缓冲区
safearray_inp=variant_inp;//VARIANT型变量转换为ColeSafeArray型变量
len=safearray_inp.GetOneDimSize();//得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
for(k=0;k<len;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(rxdata+k);
strtemp.Format("%02X",bt); //将字符送入临时变量strtemp存放
str+=strtemp; //加入接收编辑框对应字符串
afxDump<<strtemp<<" ";
}
afxDump<<"\n";
strsum+=str;
TRACE("strsum=%s\n",strsum);
stradd=ExtractStr(strsum,strposition);
TRACE("stradd=%s\n",stradd);
if(stradd.Compare(" "))
{
strarray.Add(stradd);
CString InquandHandle,inqumesstr("the device address is "),handlestr("the link handle=");
InquandHandle=stradd[2];
InquandHandle+=stradd[3];
if(!InquandHandle.Compare("02"))
{
for(int i=8;i<20;i++)
address+=stradd[i];
inqumesstr+=address;
MessageBox(inqumesstr,"Inquire");
}
if(!InquandHandle.Compare("03"))
{
linkhandle=stradd[10];
linkhandle+=stradd[11];
linkhandle+=stradd[8];
linkhandle+=stradd[9];
handlestr+=linkhandle;
for(int num=12;num<24;num++)
address+=stradd[num];
MessageBox(handlestr,"HANDLE");
}
}
}
/* if ((rxdata[0]==2)&&(rxdata[1]==1)&&(rxdata[2]==32))
{
int i=*(rxdata+5);
TRACE("i=%d\n",i);
for(int m=0;m<i;m++)
{
int bb=*(char*)(rxdata+m+1);
CString ss;
ss=(char)bb;
m_editaccept+=ss;
}
}
UpdateData(false);
*/
/*
int size=0;
size=strarray.GetSize();
if(size!=count+1)
{
for(int k=count+1;k<size;k++)
{
strarray[count]+=strarray[k];
}
for(int i=count+1;i<size;i++)
{
strarray.RemoveAt(i);
}
}
*/
}
void CWorkDlg::OnButtonInitialize()
{
// TODO: Add your control notification handler code here
strarray.RemoveAll();
count=0;
Initialize();
SetTimer(1,500,NULL);
}
void CWorkDlg::OnCancel()
{
// TODO: Add extra cleanup here
TRACE("strarray output\n");
for(int i=0;i<strarray.GetSize();i++)
afxDump<<strarray[i]<<"\n";
strarray.RemoveAll();
CDialog::OnCancel();
}
void CWorkDlg::OnButtonCleardata()
{
// TODO: Add your control notification handler code here
m_editaccept.Empty();
UpdateData(false);
}
void CWorkDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
count++;
Initialize();
CDialog::OnTimer(nIDEvent);
}
void CWorkDlg::Initialize()
{
CByteArray hexdata;
CStringArray initarray;
CString str0("01 03 0c 00"),str1("01 05 10 00"),
str2("01 05 0c 01 00"),str3("01 1a 0c 01 03"),
str4("01 20 0c 01 00"),str5("01 26 0c 02 60 00"),
str6("01 05 0c 03 02 00 02"),str7("01 16 0c 02 00 20"),
str8("01 18 0c 02 00 30"),str9("01 07 fc 01 03"),
str10("01 09 10 00");
initarray.Add(str0);
initarray.Add(str1);
initarray.Add(str2);
initarray.Add(str3);
initarray.Add(str4);
initarray.Add(str5);
initarray.Add(str6);
initarray.Add(str7);
initarray.Add(str8);
initarray.Add(str9);
initarray.Add(str10);
// for (int i=0;i<initarray.GetSize();i++)
if (count>10) {
KillTimer(1);
}
else
{
int len=String2Hex(initarray[count],hexdata);
m_mscomm.SetOutput(COleVariant(hexdata));//发送数据
}
}
void CWorkDlg::OnButtonInquire()
{
// TODO: Add your control notification handler code here
address.Empty();
linkhandle.Empty();
CString str("01 01 04 05 33 8b 9e 06 00");
CByteArray hexdata;
int len=String2Hex(str,hexdata);
m_mscomm.SetOutput(COleVariant(hexdata));//发送数据
/*
count++;
int size=strarray.GetSize();
CString strtempsum,strtempresult,strtempend,strtempstatus;
int strcount=0;
strtempsum=strarray[size-1];
strtempresult=ExtractStr(strtempsum,strcount);
strcount+=1;
strtempend=ExtractStr(strtempsum,strcount);
strcount+=1;
strtempstatus=ExtractStr(strtempsum,strcount);
strarray.RemoveAt(size-1);
strarray.Add(strtempresult);
strarray.Add(strtempend);
strarray.Add(strtempstatus);
*/
}
void CWorkDlg::OnButtonConnect()
{
// TODO: Add your control notification handler code here
if(address.IsEmpty())
{
MessageBox("can not find device");
return;
}
CString str,str1("01 05 04 0d"),str2("08 00 00 00 00 00 00");
CByteArray hexdata;
str=str1+address+str2;
int len=String2Hex(str,hexdata);
m_mscomm.SetOutput(COleVariant(hexdata));//发送数据
}
CString CWorkDlg::ExtractStr(CString string, int& position)
{
CString stringtemp,strcmp,strview,strhandle,str;
int highhexdata,lowhexdata,decimal,lastpos;
lastpos=position;
for(int i=position;i<position+6;i++)
strcmp+=string[i];
if(!linkhandle.IsEmpty())
{
str=linkhandle[2];
str+=linkhandle[3];
}
strhandle="02"+str+"20";
if(!strcmp.Compare(strhandle))
{
int hhdata,hldata;
hhdata=ConvertHexChar(string[position+12]);
hldata=ConvertHexChar(string[position+13]);
highhexdata=ConvertHexChar(string[position+10]);
lowhexdata=ConvertHexChar(string[position+11]);
decimal=hhdata*16*16*16+hldata*16*16+highhexdata*16+lowhexdata;
TRACE("decimal=%d",decimal);
if((string.GetLength()-position)>=(decimal*2+18))
{
for(;position<decimal*2+18+lastpos;position++)
{
stringtemp+=string[position];
if(position>=lastpos+18)
strview+=string[position];
}
for(int k=0;k<strview.GetLength();k++)
{
CString strconv;
int highint=ConvertHexChar(strview[k]);
int lowint=ConvertHexChar(strview[++k]);
BYTE sum=16*highint+lowint;
strconv+=(unsigned char)sum;
if((sum>0)&&(sum<128))
{
m_editaccept+=(char)sum;
}
else
{
int hhint=ConvertHexChar(strview[++k]);
int hlint=ConvertHexChar(strview[++k]);
BYTE sumup=hhint*16+hlint ;
strconv+=(unsigned char)sumup;
USES_CONVERSION;
m_editaccept+=A2W(strconv);
}
}
UpdateData(false);
int length=strsum.Delete(0,strposition);
strposition=0;
return stringtemp;
}
else
{
int length=strsum.Delete(0,strposition);
strposition=0;
return " ";
}
}
highhexdata=ConvertHexChar(string[position+4]);
lowhexdata=ConvertHexChar(string[position+5]);
decimal=highhexdata*16+lowhexdata;
TRACE("decimal=%d",decimal);
if((string.GetLength()-position)>=(decimal*2+6))
{
for(;position<decimal*2+6+lastpos;position++)
stringtemp+=string[position];
return stringtemp;
}
else
{
return " ";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -