//	M520 : Send and receive binary packet data via serial port

define	bno_tx	= 0;
define	bno_rx	= 0;
define	port	= 0;
define	$InpTxt = "";

function main()
{
	//	Memory block allocation
	bno_tx = _MemAlloc(1024);
	bno_rx = _MemAlloc(1024);

	//	Button display
	_BtnOpen(0, "PortSetting");
	_BtnOpen(1, "Send");
	_BtnOpen(2, "End");

	_BtnEnable(1, _FALSE);

	_Cls();

	_Stop();

	/*	Resource destruction				*/
	_BtnClose(0); _BtnClose(1); _BtnClose(2);
	if (port != 0) {
		_ComClose (port);
		_ComDelete(port);
	}
}

//	Button Event processing
function _OnButton(BtnNo)
{
	if		(BtnNo == 0) {				//	- "Port setting" button pressed?
		if (port = _ComCreateByDialog(port)) {
			_ComSetOnRxPacket(port, "OnRxPacket", bno_rx);
			if (!_ComIsOpened(port) && _ComIsOpenPossible(port)) {
				_ComOpen(port);
			}
			if (_ComIsOpened(port)) _BtnEnable(1, _TRUE );
			else					_BtnEnable(1, _FALSE);
		}
	}
	else if (BtnNo == 1) {				//	- "Send" button pressed?
		//	Transmit binary data (0x0F, 0x10, 0x11) as transparent type packet
		_PutMemBlk(bno_tx, 0, 3, 0x11100F);
		_ComSendPacket(port, bno_tx, 0, 3);

	}
	else if (BtnNo == 2) _Restart();	//	- "End" button pressed?
}

//	Packet reception event processing
function  OnRxPacket(len)
{
	define	loc;

	_PrintF("Reception data =");
	for (loc = 0; loc < len; loc++) {
		_PrintF(" %02X", _GetMemBlk(bno_rx, loc, 1));
	}
	_PrintF("\n");
}
