//	M070 : Serial transmission / reception and event
option unsigned;

define	port, bno = 0;
define	fEnd	= 0;
define	fOpened = _FALSE;

//------------------------------------------------------------------------------//
//	Serial transmission / reception and event									//
//																				//
//	Return	: None																//
//------------------------------------------------------------------------------//
function main()
{
	define	n[10];

	_BtnOpen(0, "Port setting");		//	Button display
	_BtnOpen(1, "Port open");			//	.
	_BtnOpen(2, "End");					//	.

	//	Operation guide display
	_Cls();
	 _SetTextColor(4);
	_Print("Display received data from the port.\n"
		   "Received data loops back as it is.\n");
	_Print("@EYou can change the port setting with "Port setting" button.\n");
	_Print("@EWhen double-clicked, the line text you double-clicked is displayed.\n");
	_Print("@EWhen a key is entered, the code of the key is displayed.\n");
	_Print("@ERight click to display the send menu.\n");
	_Print("@EWhen you press the "End" button, execution of the macro text ends.\n");
	_Print("* When entering the key, please key in this window with focus.\n\n");
	_ResetColor();

	_EnableStdRClk(_FALSE);	//	Prohibition of popup menu by standard right click
							//	With this setting "_OnRClick ()" is executed when right clicking

	bno = _MemAlloc(1024);				//	Memory block allocation

	port  = _ComCreate();				//	Create port

	_Stop();							//	Suspension of execution

	_ComClose (port);					//	Close port
	_ComDelete(port);					//	Delete port
	_MemFree(bno);						//	Memory block release
}

//------------------------------------------------------------------------------//
//	Port open state setting														//
//																				//
//	Argument: None					 											//
//																				//
//	Return	: None																//
//------------------------------------------------------------------------------//
function CheckOpenState()
{
	if (_ComIsOpened(port)) {
		fOpened = _TRUE;
		_BtnOpen(1, "PortClose");
	}
	else {
		fOpened = _FALSE;
		_BtnOpen(1, "PortOpen");
	}
	//	Port information display
	_ShowText($_StrCat(" ", $_ComGetParam(port), " "));
}
//------------------------------------------------------------------------------//
//	Port open / close															//
//																				//
//	Argument: pno - Port number	 												//
//																				//
//	Return	: None																//
//------------------------------------------------------------------------------//
function OpenPort()
{
	define	$text;

	if (!fOpened) {										//	Not yet open?
		if (_ComIsOpenPossible(port)) {					//		Openable?
			_ComSetOnRxText  (port, "OnText");			//			Register reception text 	 event processing function
			_ComSetOnRxCtrl  (port, "OnCtrl");			//			Register reception ctrl-code event processing function
			_ComSetOnRxEsc	 (port, "OnEsc" );			//			Register reception ESC-code  event processing function
			_ComSetOnRxPacket(port, "OnPacket", bno);	//			Register reception Packet	 event processing function
			_ComOpen		 (port);					//			Port open
		}
	}
	else {												//	Opened?
		_ComClose(port);								//		Port close
	}
	CheckOpenState();									//	Port open status check
}
//------------------------------------------------------------------------------//
//	Processing when text data is received from the port							//
//																				//
//	Argument:	$Text - Received text											//
//																				//
//	Return	:	None															//
//------------------------------------------------------------------------------//
function OnText($Text)
{
	_Print("RxText = '", $Text, "'\n");					//	Display received text
	_ComSendText(port, $Text);							//	Send loopback
}

//------------------------------------------------------------------------------//
//	Processing when control code is received from port							//
//																				//
//	Argument:	ctrl - Received control code									//
//																				//
//	Return	:	None															//
//------------------------------------------------------------------------------//
function OnCtrl(ctrl)
{
	_Pd("RxCtrl = 0x"); _Ph(ctrl, 2); _Pd("\n");		//	Display of reception control code
	_ComSendBytes(port, ctrl);							//	Send loopback
}

//------------------------------------------------------------------------------//
//	Processing when ESC sequence is received from port							//
//																				//
//	Argument:	$Esc - Received ESC sequence									//
//																				//
//	Return	:	None															//
//------------------------------------------------------------------------------//
function OnEsc($Esc)
{
	_Print("RxEsc  = '\x1B', $_StrPart($Esc, 1, 100), "'\n");	//	Display of reception ESC sequence
	_ComSendText(port, $Esc);									//	Send loopback
}

//------------------------------------------------------------------------------//
//	Processing when packet data is received from the port						//
//																				//
//	Argument:	$Esc - Received packet data 									//
//																				//
//	Return	:	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");										//	.
	_ComSendPacket(port, bno, 0, len);					//	Send loopback
}
//------------------------------------------------------------------------------//
//	Processing at key input														//
//																				//
//	Argument:	key - Key code			 										//
//																				//
//	Return	:	None															//
//------------------------------------------------------------------------------//
function _OnKeyIn(key)
{
	_PrintF("Key = 0x%02X\n", key);			//	Input key code display
}
//------------------------------------------------------------------------------//
//	Right click processing														//
//																				//
//	Argument:	x - x position			shift - Shift key flag					//
//				y - y position			ctrl  - Ctrl key flag					//
//																				//
//	Return	:	None															//
//------------------------------------------------------------------------------//
function _OnRClick(x, y, shift, ctrl)
{//						[0] 			[1] 		[2]	
	define	$menu[] = {"Send text", "Send Binary", "Send Packet"};
	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(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(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(port, bno, 0, bytes);		//	 Transmit packet data transmission
				}
			}
		}
	}
	else _MsgBox("Please open the port", $_MyName, _MB_OK);
}
//------------------------------------------------------------------------------//
//	Processing when button is pressed											//
//																				//
//	Argument:	BtnNo - Button ID		 										//
//																				//
//	Return	:	None															//
//------------------------------------------------------------------------------//
function _OnButton(BtnNo)
{
	define	NewPort;
	define	parity;

	if		(BtnNo == 0) {									//	0: Port setting button
		if (NewPort = _ComCreateByDialog(port)) {			//		Port generation by dialog
			port = NewPort;									//			New port number setting
			parity = _ComGetParity(port);
			if		(parity == 0) parity = 'N';
			else if (parity == 1) parity = 'O';
			else				  parity = 'E';
			_PrintF("Rate=%d, DataBit=%d, Parity=%c, StopBit=%d\n",
						_ComGetRate(port), _ComGetDataBits(port), parity, _ComGetStopBits(port));
		}
	}
	else if (BtnNo == 1) {									//	1: Port open
		OpenPort();											//		Open the port
	}
	else if (BtnNo == 2) {									//	2: End button
		if (_MsgBox("Do you want to end it?", $_MyName, _MB_YESNO) == _IDYES) {
			_Restart();
		}
	}
	return 0;
}
