' =====================================================================
'     Name: PLC_Emulator_12.txt                       Date: 21 Oct 2020
'    Autor: Leonardo DE PALO Ph.D.
'    Scope: Emulating PLC sending serial data for DigiScrew testing.
'    Plate: 1530 TXB.
'   Lenght: 9 - 12 - 13 - 14 - 15 - 16 - 17 - 18 - 19 - 22 - 26
' Hardware: USB to Serial RS232 adapter or COMx RS232 connection
'     Note: To be used with Relase 5.5.5 of DigiScrew
' =====================================================================
Load Toc.wav
Option Speed 9

Controls OpenTextBox 

v1 = 12 '4  ' <--- Write here the COM-Port number
COM Open v1 9600
s1 = COM_Status
s1 = "Port status: " + s1 + " COM" + v1 + CRLF
s1 = s1 + "Valid ports: " + COM_PortNames + CRLF + CRLF
Print s1

Button  1 Text 18 FOREVER
Button  2 Text Sequence		' 9
Button  3 Text 12
Button  4 Text 13
Button  5 Text 14
Button  6 Text 15
Button  7 Text 16
Button  8 Text 17
Button  9 Text 18
Button 10 Text 19
Button 11 Text 22
Button 12 Text 26

Button 14 Text START
Button 15 Text STOP
Button 16 Text CLOSE ALL

Button 1 Color Yellow
Button 2 Color Yellow
Button 3 Color YellowGreen
Button 4 Color YellowGreen
Button 5 Color YellowGreen
Button 6 Color YellowGreen
Button 7 Color YellowGreen
Button 8 Color YellowGreen
Button 9 Color LawnGreen
Button 10 Color YellowGreen
Button 11 Color YellowGreen
Button 12 Color YellowGreen

Button 14 Color Green
Button 15 Color Red
Button 16 Color Black

'Load Theremino_HAL\Theremino_HAL.exe
'Load ..\Theremino_Terminal_RX\Theremino_Terminal.exe
'Load ..\Theremino_Terminal_TX\Theremino_Terminal.exe
'Load ..\Theremino_HAL\Theremino_SlotViewer.exe
'Load ..\Theremino_HAL\Theremino_SignalScope.exe

' ------------------------------------- Remember last totals
Load Vars

'Controls OpenTextBox

Stop                                            '

Label CLOSE ALL
	Load CloseApps
Return

Label EventStop
	Save Vars
End                                             '


Label 18 FOREVER
	s2 = ""
	For v1 = 1 To 9e99
		s1 = "18M4CW" + CRLF
		COM WriteString s1
		Gosub WaitResponse
	Next
Return

Label WaitResponse
	For v2 = 1 To 1e99
		s2 = s2 + COM_Received
		Select s2
			Case "AK"
				s2 = ""
			Case "OK"
				s2 = ""
				v7 = v7 + 1
				Exit
			Case "NO"
				s2 = ""
				Load "sirena.wav"
				Wait Seconds 2
				v8 = v8 + 1
				v7 = v7 + 1
				Exit
		EndSelect
	Next
	Print " Test num: " + v7 + " Errors: " + v8 + CRLF
	Wait Seconds 0.1
Return

Label 9
	s1 = "9M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 12
	s1 = "12M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 13
	s1 = "13M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 14
	s1 = "14M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 15
	s1 = "15M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 16
	s1 = "16M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 17
	s1 = "17M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 18
	s1 = "18M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 19
	s1 = "19M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 22
	s1 = "22M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label 26
	s1 = "26M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
Return

Label START
	Slot(14) = 1000		'show start signal
	s1 = "START" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
	Slot(14) = 0		'show stop signal
Return

Label 20M4CW
	Slot(14) = 1000		'show start signal
	s1 = "20M4CW" + CRLF
	Wait Seconds 0.02
	COM WriteString s1
	Print s1
	Slot(14) = 0		'show stop signal
Return

Label 2M4CCW			'show start signal
	Slot(14) = 1000
	s1 = "2M4CCW" + CRLF
	'Wait Seconds 0.02
	COM WriteString s1
	Print s1
	Slot(14) = 0		'show stop signal
Return

Label STOP		     	'show start signal
	Slot(14) = 1000 + CRLF
	'Wait Seconds 0.02
	s1 = "STOP" + CRLF
	COM WriteString s1
	Print s1
	Slot(14) = 0		'show stop signal
Return


' ====================================================================
'  SEQUENCE START
' ====================================================================
Label Sequence
	SequenceFileName = "SEQUENCES\AllScrews.seq"
	Gosub ExecSequence
Return

' ====================================================================
'  SEQUENCE LOADING AND EXECUTION
' ====================================================================
Variable String SequenceFileName
Variable String Sequence
Variable String SequenceCmd
Variable Numeric SequenceIndex
Variable Numeric SequenceLength

Label ExecSequence
	Load Sequence SequenceFileName
	If Len(Sequence) = 0
		Controls OpenTextBox
		Print CRLF + " Sequence file not found: " + SequenceFileName
		Beep
		Stop
	EndIf
	' ------------------------------------------
	Variable Numeric ExecSequenceStartTime
	ExecSequenceStartTime = ElapsedTime
	' ------------------------------------------
	SequenceLength = GetSeparatedStringCount(Sequence, CRLF)
	For SequenceIndex = 0 To SequenceLength-1
		SequenceCmd = GetSeparatedString(Sequence, SequenceIndex, CRLF)
		SequenceCmd = Replace(SequenceCmd, Chr(9), " ") 'TABS to SPACES
		SequenceCmd = RemoveComments(SequenceCmd)
		SequenceCmd = Trim(SequenceCmd)
		'
		If SequenceCmd <> ""
			Print " Executing command: " + SequenceCmd + CRLF
		EndIf
		'
		Select UCase(SequenceCmd)
            ' ---------------------------------------------- End sequence
			Case "ENDSEQ"
				Exit
		    ' ---------------------------------------------- ACTIONS
			Case "CLOSE ALL"
				Gosub CLOSE ALL
			' ---------------------------------------------- LED COLORS
			'Case "REDON"
			'	Gosub RedOn
			'Case "GREENON"
			'	Gosub GreenOn
			'Case "BLUON"
			'	Gosub BluON
			'Case "YELLOWON"
			'	Gosub YellowOn
            ' ---------------------------------------------- With parameters
			CaseElse
				s1 = GetSeparatedString(SequenceCmd, 0)
				Select UCase(s1)
					Case "SCREW"
						s1 = GetSeparatedString(SequenceCmd, 1) + CRLF
						COM WriteString s1
						Gosub WaitResponse
					CaseElse
						If s1 <> ""
							Controls OpenTextBox
							Print CRLF + " Unrecognized command: " + SequenceCmd + CRLF
							Beep
							Stop
						EndIf
				EndSelect
		EndSelect
	Next
	SequenceFileName = Replace(SequenceFileName,".seq","")
	Print SequenceFileName + " executed in " + Format(ElapsedTime - ExecSequenceStartTime, "0.00") + " seconds"
Return


'WATCH COM_Status
