⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configview.cpp

📁 与LCD1602配套的上位机程序
💻 CPP
字号:
// ConfigView.cpp : implementation file
//

#include "stdafx.h"
#include "MCS.h"
#include "ConfigView.h"
#include "SerialPort.h"
#include "MainFrm.h"
#include "MyEventView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CConfigView

IMPLEMENT_DYNCREATE(CConfigView, CFormView)

CConfigView::CConfigView()
	: CFormView(CConfigView::IDD)
{
	//{{AFX_DATA_INIT(CConfigView)
	m_strSendData = _T("");
	//}}AFX_DATA_INIT
	processing=false;
	LastData=0;
	EndData=0;
	//EndData2=0;
}

CConfigView::~CConfigView()
{
}

void CConfigView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConfigView)
	DDX_Control(pDX, IDC_MCS, m_MCSOnline);
	DDX_Control(pDX, IDC_MANCOMMAND, m_EditManCom);
	DDX_Control(pDX, IDC_FUNCHECK, m_AccCheck);
	DDX_Control(pDX, IDC_SENDTIME, m_Sendtime);
	DDX_Control(pDX, IDC_COMOPEN, m_BtnComOpen);
	DDX_Control(pDX, IDC_COMCLOSE, m_BtnComClose);
	DDX_Control(pDX, IDC_TIMESEND, m_BtnTimeSend);
	DDX_Control(pDX, IDC_MANSEND, m_BtnManSend);
	DDX_Control(pDX, IDC_APPLY, m_BtnApply);
	DDX_Control(pDX, IDC_COMBO_STOP, m_ComStop);
	DDX_Control(pDX, IDC_COMBO_NUM, m_ComNum);
	DDX_Control(pDX, IDC_COMBO_DATA, m_ComData);
	DDX_Control(pDX, IDC_COMBO_CHECK, m_ComCheck);
	DDX_Control(pDX, IDC_COMBO_BOTE, m_ComBote);
	DDX_Text(pDX, IDC_MANCOMMAND, m_strSendData);
	DDV_MaxChars(pDX, m_strSendData, 1024);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CConfigView, CFormView)
	//{{AFX_MSG_MAP(CConfigView)
	ON_BN_CLICKED(IDC_COMOPEN, OnComopen)
	ON_BN_CLICKED(IDC_APPLY, OnApply)
	ON_BN_CLICKED(IDC_COMCLOSE, OnComclose)
	ON_BN_CLICKED(IDC_LCD_DOWN, OnLcdDown)
	ON_BN_CLICKED(IDC_LCD_LEFT, OnLcdLeft)
	ON_BN_CLICKED(IDC_LCD_RIGHT, OnLcdRight)
	ON_BN_CLICKED(IDC_LCD_UP, OnLcdUp)
	ON_MESSAGE(WM_COMM_RXCHAR,OnComm)
	ON_CBN_SELENDOK(IDC_COMBO_BOTE, OnSelendokComboBote)
	ON_CBN_SELENDOK(IDC_COMBO_CHECK, OnSelendokComboCheck)
	ON_CBN_SELENDOK(IDC_COMBO_DATA, OnSelendokComboData)
	ON_CBN_SELENDOK(IDC_COMBO_NUM, OnSelendokComboNum)
	ON_CBN_SELENDOK(IDC_COMBO_STOP, OnSelendokComboStop)
	ON_COMMAND(ID_LCD_DOWN, OnLcdDownAcc)
	ON_COMMAND(ID_LCD_LEFT, OnLcdLeftAcc)
	ON_COMMAND(ID_LCD_RIGHT, OnLcdRightAcc)
	ON_COMMAND(ID_LCD_UP, OnLcdUpAcc)
	ON_BN_CLICKED(IDC_MANSEND, OnMansend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConfigView diagnostics

#ifdef _DEBUG
void CConfigView::AssertValid() const
{
	CFormView::AssertValid();
}

void CConfigView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CConfigView message handlers

char CConfigView::HexChar(char c)
{
	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 CConfigView::Str2Hex(CString str, char* 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++;
	}
	return rlen;
	
}

bool CConfigView::SendCommand(CString str)
{
	if (m_SerialPort.m_hComm != NULL)
	{
		//UpdateData(TRUE);
		
		char data[1024];
		int len=Str2Hex(str,data);
		m_SerialPort.WriteToPort(data,len);
		str="发送:"+str;
		pMain->pEventView->InsertItem(str,0);
		return TRUE;
	}
	else
	{
		pMain->m_wndStatusBar.SetPaneText(0,"串口没有打开!");
		pMain->pEventView->InsertItem("串口没有打开!",1);
	}
	return FALSE;
}

bool CConfigView::ExecuteCommand(int Endbyte)
{
	int MainCommand,AuxCommand,InputDataLength,i;
	short int InputData[1024];

//	CString tttt;
	InputDataLength=Endbyte-0x0b;

	MainCommand = CommandData[3]*256+CommandData[4];
	AuxCommand  = CommandData[5]*256+CommandData[6];

//	tttt.Format("%X",MainCommand);
//	AfxMessageBox(tttt);
	if (InputDataLength)
	{
		for (i=9;i<InputDataLength;i++)
		{
			InputData[i-9]=CommandData[i];
		}
	}
	switch (MainCommand)
	{
	case 0x0112:
// 		if (SendCommand("04 03 00 01 13 FF FF 00 0B F0 F1"))
// 		{
 			pMain->pEventView->InsertItem("MCS51上线!",0);
 			m_MCSOnline.SetWindowText("MCS在线!");
// 		}
// 		else
// 		{
// 			return FALSE;
// 		}
		break;
	case 0x1f11:
		pMain->pEventView->InsertItem("LCD逆时针执行完成",0);
		break;
	case 0x1f21:
		pMain->pEventView->InsertItem("LCD顺时针执行完成",0);
		break;
	case 0x1f31:
		pMain->pEventView->InsertItem("LCD横移执行完成",0);
		break;
	case 0x1f41:
		pMain->pEventView->InsertItem("LCD交换执行完成",0);
		break;
	default:
		pMain->pEventView->InsertItem("未知命令!",1);
	}
	return TRUE;
}

bool CConfigView::FindCommand(int Endbyte)
{
	int i,j,CommandLength;
	CString str,temp;

	for (i=0;i<Endbyte-3;i++)
	{
		if (ReciveData[i] == 0x04 && ReciveData[i+1] == 0x03 && ReciveData[i+2] == 0x00)
		{
			//MainCommand=ReciveData[i+3]*16+ReciveData[i+4];		
			//AuxCommand=ReciveData[i+5]*16+ReciveData[i+6];
			CommandLength=ReciveData[i+7]*256+ReciveData[i+8];
			
			if (CommandLength>Endbyte)
			{
				return FALSE;
			}

			if (CommandLength<0x0b)
			{
				for(j=0;j<Endbyte-i-2;j++)
				{
					ReciveData[j]=ReciveData[i+3+j];
				}
				return FALSE;
			}
			

			if (CommandLength == Endbyte-i)
			{
				str="接收:";
				for(j=0;j<CommandLength;j++)
				{
					CommandData[j]=ReciveData[i+j];
					
					temp.Format("%02X ",CommandData[j]);
					str+=temp;
				}
				pMain->pEventView->InsertItem(str,0);
				ExecuteCommand(CommandLength);
				return TRUE;
			}
		}
	}
	return FALSE;
}



void CConfigView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();

	  	m_ComNum.SetCurSel(0);
	  	m_ComBote.SetCurSel(5);
	  	m_ComCheck.SetCurSel(0);
	  	m_ComData.SetCurSel(2);
	 	m_ComStop.SetCurSel(0);

		m_Sendtime.SetWindowText("1000");

		m_nBaud=9600;
		m_nCom=1;
		m_cParity='N';
		m_nDatabits=8;
		m_nStopbits=1;
		m_dwCommEvents = EV_RXFLAG | EV_RXCHAR;
		
		pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd;


	// TODO: Add your specialized code here and/or call the base class

		OnApply();
}

void CConfigView::OnComopen() 
{
	// TODO: Add your control notification handler code here
	OnApply();
}

void CConfigView::OnApply() 
{
	// TODO: Add your control notification handler code here
	CString strStatus;
	if (m_SerialPort.InitPort(this, m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512))
	{
		m_SerialPort.StartMonitoring();
		strStatus.Format("状态:COM%d 打开,波特率%d, %c,数据位 %d位,停止位 %d位",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
		pMain->pEventView->InsertItem(strStatus,0);
		m_BtnComOpen.EnableWindow(FALSE);
		m_BtnComClose.EnableWindow(TRUE);
	}
	else
	{
		strStatus="没有发现此串口";
		pMain->pEventView->InsertItem(strStatus,1);
		m_BtnComOpen.EnableWindow(TRUE);
		m_BtnComClose.EnableWindow(FALSE);
	}
	
	pMain->m_wndStatusBar.SetPaneText(0,strStatus);
}

void CConfigView::OnComclose() 
{
	// TODO: Add your control notification handler code here
	m_SerialPort.ClosePort();
	m_BtnComOpen.EnableWindow(TRUE);
	m_BtnComClose.EnableWindow(FALSE);
}


void CConfigView::OnLcdDown() 
{
	// TODO: Add your control notification handler code here
	//AfxMessageBox("DOWN");
	if (SendCommand("20"))//(SendCommand("04 03 00 1F 20 FF FF 00 0B F0 F1"))
	{
		pMain->pEventView->InsertItem("LCD逆时针转动",0);
	}
	else
	{
		pMain->pEventView->InsertItem("命令发送失败!",1);
	}
}

void CConfigView::OnLcdLeft() 
{
	// TODO: Add your control notification handler code here
	//AfxMessageBox("LEFT");
	if (SendCommand("30"))//(SendCommand("04 03 00 1F 30 FF FF 00 0B F0 F1"))
	{
		pMain->pEventView->InsertItem("LCD横移",0);
	}
	else
	{
		pMain->pEventView->InsertItem("命令发送失败!",1);
	}
}

void CConfigView::OnLcdRight() 
{
	// TODO: Add your control notification handler code here
	//AfxMessageBox("RIGHT");
	if (SendCommand("40"))//(SendCommand("04 03 00 1F 40 FF FF 00 0B F0 F1"))
	{
		pMain->pEventView->InsertItem("LCD交换",0);
	}
	else
	{
		pMain->pEventView->InsertItem("命令发送失败!",1);
	}
}


void CConfigView::OnLcdUp() 
{
	// TODO: Add your control notification handler code here
	//AfxMessageBox("UP");
	if (SendCommand("10"))//(SendCommand("04 03 00 1F 10 FF FF 00 0B F0 F1"))
	{
		pMain->pEventView->InsertItem("LCD顺时针转动",0);
	}
	else
	{
		pMain->pEventView->InsertItem("命令发送失败!",1);
	}
}


LONG CConfigView::OnComm(WPARAM ch,LPARAM port)
{
	//CString ttstr,ttt;
	if (port <= 0 || port > 6)
		return -1;
	ch&=0x000000ff;
// 	if (!processing)
	
// 	{
		ReciveData[EndData]=ch;
		EndData++;
// 	}
// 	else
// 	{
// 		ReciveData2[EndData]=ch;
// 		EndData2++;
// 	}
		
	if (ch==0xf1 )
	{
		//AfxMessageBox("F1 GOOD!");
		//ttt.Format("%d %d %d",ReciveData[EndData-2],ReciveData[EndData-1],ReciveData[EndData]);
		//AfxMessageBox(ttt);
		if (ReciveData[EndData-2]==0xf0)
		{
			//AfxMessageBox("F0 GOOD!");
			if (EndData>=0x0b)
			{
				//AfxMessageBox("0x0B GOOD!");
				if (FindCommand(EndData))
				{
					EndData=0;
				}
			}
		}
	}
		return 0;
}

void CConfigView::OnSelendokComboBote() 
{
	// TODO: Add your control notification handler code here
	int i=m_ComBote.GetCurSel();
	switch(i)
	{
	case 0:
		i=300;
		break;
	case 1:
		i=600;
		break;
	case 2:
		i=1200;
		break;
	case 3:
		i=2400;
		break;
	case 4:
		i=4800;
		break;
	case 5:
		i=9600;
		break;
	case 6:
		i=19200;
		break;
	case 7:
		i=38400;
		break;
	case 8:
		i=43000;
		break;
	case 9:
		i=56000;
		break;
	case 10:
		i=57600;
		break;
	case 11:
		i=115200;
		break;
	default:
		break;
		
	}
	m_nBaud=i;
}

void CConfigView::OnSelendokComboCheck() 
{
	// TODO: Add your control notification handler code here
	char temp;
	int i=m_ComCheck.GetCurSel();
	switch(i)
	{
	case 0:
		temp='N';
		break;
	case 1:
		temp='O';
		break;
	case 2:
		temp='E';
		break;
	}
	m_cParity=temp;
}

void CConfigView::OnSelendokComboData() 
{
	// TODO: Add your control notification handler code here
	int i=m_ComData.GetCurSel();
	switch(i)
	{
	case 0:
		i=8;
		break;
	case 1:
		i=7;
		break;
	case 2:
		i=6;
		break;
	}
	m_nDatabits=i;
}

void CConfigView::OnSelendokComboNum() 
{
	// TODO: Add your control notification handler code here
	m_nCom=m_ComNum.GetCurSel()+1;
}

void CConfigView::OnSelendokComboStop() 
{
	// TODO: Add your control notification handler code here
	m_nStopbits=m_ComStop.GetCurSel()+1;
}

BOOL CConfigView::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	m_SerialPort.ClosePort();
	return CFormView::DestroyWindow();
}

void CConfigView::OnLcdDownAcc() 
{
	// TODO: Add your command handler code here
	if (m_AccCheck.GetCheck())
	{
		OnLcdDown();
	}	
}

void CConfigView::OnLcdLeftAcc() 
{
	// TODO: Add your command handler code here
	if (m_AccCheck.GetCheck())
	{
		OnLcdLeft();
	}
}

void CConfigView::OnLcdRightAcc() 
{
	// TODO: Add your command handler code here
	if (m_AccCheck.GetCheck())
	{
		OnLcdRight();
	}
}

void CConfigView::OnLcdUpAcc() 
{
	// TODO: Add your command handler code here
	if (m_AccCheck.GetCheck())
	{
		OnLcdUp();
	}
}

void CConfigView::OnMansend() 
{
	// TODO: Add your control notification handler code here
		if (m_SerialPort.m_hComm != NULL)
		{
			UpdateData(TRUE);
			
			char data[512];
			int len=Str2Hex(m_strSendData,data);
			m_SerialPort.WriteToPort(data,len);
			m_strSendData="发送:"+m_strSendData;
			pMain->pEventView->InsertItem(m_strSendData,0);
			m_EditManCom.SetWindowText("");
		}
		else
		{
			pMain->m_wndStatusBar.SetPaneText(0,"串口没有打开!");
			pMain->pEventView->InsertItem("串口没有打开!",1);
		}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -