//	M062 : Read text file and send to COM port at speed of 100 ms / line
option unsigned;

define	fEnd  = _FALSE;
define	count = 0;
define	finp;
define	port = 3;

//------------------------------------------------------------------------------//
//	Read text file and send to COM port at speed of 100 ms / line				//
//																				//
//	Return	: None																//
//------------------------------------------------------------------------------//
function main()
{
	define	$file, $line;
	define	count;

	_Cls();
	_Print("It reads the text file and sends it to COM", port, " at the rate of 100 ms / line\n");
	_ComCreate(port);									//	Create COM port
	_Print("\nPlease select the file to send\n");
	if (($file = $_GetOpenFile("Please select a file to send", "Text(*.txt)/*.txt/All(*.*)/*.*", "txt")) != 0) {
		_ComOpen(port, 115200, 8, 0, 1);				//	Open COM port
		if (finp = _FOpen($file, 'rt')) {				//	File open, success?
			_Print("Transmission file =", $file, "\n");	//		Transmission file display
			_TimerStart(1, 100);						//		Timer start
			_Stop();									//		Wait for completion
			_FClose(finp);								//		Close file
		}
	}

	_ComClose (port);									//	Close COM port
	_ComDelete(port);									//	Discard COM port
}

//------------------------------------------------------------------------------//
//	Timer event																	//
//																				//
//	Return	: None																//
//------------------------------------------------------------------------------//
function _OnTimer(tid)
{
	define	$line;

	if (tid == 1) {										//	Timer 1?
		if ($line = $_FGetS(finp)) {					//		Read file line?
			_ComSendText(port, $line);					//			Send one line
			_Print("line = ", ++count, "\r");			//			Number of sent lines displayed
		}
		else {											//		EOF?
			_TimerStop(1);								//			Stop timer
			_Restart();									//			End
		}
	}
}
