?? 電力諧波測試儀view.cpp
字號:
// 電力諧波測試儀View.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "電力諧波測試儀.h"
#include "Di.h"
#include "電力諧波測試儀Doc.h"
#include "電力諧波測試儀View.h"
#include "Exeditpinpu.h"
#include "Exeditthd.h"
#include "Exeditu.h"
#include "Exeditzhutu.h"
#include "MyPropertySheet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char hextable[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
#define TOHEX(a, b) {*b++ = hextable[a >> 4];*b++ = hextable[a&0xf];}
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CFormView)
BEGIN_MESSAGE_MAP(CMyView, CFormView)
//{{AFX_MSG_MAP(CMyView)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
: CFormView(CMyView::IDD)
{
//{{AFX_DATA_INIT(CMyView)
//}}AFX_DATA_INIT
// TODO: add construction code here
bReceive=false;
m_nPort=1;
m_strSettings=_T("4800,n,8,1");
m_strSendString="a5";
m_nTime=1000;
}
CMyView::~CMyView()
{
}
void CMyView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyView)
DDX_Control(pDX, IDC_EDIT6, m_12);
DDX_Control(pDX, IDC_EDIT5, m_abc);
DDX_Control(pDX, IDC_SSTAB2, m_tab2);
DDX_Control(pDX, IDC_SSTAB3, m_tab3);
DDX_Control(pDX, IDC_SSTAB4, m_tab4);
DDX_Control(pDX, IDC_MSCOMM1, m_Com);
DDX_Control(pDX, IDC_TOGGLEBUTTON1, m_start);
DDX_Control(pDX, IDC_TOGGLEBUTTON2, m_end);
DDX_Control(pDX, IDC_SSTAB6, m_tab1);
//}}AFX_DATA_MAP
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CMyView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
OpenPort();
Di di;
m_eua.SubclassDlgItem(IDC_EDIT1,this);
m_epinpub.SubclassDlgItem(IDC_EDIT3,this);
m_ezhutuc.SubclassDlgItem(IDC_EDIT4,this);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CFormView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
BEGIN_EVENTSINK_MAP(CMyView, CFormView)
//{{AFX_EVENTSINK_MAP(CMyView)
ON_EVENT(CMyView, IDC_TOGGLEBUTTON1, -610 /* Click */, OnClickTogglebutton1, VTS_NONE)
ON_EVENT(CMyView, IDC_TOGGLEBUTTON2, -610 /* Click */, OnClickTogglebutton2, VTS_NONE)
ON_EVENT(CMyView, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
ON_EVENT(CMyView, IDC_SSTAB2, -600 /* Click */, OnClickSstab2, VTS_I2)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CMyView::OnClickTogglebutton1()
{
// TODO: Add your control notification handler code here
m_Com.GetInput();//先預讀緩沖區(qū)以清除殘留數(shù)據(jù)
bReceive = true;
SetTimer(1,m_nTime,NULL);//時間為1000毫秒
}
void CMyView::OnClickTogglebutton2()
{
// TODO: Add your control notification handler code here
bReceive = !bReceive;
if(bReceive)
{
SetTimer(1,m_nTime,NULL);//時間為1000毫秒
}
else
{
KillTimer(1); //取消定時
}
}
void CMyView::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //設置BYTE數(shù)組 An 8-bit integerthat is not signed.i
if(bReceive)
{
if(m_Com.GetInBufferCount()==68)
{
variant_inp = m_Com.GetInput(); //讀緩沖區(qū)
safearray_inp = variant_inp; //VARIANT型變量轉換為ColeSafeArray型變量
len=safearray_inp.GetOneDimSize(); //得到有效數(shù)據(jù)長度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//轉換為BYTE
m_length=len;
for(k=0;k<len;k++)
bt[k]=rxdata[k];
m_abc.get(atoibase2(0));
CString ua;
ua.Format("%4.1f",atoibase2(0));
ua+="V";
m_12.SetWindowText(ua);
m_eua.get(atoibase2(0));
}
}
UpdateData(FALSE);
}
void CMyView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CByteArray hexdata;
int len=String2Hex(m_strSendString ,hexdata);
m_Com.SetOutput(COleVariant(hexdata));
CFormView::OnTimer(nIDEvent);
}
int CMyView::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]=(char)hexdata;
hexdatalen++;
}
senddata.SetSize(hexdatalen);
return hexdatalen;
}
char CMyView::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);
}
void CMyView::OpenPort()
{
if(m_Com.GetPortOpen())
m_Com.SetPortOpen(FALSE);
m_Com.SetCommPort(m_nPort); //選擇com1
if( !m_Com.GetPortOpen())
m_Com.SetPortOpen(TRUE);//打開串口
else
AfxMessageBox("cannot open serial port");
m_Com.SetSettings(m_strSettings); //波特率9600,無校驗,8個數(shù)據(jù)位,1個停止位
m_Com.SetRThreshold(1);
//參數(shù)1表示每當串口接收緩沖區(qū)中有多于或等于1個字符時將引發(fā)一個接收數(shù)據(jù)的OnComm事件
m_Com.SetInputMode(1);
m_Com.SetInputLen(0); //設置當前接收區(qū)數(shù)據(jù)長度為0
m_Com.GetInput();//先預讀緩沖區(qū)以清除殘留數(shù)據(jù)
}
float CMyView::atoibase2(int i)
{
char buf[2048];long j,n;float b1,b2,b3,b4;
for( j=0;j<m_length;)
{
char *p=&buf[0];
for( n=0;(n<98)&&(j<m_length);n++)
{
TOHEX(bt[j+i],p);
j++;
}
}
b1=ConvertHexChar(buf[0]);
b2=ConvertHexChar(buf[1]);
b3=ConvertHexChar(buf[2]);
b4=ConvertHexChar(buf[3]);
return b1*16*16+b2*16+b3+b4/16;
}
float CMyView::atoibase(int i)
{
char buf[2048];long j,n;float b1,b2,b3,b4;
for( j=0;j<m_length;)
{
char *p=&buf[0];
for( n=0;(n<98)&&(j<m_length);n++)
{
TOHEX(bt[j+i],p);
j++;
}
}
b1=ConvertHexChar(buf[0]);
b2=ConvertHexChar(buf[1]);
b3=ConvertHexChar(buf[2]);
b4=ConvertHexChar(buf[3]);
return b1*16*16*16+b2*16*16+b3*16+b4;
}
void CMyView::OnClickSstab2(short PreviousTab)
{
// TODO: Add your control notification handler code here
// PreviousTab=m_tab2.GetTab();
if(PreviousTab==1)
{
m_tab2.SetTab(1);
m_eua.SubclassDlgItem(IDC_EDIT1,this);
m_eua.get(34);
}
if(PreviousTab==2)
{
m_tab2.SetTab(2);
m_epinpua.SubclassDlgItem(IDC_EDIT1,this);
// m_epinpua
}
if(PreviousTab==3)
{
m_tab2.SetTab(3);
m_ethda.SubclassDlgItem(IDC_EDIT1,this);
// m_ethda.f(2);
}
}
void CMyView::OnProperties()
{
// TODO: The property sheet attached to your project
// via this function is not hooked up to any message
// handler. In order to actually use the property sheet,
// you will need to associate this function with a control
// in your project such as a menu item or tool bar button.
CMyPropertySheet propSheet;
propSheet.DoModal();
// This is where you would retrieve information from the property
// sheet if propSheet.DoModal() returned IDOK. We aren't doing
// anything for simplicity.
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -