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

📄 linkplcview.cpp

📁 VC6++ program demo source for communication with PLC to read and write data.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			fprintf(f,"%%%i\n",station);
			for(int index=0 ; index<m_pMyPropSheet->m_pageMy[station].mItemCount ; index++)
			{
				fprintf(f,"%s:%s\n",m_pMyPropSheet->m_pageMy[station].mComment[index],m_pMyPropSheet->m_pageMy[station].mDevice[index]);
			}
		}

		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::SendCommand()
{
//	char nsDevice[6];
//	char nsCommand[20];
//	char nsDummy[20];
//	// Doc tu PLC
//	char nsReadBuf[1000];
//	CPropPage1 * ap;
//	ap=(CPropPage1 *) m_pMyPropSheet->GetActivePage();
/*
	for (int station=0 ; station<1 ; station++)
	{
	mComPort.WriteComm("00FFBW0Y0000011");
	Sleep(100);
	mComPort.WriteComm("00FFWW0D8120014896");
	Sleep(100);
	mComPort.ReadComm(nsReadBuf,sizeof(nsReadBuf));
	Sleep(100);
	mComPort.WriteComm("00FFBR0X000018");
	Sleep(100);
	mComPort.ReadComm(nsReadBuf,sizeof(nsReadBuf));
	Sleep(100);
	ap->XLedUpdate(nsReadBuf);
	mComPort.WriteComm("00FFBR0Y000010");
	Sleep(100);
	mComPort.ReadComm(nsReadBuf,sizeof(nsReadBuf));
	Sleep(100);
	ap->YLedUpdate(nsReadBuf);
	mComPort.WriteComm("00FFBR0M800001");
	Sleep(100);
//	strcpy(nsReadBuf,"                                                                            ");
	mComPort.ReadComm(nsReadBuf,sizeof(nsReadBuf));
	ap->RLedUpdate(nsReadBuf);//RUN LED
	Sleep(100);

  /*
		for(int index=0 ; index<m_pMyPropSheet->m_pageMy[station].mItemCount ; index++)
		{
			strcpy(nsDevice,m_pMyPropSheet->m_pageMy[station].mDevice[index]);
			strcpy(nsDummy,"");
			strcpy(nsCommand,"");
			sprintf(nsDummy,"%02i",station);
			strcat(nsCommand,nsDummy);
			strcat(nsCommand,"FFWR3");
			strcat(nsCommand,nsDevice);
			strcat(nsCommand,"01");
			mComPort.WriteComm(nsCommand);
			Sleep(100);
			mComPort.ReadComm(nsReadBuf,sizeof(nsReadBuf));
			Sleep(100);
			strncpy(m_pMyPropSheet->m_pageMy[0].mValue[index],nsReadBuf+5,4);
		}

	}
*/
}

void CLinkPLCView::ReceiveStatus()
{
//	char nsReadBuf[1000];
//	mComPort.ReadComm(nsReadBuf,sizeof(nsReadBuf));
//	CPropPage1 * ap=(CPropPage1 *) m_pMyPropSheet->GetActivePage();
//	ap->LedUpdate(nsReadBuf);

}

int CLinkPLCView::ParseReadBuf(char *lpBuf)
{
return 0;
}

UINT CLinkPLCView::ThreadCommunication(LPVOID pParam)
{
	CLinkPLCView* pView = (CLinkPLCView*)pParam;
//	char nsReadBuf[100];
	char nsCommand[20];
	char nsDummy[20];
	int station ;

	//memset(&(pView->ReadBuf), 0, sizeof(pView->ReadBuf));
	
	while(/*pView->*/m_bThread1)
	{
		pView->mComPort.WriteComm("00FFBR0M800001");
		::Sleep(100);
		pView->mComPort.ReadComm(pView->RunReadBuf,sizeof(pView->RunReadBuf));
		::Sleep(100);
		pView->mComPort.WriteComm("00FFBR3Y000010");
		::Sleep(100);
		pView->mComPort.ReadComm(pView->YReadBuf,sizeof(pView->YReadBuf));
		::Sleep(100);
		pView->mComPort.WriteComm("00FFBR3X000015");
		::Sleep(100);
		pView->mComPort.ReadComm(pView->XReadBuf,sizeof(pView->XReadBuf));
		::Sleep(100);
		
		for ( station=0 ; station<pView->m_NumberStation ; station++)
				{	
					pView->m_bFlag=0;
					strcpy(nsDummy,"");
					strcpy(nsCommand,"");
					sprintf(nsDummy,"%02i",station);
					strcat(nsCommand,nsDummy);
					strcat(nsCommand,"FFBR3");
					strcat(nsCommand,"M000901");
					
					pView->mComPort.WriteComm(nsCommand);Sleep(100);
					pView->mComPort.ReadComm(pView->MReadBuf,sizeof(pView->MReadBuf));Sleep(100);
					pView->m_ViewStation = station;
					if (pView->OnHandle(station,pView->MReadBuf))
						{
							pView->m_bFlag =1;

							strcpy(nsDummy,"");
							strcpy(nsCommand,"");	
							sprintf(nsDummy,"%02i",station);
							strcat(nsCommand,nsDummy);
							strcat(nsCommand,"FFBW0");
							strcat(nsCommand,"M0009010");
							pView->mComPort.WriteComm(nsCommand);Sleep(100);
						}
				}
	
	}

	return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CLinkPLCView::CheckThread( BOOL bSuspend)
{

	if(!bSuspend)
	{
		if(m_pComThread)
		{
			HANDLE hThread = m_pComThread->m_hThread;
		
			::WaitForSingleObject(hThread,INFINITE);
		}
	}
	else
			{
			
				m_pComThread= AfxBeginThread(ThreadCommunication,this, THREAD_PRIORITY_HIGHEST);

			}

}

bool CLinkPLCView::OnHandle(int nSubStation,char* nSubReadBuf)
{
	CString one;
	CString zero;
	one="1";
	zero="0";

//	if ((nSubReadBuf[9] == one )&&(nSubReadBuf[19] == one))
	if (nSubReadBuf[5] == one )
	{
		return TRUE;
	}

	return FALSE;
}

void CLinkPLCView::OnReadD(int nsStation)
{

	char nsVoidDevice[6];
	char nsVoidCommand[20];
	char nsVoidDummy[20];
	char nsVoidReadBuf[20];
	char buffer[50];
	CPropPage1 * ap;
	ap=(CPropPage1 *) m_pMyPropSheet->GetActivePage();

	memset(&nsVoidReadBuf, 0, sizeof(nsVoidReadBuf));

	Sleep(100);

REP:

	for(int index=0 ; index<m_pMyPropSheet->m_pageMy[nsStation].mItemCount ; index++)
		{
		//	memset(nTempVoidReadBuf[index], '\0', sizeof(nTempVoidReadBuf[index]));
			strcpy(nsVoidDevice,m_pMyPropSheet->m_pageMy[nsStation].mDevice[index]);
			strcpy(nsVoidDummy,"");
			strcpy(nsVoidCommand,"");
			sprintf(nsVoidDummy,"%02i",nsStation);
			strcat(nsVoidCommand,nsVoidDummy);
			strcat(nsVoidCommand,"FFWR3");
			strcat(nsVoidCommand,nsVoidDevice);

			strcat(nsVoidCommand,"01");
			Sleep(100);
			mComPort.WriteComm(nsVoidCommand);
//			if(mComPort.PlcRead(nsVoidReadBuf)==0)
//			mComPort.ReadComm(nsVoidReadBuf)==0,sizeof(nsVoidReadBuf)
			Sleep(100);
			if(!mComPort.ReadComm(nsVoidReadBuf,sizeof(nsVoidReadBuf)))
			{
				goto REP;
			};
			Sleep(100);
			nsVoidReadBuf[9]='\0';
			strncpy(nTempVoidReadBuf[index],nsVoidReadBuf+5,6);

		}

	m_ViewNgay = COleDateTime::GetCurrentTime();
    
	m_ViewMa_hang= strtol(nTempVoidReadBuf[0], NULL, 16);
	_gcvt(m_ViewMa_hang, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[0], buffer,7);

  	m_ViewSTT= strtol(nTempVoidReadBuf[1], NULL, 16);
	_gcvt(m_ViewSTT, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[1], buffer,7);

	m_ViewSo_loai= strtol(nTempVoidReadBuf[2], NULL, 16);
	_gcvt(m_ViewSo_loai, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[2], buffer,7);

	m_ViewChieu_dai_1= strtol(nTempVoidReadBuf[3], NULL, 16);
		_gcvt(m_ViewChieu_dai_1, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[3], buffer,7);

	m_ViewSo_luong_1= strtol(nTempVoidReadBuf[4], NULL, 16);
		_gcvt(m_ViewSo_luong_1, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[4], buffer,7);

	m_ViewChieu_dai_2= strtol(nTempVoidReadBuf[5], NULL, 16);
		_gcvt(m_ViewChieu_dai_2, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[5], buffer,7);

	m_ViewSo_luong_2= strtol(nTempVoidReadBuf[6], NULL, 16);
		_gcvt(m_ViewSo_luong_2, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[6], buffer,7);

	m_ViewChieu_dai_3= strtol(nTempVoidReadBuf[7], NULL, 16);
		_gcvt(m_ViewChieu_dai_3, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[7], buffer,7);

	m_ViewSo_luong_3= strtol(nTempVoidReadBuf[8], NULL, 16);
		_gcvt(m_ViewSo_luong_3, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[8], buffer,7);

	m_ViewChieu_dai_4= strtol(nTempVoidReadBuf[9], NULL, 16);
		_gcvt(m_ViewChieu_dai_4, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[9], buffer,7);

	m_ViewSo_luong_4= strtol(nTempVoidReadBuf[10], NULL, 16);
		_gcvt(m_ViewSo_luong_4, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[10], buffer,7);

	m_ViewChieu_dai_5= strtol(nTempVoidReadBuf[11], NULL, 16);
			_gcvt(m_ViewChieu_dai_5, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[11], buffer,7);

	m_ViewSo_luong_5= strtol(nTempVoidReadBuf[12], NULL, 16);
			_gcvt(m_ViewSo_luong_5, 7, buffer );
	strncpy(m_pMyPropSheet->m_pageMy[0].mValue[12], buffer,7);


}
void CLinkPLCView::OnRecord()
{	
	//m_bThread1=false ;
	long m_NewID=m_pSet->NewID()+1;
	m_pSet->AddNew();
	
	m_pSet->m_Ngay=m_ViewNgay;
	m_pSet->m_Ma_hang=m_ViewMa_hang;
	m_pSet->m_STT=m_ViewSTT;
	m_pSet->m_So_loai = m_ViewSo_loai;
	m_pSet->m_Chieu_dai_1 =m_ViewChieu_dai_1 ;
	m_pSet->m_So_luong_1 = m_ViewSo_luong_1;
	m_pSet->m_Chieu_dai_2 = m_ViewChieu_dai_2;
	m_pSet->m_So_luong_2 = m_ViewSo_luong_2;
	m_pSet->m_Chieu_dai_3 = m_ViewChieu_dai_3;
	m_pSet->m_So_luong_3 = m_ViewSo_luong_3;
	m_pSet->m_Chieu_dai_4 = m_ViewChieu_dai_4;
	m_pSet->m_So_luong_4 = m_ViewSo_luong_4;
	m_pSet->m_Chieu_dai_5 = m_ViewChieu_dai_5;
	m_pSet->m_So_luong_5 = m_ViewSo_luong_5;
	
	m_pSet->m_ID=m_NewID;
	m_pSet->Update();
	m_pSet->Requery();
	m_pSet->MoveLast();
//	::Sleep(100);
	m_bShouldWrite =0;

	m_bThread1=true ;
	CheckThread(m_bThread1);

//	SetTimer(ID_MAIN_TIMER,50,NULL);
}
void CLinkPLCView::OnDestroy() 
{
// TODO: Add your message handler code here

	if(m_bThread1)
	{
		m_bThread1=false;
		CheckThread(m_bThread1);
	}
	CDaoRecordView::OnDestroy();
}


void CLinkPLCView::Process()
{
	m_bThread1=false ;
	OnReadD(m_ViewStation);
	
}

⌨️ 快捷键说明

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