📄 activexcommview.cpp
字号:
// ActivexcommView.cpp : implementation of the CActivexcommView class
//
#include "stdafx.h"
#include "Activexcomm.h"
#include <stdlib.h>
#include "ActivexcommDoc.h"
#include "ActivexcommView.h"
#include <afxmt.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CEvent Done;
///MultiThread communication
UINT CommThread(LPVOID param)
{
TRACE("starting communication.......");
CActivexcommView *wnd=(CActivexcommView*)param;
wnd->ThreadEntry();
Done.SetEvent();
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CActivexcommView
IMPLEMENT_DYNCREATE(CActivexcommView, CFormView)
BEGIN_MESSAGE_MAP(CActivexcommView, CFormView)
//{{AFX_MSG_MAP(CActivexcommView)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CActivexcommView construction/destruction
CActivexcommView::CActivexcommView()
: CFormView(CActivexcommView::IDD)
{
//{{AFX_DATA_INIT(CActivexcommView)
m_receive = _T("");
m_youwei = _T("");
m_chesu = _T("");
m_dangrilicheng = 0;
m_signal = _T("");
m_zhuansu = 0;
m_abs = _T("");
m_back = _T("");
m_left = _T("");
m_moter = _T("");
m_right = _T("");
m_sache = _T("");
m_sacheshilin = _T("");
m_stop = _T("");
m_zonglicheng = 0.0;
m_signal10 = _T("");
m_signal11 = _T("");
m_signal12 = _T("");
m_signal9 = _T("");
m_dianya = 0.0f;
m_shuiwen = 0.0f;
//}}AFX_DATA_INIT
// TODO: add construction code here
bDanpianji1Ready=false;
bDanpianji2Ready=false;
bDanpianji1Finish=false;
bDanpianji2Finish=false;
count=0;
bStop1=false;
bStop2=false;
}
CActivexcommView::~CActivexcommView()
{
}
void CActivexcommView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CActivexcommView)
DDX_Control(pDX, IDC_MSCOMM1, m_comm);
DDX_Text(pDX, IDC_EDIT1, m_receive);
DDX_Text(pDX, IDC_CHESU, m_chesu);
DDX_Text(pDX, IDC_DANGRILICHENG, m_dangrilicheng);
DDX_Text(pDX, IDC_SIGNAL, m_signal);
DDX_Text(pDX, IDC_ZHUANSU, m_zhuansu);
DDX_Text(pDX, IDC_ABS, m_abs);
DDX_Text(pDX, IDC_BACK, m_back);
DDX_Text(pDX, IDC_LEFT, m_left);
DDX_Text(pDX, IDC_MOTER, m_moter);
DDX_Text(pDX, IDC_RIGHT, m_right);
DDX_Text(pDX, IDC_SACHE, m_sache);
DDX_Text(pDX, IDC_SACHESHILIN, m_sacheshilin);
DDX_Text(pDX, IDC_STOP, m_stop);
DDX_Text(pDX, IDC_ZONGLICHENG, m_zonglicheng);
DDX_Text(pDX, IDC_SIGNAL10, m_signal10);
DDX_Text(pDX, IDC_SIGNAL11, m_signal11);
DDX_Text(pDX, IDC_SIGNAL12, m_signal12);
DDX_Text(pDX, IDC_SIGNAL9, m_signal9);
DDX_Text(pDX, IDC_DIANYA, m_dianya);
DDX_Text(pDX, IDC_SHUIWEN, m_shuiwen);
//}}AFX_DATA_MAP
}
//***************************************************
BOOL CActivexcommView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CActivexcommView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
//*****************************************
//init the timer
// SetTimer(1,150,NULL);
//communcation
m_comm.SetCommPort(1);
if(!m_comm.GetPortOpen())
m_comm.SetPortOpen(true);
m_comm.SetSettings("4800,n,8,1");
m_comm.SetInputMode(1);
m_comm.SetInputLen(0);
m_comm.GetInput();
m_comm.SetRThreshold(15);
AfxBeginThread(CommThread,(LPVOID)this,THREAD_PRIORITY_ABOVE_NORMAL);
}
/////////////////////////////////////////////////////////////////////////////
// CActivexcommView printing
BOOL CActivexcommView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CActivexcommView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CActivexcommView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CActivexcommView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CActivexcommView diagnostics
#ifdef _DEBUG
void CActivexcommView::AssertValid() const
{
CFormView::AssertValid();
}
void CActivexcommView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CActivexcommDoc* CActivexcommView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CActivexcommDoc)));
return (CActivexcommDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CActivexcommView message handlers
BEGIN_EVENTSINK_MAP(CActivexcommView, CFormView)
//{{AFX_EVENTSINK_MAP(CActivexcommView)
ON_EVENT(CActivexcommView, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
//******************************
char HexChar(char c)//检测一个字符是不是十六进制字符,若是返回相应的值,否则返回0x10;
{
if((c>='0')&&(c<='9'))
return c-0x30;
else if((c>='A')&&(c<='F'))
return c-'A'+10;
else if((c>='a')&&(c<='f'))
return c-'a'+10;
else return 0x10;
}
//将一个字符串作为十六进制串转化为一个字节数组,字节间可用空格分隔,返回转换后的字节数组长度,同时字节数组长度自动设置。
int Str2Hex(CString str,CByteArray &data)
{
int t,t1;
int rlen=0,len=str.GetLength();
data.SetSize(len/2);
for(int i=0;i<len;)
{char l,h=str[i];
if(h==' ')
{i++;
continue;
}
i++;
if(i>=len)break;
l=str[i];
t=HexChar(h);
t1=HexChar(l);
if((t==16)||(t1==16))
break;
else t=t*16+t1;
i++;
data[rlen]=(char)t;
rlen++;
}
data.SetSize(rlen);
return rlen;
}
//************************************
//字符串转换为整型
int strtoint(char c)
{
int i;
if((c>='0')&&(c<='9'))
return i=c-'0';
else if((c>='A')&&(c<='F'))
return i=c-'A'+10;
else if((c>='a')&&(c<='f'))
return i=c-'a'+10;
}
//处理接收数据的通讯函数
void CActivexcommView::OnComm()
{
int m;
// MessageBox("receive data",MB_OK);
VARIANT m_input1;
COleSafeArray m_input2;
long length,i;
CString str;
m_receive.Empty();//清空数据
UpdateData(false);
if(m_comm.GetCommEvent()==2)//接收区中有数据
{
m_input1=m_comm.GetInput();//读取数据
m_input2=m_input1;//将VARIANT 型变量转换为COleSafeArray型数据
length=m_input2.GetOneDimSize();//确定数据长度
for(i=0;i<length;i++)
{
m_input2.GetElement(&i,data+i);
// char a=*(char*)(data+i);
BYTE a=*(char*)(data+i);
str.Format("%02x",a);
// str.Format("%d",a);//10进制表示数据
m_receive+=str;
}
UpdateData(false);
m_comm.SetInputLen(0);//清空接收区的数据等待下组数据
//应答信息
if(m_receive.Left(4)=="ff55"&&data[3]==0xaa)
{
// MessageBox("RECEIVE SIGNAL OF DANPIANJI",MB_OK);
switch(data[2])
{
case 1:
// AfxMessageBox("danpianji1 send signal to computer");
bDanpianji1Ready=true;
SendDate(1);//发送传感器转换命令
CollectDataCmd(1);
break;
case 2:
bDanpianji2Ready=true;
SendDate(2);
CollectDataCmd(2);
// AfxMessageBox("danpianji2 send signal to computer");
break;
case 85:
bBlackBoxReady=true;
SendDate(85);
break;
}
}
//上传数据
if(m_receive.Left(4)=="ff33"&&data[3]==0xaa)
{
// AfxMessageBox("danpianji send data to computer");
switch(data[2])
{
case 1:
Danpianji1Data();//处理单片机1数据
// CollectDataCmd(1);//change at 16:45 pm 2001.8.5
bDanpianji1Finish=true;
bStop2=false;
bDanpianji2Ready=false;
count=0;
// SendCommand(2);//发点名信息给单片机2//change at 16:45 pm 2001.8.5
// XhDianming(1);
Sleep(500);
SendCommand(2);
break;
case 2:
Danpianji1Data();//处理单片机2数据
// SendCommand(1);
Sleep(500);
XhDianming(1);
break;
}
}
// m_receive.Empty();//清空数据
UpdateData(false);
}
}
//发送时间和日期
void CActivexcommView::SendDate(int i)
{
CTime t;
// char buffer[10];
CByteArray Timedata;
Timedata.SetSize(15);
CString senddate,TXT[10],Danpianji;
t=CTime::GetCurrentTime();
CString year=t.Format("%y");
CString month=t.Format("%m");
CString day=t.Format("%d");
CString hour=t.Format("%H");
CString min=t.Format("%M");
CString sec=t.Format("%S");
senddate="ffa5aa";
switch(i)
{
case 1:
Danpianji="01";
break;
case 2:
Danpianji="02";
break;
case 85:
Danpianji="55";
break;
}
senddate+=Danpianji+"06";
senddate+=year+month+day+hour+min+sec;
int length=Str2Hex(senddate,Timedata);
//补足空余数据位
for(int j=0;j<15-length;j++)
{
senddate+="00";
}
//send date and time to danpanji
int sendlength=Str2Hex(senddate,Timedata);
m_comm.SetOutput(COleVariant(Timedata));
}
void CActivexcommView::SendCommand(int danpianji)
{
//点名
CByteArray Command;
Command.SetSize(15);
Command[0]=0xff;
Command[1]=0xaa;
Command[2]=0xaa;
//选择单片机
switch(danpianji)
{
case 1:
Command[3]=0x01;
break;
case 2:
Command[3]=0x02;
break;
case 3:
Command[3]=0x03;
break;
case 4:
Command[3]=0x55;
break;
}
//用0 补足空余
for(int i=4;i<15;i++)
{
Command[i]=0x00;
}
m_comm.SetOutput(COleVariant(Command));
}
//处理一号单片机数据
void CActivexcommView::Danpianji1Data()
{
int len=data[4];
float shuiwen,dianya;
CString str,temp,Temp1,Temp2,Temp3,Temp4,Temp5,Temp6,Temp7,Temp8,Temp9,Temp10,Temp11,Temp12,Temp13;
char buffer[50];
CString TXT1,TXT2,TXT3;
CString signal1, signal2, signal3,signal;
if(len>0)
{
//SIGNAL
str.Format("%02x",data[5]);
TXT1=str;
//处理信号的高位
signal1=itoa(strtoint(TXT1[0]),buffer,2);
if(TXT1[0]<'8' && TXT1[0]>'3')
signal1="0"+signal1;
if(TXT1[0]=='2'|| TXT1[0]=='3')
signal1="00"+signal1;
if(TXT1[0]<'2')
signal1="000"+signal1;
//处理信号的低位
signal2=itoa(strtoint(TXT1[1]),buffer,2);
if(TXT1[1]<'8' && TXT1[1]>'3')
signal2="0"+signal2;
if(TXT1[1]=='2' || TXT1[1]=='3')
signal2="00"+signal2;
if(TXT1[1]<'2')
signal2="000"+signal2;
str.Format("%02x",data[6]);
TXT2=str;
//处理信号的低位
signal3=itoa(strtoint(TXT2[1]),buffer,2);
if(TXT2[1]<'8' && TXT2[1]>'3')
signal3="0"+signal3;
if(TXT2[1]=='2' || TXT2[1]=='3')
signal3="00"+signal3;
if(TXT2[1]<'2')
signal3="000"+signal3;
signal=signal1+signal2+signal3;
//给各个信号赋值
m_back=signal[0];
m_left=signal[1];
m_right=signal[2];
m_stop=signal[3];
m_sache=signal[4];
m_sacheshilin=signal[5];
m_moter=signal[6];
m_abs=signal[7];
m_signal9=signal[8];
m_signal10=signal[9];
m_signal11=signal[10];
m_signal12=signal[11];
//chesu
str.Format("%d",data[7]);
TXT3=str;
//zhuansu
str.Format("%02x",data[8]);
Temp1=str;
str.Format("%02x",data[9]);
Temp2=str;
temp=Temp1+Temp2;//1db0;
int a=strtoint(temp[0])*16*16*16;
int b=strtoint(temp[1])*16*16;
int c=strtoint(temp[2])*16;
int d=strtoint(temp[3]);
int zhuansu=a+b+c+d;
//licheng
str.Format("%02x",data[10]);
Temp3=str;
str.Format("%02x",data[11]);
Temp4=str;
str.Format("%02x",data[12]);
Temp5=str;
temp=Temp3+Temp4+Temp5;
int licheng=strtoint(temp[0])*16*16*16*16*16+strtoint(temp[1])*16*16*16*16+
strtoint(temp[2])*16*16*16+strtoint(temp[3])*16*16+strtoint(temp[4])*16+strtoint(temp[5]);
//处理里程信号的脉冲数
str.Format("%02x",data[13]);
Temp6=str;
str.Format("%02x",data[14]);
Temp7=str;
temp=Temp6+Temp7;
int lichengmeter=strtoint(temp[0])*16*16*16+strtoint(temp[1])*16*16*16+
strtoint(temp[2])*16+strtoint(temp[3]);
float meter=(float)lichengmeter/4128;
double Zonglicheng=(double)licheng+meter;
//dangrilicheng
str.Format("%02x",data[15]);
Temp8=str;
str.Format("%02x",data[16]);
Temp9=str;
temp=Temp8+Temp9;
int Dangrilicheng=strtoint(temp[0])*16*16*16+strtoint(temp[1])*16*16*16+
strtoint(temp[2])*16+strtoint(temp[3]);
//水温
str.Format("%d",data[17]);
Temp10=str;
str.Format("%d",data[18]);
Temp11=str;
shuiwen=float((atoi(Temp10))*256+atoi(Temp11))/100;
//电压
str.Format("%d",data[19]);
Temp12=str;
str.Format("%d",data[20]);
Temp13=str;
dianya=float((atoi(Temp12))*256+atoi(Temp13))/100;
/* CSystemApp *app=(CSystemApp*)AfxGetApp();
app->chesu=TXT2;
app->shuiwen=shuiwen;
app->dangrilicheng=Dangrilicheng;
app->zhuansu=itoa(zhuansu,buffer,10);
app->zonglicheng= _gcvt(Zonglicheng,10, buffer );
app->dianya=dianya;
*/
//数据显示
m_chesu=TXT3;
// m_zhuansu=itoa(zhuansu,buffer,10);
m_zhuansu=zhuansu;
m_shuiwen=shuiwen;
m_dangrilicheng=Dangrilicheng;
m_zonglicheng=Zonglicheng;
m_dianya=dianya;
m_signal=TXT1+TXT2;
UpdateData(false);
}
}
void CActivexcommView::Danpianji2Data()
{
AfxMessageBox("danpianji2 send data to computer");
}
void CActivexcommView::OnButton1()
{
m_receive.Empty();
UpdateData(false);
}
void CActivexcommView::CollectDataCmd(int nDpj)
{
CByteArray CollectData;
CollectData.SetSize(15);
CollectData[0]=0xff;
CollectData[1]=0xcc;
CollectData[2]=0xaa;
//选择单片机
switch(nDpj)
{
case 1:
CollectData[3]=0x01;
break;
case 2:
CollectData[3]=0x02;
break;
case 3:
CollectData[3]=0x03;
break;
case 4:
CollectData[3]=0x55;
break;
}
//用0 补足空余
for(int i=4;i<15;i++)
{
CollectData[i]=0x00;
}
m_comm.SetRThreshold(18);
m_comm.SetOutput(COleVariant(CollectData));
}
void CActivexcommView::XhDianming(int dpjn)
{
SendCommand(dpjn);
int times=1;
int j=1;//延时程序
do
{
j++;
if( j>50000 )
{
SendCommand(dpjn);
// MessageBox("无法与单片机1联系,请检查仪表系统");
times++;
}
}while(m_comm.GetInBufferCount()<15||times==3);
}
void CActivexcommView::ThreadEntry()
{
XhDianming(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -