//	M200 : Connection with AJATERM
define	bno 	= 0;
define	fEnd	= 0;
define	fOpened = _FALSE;

//------------------------------------------------------------------------------//
//	Connection with AJATERM														//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function main()
{
	_BtnOpen(0, "End");					//	Button display

	//	Operation guide display
	_Cls();
	_Print("Display data received by the terminal from the port.\n");
	_Print("@- CTRL + Right click to display the send menu.\n");
	_Print("@- When you press the "End" button, execution of the macro text ends.\n");

	_Stop();							//	Suspension of execution

	//	The following three steps are OK (it is automatically discarded at the end of the macro) even if not specifically described
	_ComClose (_SLOT_PORT);				//	Port close
	_ComDelete(_SLOT_PORT);				//	Port delete
	if (bno != 0) _MemFree(bno);		//	Memory block release
}
//------------------------------------------------------------------------------//
//	Connection event with terminal												//
//																				//
//	Argument:	$MySlot  - Your mail slot name									//
//				$RmtSlot - Terminal-side mailslot name							//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function _OnTermLink($MySlot, $RmtSlot)
{
	_PrintF("Connected to terminal\n");
	//	Memory block allocation
	bno = _MemAlloc(1024);
	//	Mail slot / port generation for connection with terminal
	_ComCreate($MySlot);
	//	Receive event function setting
	_ComSetOnRxText  (_SLOT_PORT, "OnText");
	_ComSetOnRxCtrl  (_SLOT_PORT, "OnCtrl");
	_ComSetOnRxEsc	 (_SLOT_PORT, "OnEsc" );
	_ComSetOnRxPacket(_SLOT_PORT, "OnPacket", bno);
	//	Mail slot / port open for connection with terminal
	_ComOpen($RmtSlot);
	fOpened = _TRUE;
}

//------------------------------------------------------------------------------//
//	Disconnection event with terminal											//
//																				//
//	Argument:	None															//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function _OnTermUnlink()
{
	_PrintF("Terminal has ended\n");
}

//------------------------------------------------------------------------------//
//	Terminal side port open event												//
//																				//
//	Argument:	None															//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function _OnTermOpened()
{
	_PrintF("A port on the terminal side has been opened.\n");
}

//------------------------------------------------------------------------------//
//	Terminal side port close event												//
//																				//
//	Argument:	None															//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function _OnTermClosed()
{
	_PrintF("The port on the terminal side was closed.\n");
}

//------------------------------------------------------------------------------//
//	Processing when text data is received from the port							//
//																				//
//	Argument:	$Text - Received text											//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function OnText($Text)
{
	_PrintF("RxText = '%s'\n", $Text);					//	Display received text
}

//------------------------------------------------------------------------------//
//	Processing when control code is received from port							//
//																				//
//	Argument:	ctrl - Control code												//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function OnCtrl(ctrl)
{
	_PrintF("RxCtrl = 0x%02X\n", ctrl);					//	Display of reception control code
}

//------------------------------------------------------------------------------//
//	Processing when ESC sequence is received from port							//
//																				//
//	Argument:	$Esc - ESC sequence		 										//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function OnEsc($Esc)
{
	_PrintF("RxEsc  = \x1B%s\n", $_StrPart($Esc, 1, 100));	//	Receive ESC indication
}

//------------------------------------------------------------------------------//
//	Processing when packet data is received from the port						//
//																				//
//	Argument:	$Esc - ESC sequence		 										//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function OnPacket(len)
{
	define	b, i;

	_Print("RxPkt  =");									//	Receive packet display
	for (i = 0; i < len; i++) {							//	.
		b = _GetMemBlk(bno, i, 1);						//	.
		_Ph(" 0x"); _Ph(b, 2);							//	.
	}													//	.
	_Print("\n");										//	.
}
//------------------------------------------------------------------------------//
//	Right click processing														//
//																				//
//	Argument:	x - x position		shift - Shift key flag						//
//				y - y position		ctrl  - Ctrl  key flag						//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function _OnRClick(x, y, shift, ctrl)
{//						[0] 		 [1] 						 [2]
	define	$menu[] = {"Send text", "Binary data transmission", "Packet data transmission"};
	define	id, bytes;
	define	$txt;

	if (fOpened) {
		id = _PopupMenu($menu[], x, y);					//	Pop-up menu

		if		(id == 0) {								//	- Text transmission?
			$txt = $_Input("Please enter the text to send");
			if ($txt != "") {									// With character string input?
				_ComSendText(_SLOT_PORT, $txt, "\r\n");			//	 Text data transmission
			}
		}
		else if (id == 1) {								//	- Binary data transmission?
			$txt = $_Input("Please enter the binary data (2 hexadecimal digits) to send, separated by blanks");
			if ($txt != "") {									// With character string input?
				if (bytes = _HexStrToBinary($txt, bno, 0)) {	//	 Hexadecimal character string  binary conversion
					_ComSendBinary(_SLOT_PORT, bno, 0, bytes);	//	 Binary data transmission
				}
			}
		}
		else if (id == 2) {								//	- Packet data transmission?
			$txt = $_Input("Please enter the packet data to send (2 hexadecimal digits) separated by blanks");
			if ($txt != "") {									// With character string input?
				if (bytes = _HexStrToBinary($txt, bno, 0)) {	//	 Hexadecimal character string  binary conversion
					_ComSendPacket(_SLOT_PORT, bno, 0, bytes);	//	 Packet data transmission
				}
			}
		}
	}
	else _MsgBox("Please start the macro window from the terminal", $_MyName, _MB_OK);

	return 123;
}
//------------------------------------------------------------------------------//
//	Processing when button is pressed											//
//																				//
//	Argument:	BtnNo - Buton ID		 										//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function _OnButton(BtnNo)
{
	if (BtnNo == 0) {
		if (_MsgBox("Do you want to end it?", $_MyName, _MB_YESNO) == _IDYES) {
			_Restart();
		}
	}
	return 0;
}
