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

📄 opser.cpp

📁 101通讯规约代码,实现和数据库的完整对接,采用多线程多任务开发.
💻 CPP
字号:
// OpSer.cpp: implementation of the COperate101 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "OpSer.h"
#include "serport.h"
#include <process.h>   


const char plcStr[20][9]={"lopo","clpo","freq","xont","yoff",
							"wpar","-psetut+","-ofpump+",
							"-pzerot+","-psetuf+","-pzerof+",
							"-exigen+","-logawa+","-clloga+",
							"-leftpo+","-rightp+","-cckerr+","-vltrue+","-vdtrue+","-jcstop+"};

static char SRead[20];               //scale read data
static char SRece[36];               //scale receive data
static char PRece[10];               //PLC receive data
static char PRead[10];               //PLC read data
static char PLCData[10];             //the last PLC data
static char PWrite[10];              //the data of send to PLC
const  LPTSTR AskPlc="+askaga-";  //ask plc send againg for itself
static int nCount =0;                //count the same send times
static BOOL bIsPc = FALSE;           //judge the order is vb send
CRITICAL_SECTION cs_sca;
float m_ReadData = 0;
static char TTemp[10];
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

COperate101::COperate101()
{
	InitializeCriticalSection(&cs_sca);
	DWORD  dwThreadID; 
	m_Infos.lpSer = (long)m_pScaleSer;
	m_Infos.bExitThread = TRUE;
	m_Infos.hThread = CreateThread(NULL,0,y101WriteProc,&m_Infos,0,&dwThreadID);
	if(!m_Infos.hThread)
	{
	    MessageBeep(MB_ICONQUESTION);
		MessageBox(NULL,"Create thread failure!","Error Message",MB_OK|MB_ICONERROR);
	}

	m_pPLCSer = new C101Port;
	if(!m_pPLCSer->IsOpened ())
		m_pPLCSer->Open (1);
	if(!m_pPLCSer->SetSerMask (EV_RXCHAR))
	{
	   MessageBeep(MB_ICONQUESTION);
	   MessageBox(NULL,"Set parameter failure!","Error Message",MB_OK|MB_ICONERROR);
    }
	memset(PLCData,0,20);

	m_Infop.lpSer = (long)m_pPLCSer;
	m_Infop.bExitThread = TRUE;
	m_Infop.hThread = CreateThread(NULL,0,y101ReadProc,&m_Infop,0,&dwThreadID);
	if(!m_Infop.hThread)
	{
	    MessageBeep(MB_ICONQUESTION);
		MessageBox(NULL,"Create thread failure!","Error Message",MB_OK|MB_ICONERROR);
	}

}

COperate101::~COperate101()
{
 	DeleteCriticalSection(&cs_sca);
	if(m_Infos.hThread)
	{
		m_Infos.bExitThread = FALSE;
		TerminateThread(m_Infos.hThread,0);
		m_Infos.hThread = NULL;
	}

	if(m_Infop.hThread)
	{
		m_Infop.bExitThread = FALSE;
		TerminateThread(m_Infop.hThread,0);
		m_Infos.hThread = NULL;
	}


	if(m_pScaleSer)
		delete m_pScaleSer;
	m_pScaleSer = NULL;

	if(m_pPLCSer)
		delete m_pPLCSer;
	m_pPLCSer = NULL;
}


#ifdef _DEBUG
static bool temp_Start_copy= 0;
static int	temp_Read_Len= 0;
static BYTE	*temp_Read_Data= NULL;
static int	temp_Read_Up= 6*1024*1024;
#endif

DWORD WINAPI y101WriteProc(LPVOID pParam)
{
	SCALEINFO* Info = (SCALEINFO*)pParam;
	C101Port *SCSer  = (C101Port*)Info->lpSer ;
	int nLen;
	
	int read_len= 22;

	while(Info->bExitThread)
	{
		if(!(SCSer->GetSerMask() & EV_RXCHAR))
			continue;
		memset(SRece,0,read_len);
		nLen = SCSer->Read(SRece,read_len);
#ifdef _DEBUG
		if(temp_Start_copy && temp_Read_Len<temp_Read_Up)
		{
			if(!temp_Read_Data)
				temp_Read_Data= new BYTE[temp_Read_Up];
			::memcpy(temp_Read_Data+temp_Read_Len, SRece, nLen);
			temp_Read_Len += nLen;
		}
#endif

		EnterCriticalSection(&cs_sca);
		SCSer->UpdateDatabase(SRece, nLen);
		LeaveCriticalSection(&cs_sca);
		memset(SRead,0,20);
	}
	Info->hThread = NULL;
	return true;
}

float COperate101::GetScale()
{
	return 10000;
}

void COperate101::SendToPLC(LPSTR lpBuf)
{
	if(!m_pPLCSer->IsOpened ())
		m_pPLCSer->Open(1);
	memcpy(PWrite,lpBuf,10);
 	m_pPLCSer->Write(lpBuf,8);
	bIsPc = TRUE;
}

DWORD WINAPI y101ReadProc(LPVOID pParam)
{
	PLCINFO* Info = (PLCINFO*)pParam;
	C101Port *PCSer = (C101Port*)Info->lpSer;
	int nLength;
	BOOL bPcSend,bPlcSend;

	while(Info->bExitThread)
	{
		if((PCSer->GetSerMask() != EV_RXCHAR))
			continue;
		Sleep(40);
		memset(PRece,0,10);
		nLength = PCSer->Read(PRece,8);
		bPcSend = PRece[0] == '+' && PRece[7] == '-';
		bPlcSend = PRece[0] == '-' && PRece[7] == '+';
		switch(nLength)
		{
		case 8:
			if(bPcSend)
			{
				memcpy(TTemp,PRece,10); 
//				SendMessage(g_hWnd,WM_MYMSG,PC_WPARAM,0);
			}
			else if(bPlcSend)
			{
				memcpy(TTemp,PRece,10); 
			}
			else
			{
				WriteAgain(PCSer,bIsPc);
				continue;
			}
			break;
		case 0:
			continue;
		default:
			WriteAgain(PCSer,bIsPc);
			continue;
		}

		PCSer->ClearInBuf();
		nCount = 0;
	}
	Info->hThread = NULL;
	return true;
}

void WriteAgain(C101Port* pPoint,BOOL bIsPc)
{
	if(nCount>10)
	{
	   nCount=0;
	   bIsPc = FALSE;
	   MessageBeep(MB_ICONQUESTION);
	   MessageBox(NULL,"接受到错误数据!","警告信息",MB_OK|MB_ICONWARNING);
	}
	else
	{
		if(bIsPc)
		{
			pPoint->ClearInBuf();
			Sleep(50);
			pPoint->Write(PWrite,8);
		}
		else
		{
			pPoint->ClearInBuf();
			pPoint->Write(AskPlc,8);
		}
		nCount++;
	}

}

⌨️ 快捷键说明

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