//	M071 : Send and receive by XMODEM
option unsigned;

define	port	  = 0;

//------------------------------------------------------------------------------//
//	Send and receive by XMODEM													//
//																				//
//	Return	: None																//
//------------------------------------------------------------------------------//
function main()
{
	define	i, x, y;
	define	$menu[] = {"Send a file with XMODEM", "Receive files with XMODEM", "End"};

	_BtnOpen(0, "bnl|[g̐ݒ");		//	{^\

	//	Operation guide display reception
	_Cls();
	_Print("\n\n");
	_Print("Right click and select a process from the popup menu\n");
	_Print("You can change the port setting with the COM port setting button.\n");
	_Print("When a valid COM port is opened, port information is displayed in the upper right.\n");

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

	port  = _ComCreate();					//	Create COM port
	OpenComPort();							//	COM port open

	_Stop();								//	Wait until you select 'End' on the menu

}
//------------------------------------------------------------------------------//
//	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)
{
	define	$menu[] = {"File transmission with XMODEM", "Receive files with XMODEM", "End"};
	define	id, bytes;
	define	$file;

	_BtnEnable(0, _FALSE);							//	Button operation prohibited

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

	if		(id == 0) {								//	- Send file?
		$file = $_GetOpenFile("Please select the file to send", "All(*.*)/*.*/Text(*.txt)/*.txt", "txt");
		if ($file != "") {
			_ComSendXModem1K(port, $file);			//		File transmission by XMODEM
		}
	}
	else if (id == 1) {								//	- Receive file?
		$file = $_GetSaveFile("Please select a file to store received data", "All(*.*)/*.*/Text(*.txt)/*.txt", "txt");
		if ($file != "") {
			_ComRecvXModem1K(port, $file);			//		Receive file by XMODEM
		}
	}
	else if (id == 2) {								//	- End?
		_ComClose (port);							//		Port close
		_ComDelete(port);							//		Port delete
		_Exit(""End" was selected in the pop-up menu");
	}

	_BtnEnable(0, _TRUE);							//	Button operation permission

}
//------------------------------------------------------------------------------//
//	Processing when button is pressed											//
//																				//
//	Argument:	BtnNo - Button ID		 										//
//																				//
//	Return	: None																//
//------------------------------------------------------------------------------//
function _OnButton(BtnNo)
{
	define NewPort;

	if (BtnNo == 0) {
		if (NewPort = _ComCreateByDialog(port)) {		//	COM port generation by dialog
			port = NewPort;								//		New port number setting
			OpenComPort();								//		Port open
		}
	}
}
//------------------------------------------------------------------------------//
//	COM port open																//
//																				//
//	Argument:	pno - Port number		 										//
//																				//
//	Return	: None																//
//------------------------------------------------------------------------------//
function OpenComPort()
{
	define	$text;

	if (_ComIsOpenPossible(port)) {							//	Openable?
		_ComOpen(port);										//		Oprt open
		_ShowText($_StrCat(" ", $_ComGetParam(port), " "));	//		COM port information display
	}
	else {													//	Can not open?
		_ShowText("");										//		COM port information hidden
	}
}
