comsend.cpp

来自「DVD」· C++ 代码 · 共 130 行

CPP
130
字号
// **********************************************************************
//  This file is a part of MaBreakers
//  MTK RS232 Communication package
// **********************************************************************
//
//  Copyright (C) 2006 MaBreaker
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
//  02110-1301, USA.
//
// **********************************************************************

#include <stdio.h>
#include "ComMain.h"

// **********************************************************************
#pragma package(smart_init)
// **********************************************************************

__fastcall TComSend::TComSend(void)
{
}
//---------------------------------------------------------------------------

bool __fastcall TComSend::Byte(uchar bData)
{
	bool Ret;
	ulong ulBytesSend;

	// Send Data
	Ret = WriteFile(ComPort->hComm, &bData, 1, &ulBytesSend, NULL);

  #ifdef _DEBUG
	if(Ret)
	{
        sprintf(ComPort->Msg, "Send : %02X", bData);
		Print->Debug(ComPort->Msg);
	}
	else
	{
		Print->Debug("Send : Error!");
	}
  #endif

	// Return send succes status
	return Ret;
}
//---------------------------------------------------------------------------

bool __fastcall TComSend::Command(uchar bCommand, uchar bValue)
{
	bool Ret;
	uchar cData[2];
	ulong ulBytesSend;

	// Set data to send
	cData[0] = bCommand;
	cData[1] = bValue;

	// Send Data
	Ret = WriteFile(ComPort->hComm, cData, sizeof(cData), &ulBytesSend, NULL);

  #ifdef _DEBUG
	if(Ret)
	{
        sprintf(ComPort->Msg, "Send : %02X %02X", bCommand, bValue);
		Print->Debug(ComPort->Msg);
	}
	else
	{
		Print->Debug("Send : Error!");
	}
  #endif

	// Return send succes status
	return Ret;
}
//---------------------------------------------------------------------------

bool __fastcall TComSend::Data(uchar *bData, ulong ulLength)
{
	bool Ret;
	ulong ulBytesSend;

	if(ulLength > MTK_SEND_BUFFER)
    	ulLength = MTK_SEND_BUFFER;

	// Send Data
	Ret = WriteFile(ComPort->hComm, bData, ulLength, &ulBytesSend, NULL);

  #ifdef _DEBUG
	if(Ret)
	{
		strcpy(ComPort->Msg, "Send : ");

		// Do not overflow string (Length = 7 + 20 * 2 + 2)
		if(ulBytesSend > 20)
			ulBytesSend = 20;

		for(ulLength = 0; ulLength < ulBytesSend; ulLength++)
		{
			sprintf(ComPort->Tmp, "%02X ", *(bData+ulLength));
            strcat(ComPort->Msg, ComPort->Tmp);
		}
		Print->Debug(ComPort->Msg);
	}
	else
	{
		Print->Debug("Send : Error!");
	}
  #endif

	// Return send succes status
	return Ret;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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