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

📄 linkplcview.cpp

📁 VC6++ program demo source for communication with PLC to read and write data.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// LinkPLCView.cpp : implementation of the CLinkPLCView class
//

#include "stdafx.h"
#include "LinkPLC.h"

#include "LinkPLCSet.h"
#include "LinkPLCDoc.h"
#include "LinkPLCView.h"

#include <conio.h>
#include <stdlib.h>
#include <stdio.h>

#include "setting.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
	
	CFont m_pFont;
	char dummy[20];
	char buf[100];
	int i=0;
volatile bool m_bThread1;
volatile	bool m_bShouldWrite ;	
/////////////////////////////////////////////////////////////////////////////
// CLinkPLCView

IMPLEMENT_DYNCREATE(CLinkPLCView, CDaoRecordView)

BEGIN_MESSAGE_MAP(CLinkPLCView, CDaoRecordView)
	//{{AFX_MSG_MAP(CLinkPLCView)
	ON_BN_CLICKED(IDC_SETTING, OnSetting)
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_MESSAGE(WM_FILEUPDATE,OnFileUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLinkPLCView construction/destruction

CLinkPLCView::CLinkPLCView()
	: CDaoRecordView(CLinkPLCView::IDD)
{
	//{{AFX_DATA_INIT(CLinkPLCView)
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	i=0;
	m_bThread1=0;
	m_bShouldWrite =0;
	m_bFlag = 0;
			for (int j=0 ; j<20 ; j++)
			{
		nTempVoidReadBuf[j] = new char[10];
			}

}

CLinkPLCView::~CLinkPLCView()
{
}

void CLinkPLCView::DoDataExchange(CDataExchange* pDX)
{
	CDaoRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLinkPLCView)
	//}}AFX_DATA_MAP
}

BOOL CLinkPLCView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CDaoRecordView::PreCreateWindow(cs);
}

void CLinkPLCView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_linkPLCSet;
	CDaoRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	
	CWnd* pwndPropSheetHolder = GetDlgItem(IDC_PLACEHOLDER);
	m_pMyPropSheet = new CViewPropertySheet(pwndPropSheetHolder);
	if (!m_pMyPropSheet->Create(pwndPropSheetHolder, 
		WS_CHILD | WS_VISIBLE, 0))
	{
		delete m_pMyPropSheet;
		m_pMyPropSheet = NULL;
		return;
	}

	// fit the property sheet into the place holder window, and show it
	CRect rectPropSheet;
	pwndPropSheetHolder->GetWindowRect(rectPropSheet);
	m_pMyPropSheet->SetWindowPos(NULL, 0, 0,
		rectPropSheet.Width(), rectPropSheet.Height(),
		SWP_NOZORDER | SWP_NOACTIVATE);

	UpdateDataFile();	

}

/////////////////////////////////////////////////////////////////////////////
// CLinkPLCView diagnostics

#ifdef _DEBUG
void CLinkPLCView::AssertValid() const
{
	CDaoRecordView::AssertValid();
}

void CLinkPLCView::Dump(CDumpContext& dc) const
{
	CDaoRecordView::Dump(dc);
}

CLinkPLCDoc* CLinkPLCView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLinkPLCDoc)));
	return (CLinkPLCDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLinkPLCView database support
CDaoRecordset* CLinkPLCView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CLinkPLCView message handlers

int CLinkPLCView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDaoRecordView::OnCreate(lpCreateStruct) == -1)
		return -1;
	// TODO:Add your specialized creation code here
	 FILE *f;

	// setting
	if ( f=fopen("dnc.ini","rt") )
	{
		fscanf(f,"%s%s%s%d%s%d%s%d%s%d",
			&dummy,&m_sPort,
			&dummy,&m_iBaudRate,
			&dummy,&m_iByteSize,
			&dummy,&m_iParity,
			&dummy,&m_iStopBits);
		fclose(f);
	}
	else 
	{ 
		MessageBox("Cannot open \"DNC.INI\" file. Program is terminated!","Warning!",MB_OK);
		return   -1;
	}

	mComPort.Connect(m_sPort, m_iBaudRate, m_iByteSize, m_iParity, m_iStopBits);
	m_NumberStation=1;
	SetTimer(ID_MAIN_TIMER,100,NULL);
	
	m_bThread1=1;
	CheckThread(m_bThread1);
	
	return 0;
}

void CLinkPLCView::OnSetting() 
{
	// TODO: Add your control notification handler code here
	CSetting setting;
	setting.m_sPort=m_sPort;
	sprintf(dummy,"%i",m_iBaudRate);
	setting.m_sBaud=dummy;
	sprintf(dummy,"%i",m_iByteSize);
	setting.m_sBits=dummy;

	switch(m_iParity)
	{
		case 1: setting.m_sParity="ODD"; break;
		case 2: setting.m_sParity="EVEN"; break;
		case 3: setting.m_sParity="None"; break;
	}
	
	switch(m_iStopBits)
	{
		case 1: setting.m_sStop="1"; break;
		case 2: setting.m_sStop="2"; break;
	}

	if(setting.DoModal()==IDOK)
	{
		UpdateData(TRUE);

		strcpy(m_sPort,setting.m_sPort);
		m_iBaudRate=atoi(setting.m_sBaud);
		m_iByteSize=atoi(setting.m_sBits);
		switch(setting.m_sParity[0])
		{
			case 'O': m_iParity=1;break;
			case 'E': m_iParity=2;break;
			case 'N': m_iParity=3;break;
		}
		m_iStopBits=atoi(setting.m_sStop);

		// write .INI file
		FILE *f;
		if ( f=fopen("dnc.ini","wt") )
		{
			fprintf(f,"%s%s\n%s%d\n%s%d\n%s%d\n%s%d\n",
				"Port: ",m_sPort,
				"BaudRate: ",m_iBaudRate,
				"ByteSize: ",m_iByteSize,
				"Parity: ",m_iParity,
				"StopBits: ",m_iStopBits);
		
			fclose(f);
		}
		else 
		{ 
			MessageBox("Cannot open \"DNC.INI\" file. Program is terminated!","Warning!",MB_OK);
			return   ;
		}

		mComPort.Disconnect();
		mComPort.Connect(m_sPort, m_iBaudRate, m_iByteSize, m_iParity, m_iStopBits);
	}

}


void CLinkPLCView::OnTimer(UINT nIDEvent) 
{		
	CPropPage1 * ap=(CPropPage1 *) m_pMyPropSheet->GetActivePage();
	//	ap->ItemUpdate();
	switch (nIDEvent)
	{
	case ID_MAIN_TIMER:
		{
		ap->ItemUpdate();
		ap->RLedUpdate(RunReadBuf);
		ap->YLedUpdate(YReadBuf);
		ap->XLedUpdate(XReadBuf);
		if ((m_bFlag ==1) && (m_bShouldWrite !=1))
			{
				m_bShouldWrite =1 ;
			}
			 
		if 	((m_bFlag ==0) && (m_bShouldWrite ==1) )
			{	
				Process();
				OnRecord();
				m_bShouldWrite =0;
	
			}		
		//ap->YLedUpdate(ReadBuf);
		}
		break;
	case ID_CHECK_TIMER:
		{	
	
		}
		break;
	}
	
	
	CDaoRecordView::OnTimer(nIDEvent);
}

void CLinkPLCView::UpdateDataFile()
{
	FILE *f;
	char dummy[100]="";
	char *tail;
	char *result;
	char *label=NULL;
	int index=0;
	int station;

	if ( f=fopen("link.dat","rt") )
	{
		while(result!=NULL)
		{
			while(label==NULL  && result!=NULL)// tim nhan khoi dau %
			{
					result=fgets(dummy,98,f);
					label=strpbrk( dummy, "%" );
			}

			station=atoi(label+1);
			do{
					result=fgets(dummy,98,f);
					int i=0;
					tail=strpbrk( dummy, ":" );
					if(tail!=NULL)
					{
						dummy[strlen(dummy)-1]='\0';
						m_pMyPropSheet->m_pageMy[station].mComment[index]=new char[100];
						m_pMyPropSheet->m_pageMy[station].mDevice[index]=new char[20];
						dummy[tail-dummy]='\0';
						strcpy(m_pMyPropSheet->m_pageMy[station].mComment[index],dummy);
						strcpy(m_pMyPropSheet->m_pageMy[station].mDevice[index],++tail);
						index++;
					}
					label=strpbrk( dummy, "%" );
			}while(result !=NULL && label==NULL);
			m_pMyPropSheet->m_pageMy[station].mItemCount=index;
			index=0;
		}
		fclose(f);
	}
	else 
	{ 
		MessageBox("Cannot open \"LINK.DAT\" file. Program is terminated!","Warning!",MB_OK);
		return  ;
	}

	CPropPage1 * ap=(CPropPage1 *) m_pMyPropSheet->GetActivePage();
	ap->ItemUpdate();
}

void CLinkPLCView::OnFileUpdate(WPARAM wParam , LPARAM lParam)
{
	FILE *f;

	if ( f=fopen("link.dat","wt") )
	{
		for(int station=0 ; station<16 ; station++)
		{

⌨️ 快捷键说明

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