//	M510 : Send and receive text data via serial port

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

function main()
{
	//	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)
{
	define	$txt;

	if		(BtnNo == 0) {				//	- "Port setting" button pressed?
		if (port = _ComCreateByDialog(port)) {
			_ComSetOnRxText(port, "OnRxText");
			_ComSetOnRxCtrl(port, "OnRxCtrl");
			if (!_ComIsOpened(port) && _ComIsOpenPossible(port)) {
				_ComOpen(port);
				_BtnEnable(1, _TRUE);
			}
		}
	}
	else if (BtnNo == 1) {				//	- "Send" button pressed?
		if ($InpTxt = $_Input("Please enter the text to send", $InpTxt)) {
			_ComSendText(port, $_StrCat($InpTxt, "\n"));
			_PrintF("--> '%s'\n", $InpTxt);
		}
	}
	else if (BtnNo == 2) _Restart();	//	- "End" button pressed?
}

//	Text reception event processing
function  OnRxText($Text)
{
	_PrintF("<-- '%s'", $Text);
}

//	Control code reception event processing
function  OnRxCtrl(ctrl)
{
	_PrintF("%c", ctrl);
}
