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

📄 mysocket.cpp

📁 TCP到串口的数据转发应用程序
💻 CPP
字号:
// MySocket.cpp : 实现文件
//

#include "stdafx.h"
#include "Tcp2Com.h"
#include "MySocket.h"
#include "Tcp2ComDlg.h"
#include ".\mysocket.h"

// CMySocket
const char CV = 0x0B;
CMySocket::CMySocket(CTcp2ComDlg *pParent)
: m_pParent(pParent)
, m_nSendCnt(0)
, m_nRecvCnt(0)
{
	ASSERT(m_pParent);
}

CMySocket::~CMySocket()
{
}

void CMySocket::OnConnect(int nErrorCode)// 连接打开
{
	if( 0==nErrorCode )// 连接成功
	{
		CString s = '#'+m_strNO+"+1+0+0+91";
		s.AppendChar((char)0x0D);
		s.AppendChar((char)0x0A);
		strcpy(m_sendBuf, s);
		m_nSendCnt = s.GetLength();
		OnSend(0);
		m_pParent->OnConnectSucess( );
	}
	else// 连接失败
		m_pParent->OnConnectFail( );
}

void CMySocket::OnReceive(int nErrorCode)
{
	if( 0!=nErrorCode )
	{
		OnError();
	}
	else
	{
		int n = Receive(m_recvBuf, MAX_BUF_LEN-m_nRecvCnt);
		if( n == SOCKET_ERROR )
			OnError();
		else
		{
			m_nRecvCnt += n;
			m_pParent->OnReceive( );
		}
	}

}
void CMySocket::OnSend(int nErrorCode)
{
	if( 0!=nErrorCode )
		OnError( );
	else if( m_nSendCnt > 0 )
	{
		int n = Send( m_sendBuf, m_nSendCnt );
		if( n == SOCKET_ERROR )
			OnError();
		else
		{
			m_nSendCnt -= n;
		}
	}

}

void CMySocket::OnClose(int nErrorCode)
{
	if( 0==nErrorCode )
		m_pParent->OnClose( );
}


// CMySocket 成员函数

void CMySocket::OnError(void)
{
	m_nSendCnt = 0;
	m_nRecvCnt = 0;
	m_pParent->OnClose( );
}

void CMySocket::SendSvr(const char * pbuf, int nLen)
{
	CString s = '#'+m_strNO+"+1+4+11+";
	char buf[MAX_BUF_LEN];
	strcpy(&buf[0], s);
	int n = s.GetLength();
	for(int i=0; i<nLen; ++i)
	{
		buf[n+i]=pbuf[i];
	}
	n += nLen;
	for(int i=0; i<n; ++i)// 转义
	{
		char ch = buf[i];
		switch(ch)
		{
		case 0x0A:
			m_sendBuf[m_nSendCnt++] = CV;
			ch = 0x01;
			break;
		case 0x0B:
			m_sendBuf[m_nSendCnt++] = CV;
			ch = 0x02;
			break;
		case 0x0C:
			m_sendBuf[m_nSendCnt++] = CV;
			ch = 0x03;
			break;
		case 0x0D:
			m_sendBuf[m_nSendCnt++] = CV;
			ch = 0x04;
			break;
		case 0x00:
			m_sendBuf[m_nSendCnt++] = CV;
			ch = 0x05;
			break;
		}
		m_sendBuf[m_nSendCnt++] = ch;
	}
	m_sendBuf[m_nSendCnt++] = 0x0D;
	m_sendBuf[m_nSendCnt++] = 0x0A;
	OnSend(0);
}

int CMySocket::RecvSvr(char * pBuf)
{
	int nLen = 0;
	char buf[ MAX_BUF_LEN ];
	// 找0D0A
	int pos = 0;
	for( ; pos < m_nRecvCnt; ++pos )
	{
		if( (char)0x0D==m_recvBuf[pos] && (char)0x0A==m_recvBuf[pos+1] )
		{
			break;
		}
	}
	if( pos != m_nRecvCnt )// 找到了
	{
		// 拷贝出来,反转义
		for(int i=0; i<pos; ++i)
		{
			char ch = m_recvBuf[i];
			if(CV==ch)
			{
				ch = m_recvBuf[++i];
				switch( ch )
				{
				case 0x01:
					ch = 0x0A;
					break;
				case 0x02:
					ch = 0x0B;
					break;
				case 0x03:
					ch = 0x0C;
					break;
				case 0x04:
					ch = 0x0D;
					break;
				case 0x05:
					ch = 0x00;
					break;
				}
			}
			buf[nLen++] = ch;
		}
	}
	// 拷贝剩下的
	pos += 2;
	for(int i=0; pos < m_nRecvCnt; ++pos, ++i)
	{
		m_recvBuf[i]=m_recvBuf[pos];
	}
	m_nRecvCnt = i;
	//  解析包
	CString s = '#'+m_strNO+"+1+4+11+";
	int len = 0;
	if(0==strncmp(s, buf, s.GetLength()))
	{
		for( int pos=s.GetLength(); pos<nLen; ++pos,++len )
		{
			pBuf[len]=buf[pos];
		}
	}
	//
	return len;
}

⌨️ 快捷键说明

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