'╔════════════════════════════════════════════════════════════╗
'║     Name: ZeroErr_Test_V01.txt           Date: 23 Apr 2022 ║
'║   Author: Leonardo                                         ║
'║    Scope: Test basic functionality of eR070 MODBUS         ║
'╠════════════════════════════════════════════════════════════╣
'║Hardware: eRob0070                                          ║
'╟────────────────────────────────────────────────────────────╢
'║ Note: First run                                 23/04/2022 ║
'╚════════════════════════════════════════════════════════════╝

Option Speed 9
'Load images\ZeroErr.jpg
'Beep "2500 200, 1500 200"

COM Open 14 115200

' ╔════════════════════════════════════════════╗ █   █ █▀▀█ █▀▀▀█
' ║ VARIABLE INITIALIZATIONS & ASSIGNEMENTS    ║ █   █ █▄▄█ █▄▄▄█
' ╚════════════════════════════════════════════╝  ▀▄▀  █  █ █  ▀▄
'Variable Numeric NumCycle
'Variable Numeric StartTime
'Variable String  Result
'Variable Numeric Slot_Emergency
'
'Variable Numeric Slot_ClocheX			=   1
'Variable Numeric Slot_ClocheY			=   2
''Variable Numeric Slot_SwitchAB			 =   3
'Variable Numeric Slot_EncoderData		=   4
''Variable Numeric Slot_Buzzed			 =   5
'
'

' ╔════════════════════════════════════════════╗ █▀▀▄ █  █ ▀▀█▀▀
' ║ BUTTONS                                    ║ █▀▀█ █  █   █
' ╚════════════════════════════════════════════╝ █▄▄█ █▄▄█   █
Button 1 Text ENABLE
Button ENABLE Text "ENABLE"
Button ENABLE Color Aquamarine

Button 2 Text DISABLE
Button DISABLE Text "DISABLE"
Button DISABLE Color Aquamarine

Button 3 Text StartRunning
Button StartRunning Text "Start Running"
Button StartRunning Color LightGreen

Button 4 Text StopRunning
Button StopRunning Text "Stop Running"
Button StopRunning Color DarkSalmon

Button 5 Text StartPos
Button StartPos Text "Pos=131072"
Button StartPos Color OldLace

Button 6 Text StopPos
Button StopPos Text "Pos=262144"
Button StopPos Color OldLace

Button 7 Text Position
Button Position Text "POSITION"
Button Position Color Blue



'01 03 00 66 00 02 24 14       'ask position

'01 03 04 00 03 FF FF 0B 83    'current position


' ╔════════════════════════════════════════════╗  █▀▀▀ ▀▀█▀▀ █▀▀▀█ █▀▀▀█ ▀▀█▀▀
' ║  START FROM HERE                           ║  ▀▀▀█   █   █▀▀▀█ █▀█▀▀   █
' ╚════════════════════════════════════════════╝  ▄▄▄█   █   █   █ █  ▀▄   █

Controls OpenTextBox
Controls ClearTextBox

s1 = COM_Status
Print s1 + CRLF
Load Hide
Loop
Stop



' ╔════════════════════════════════════════════╗ █   █▀▀█ █▀▀█ █▀▀█
' ║ LOOP                                       ║ █   █  █ █  █ █▄▄█
' ╚════════════════════════════════════════════╝ █▄▄ █▄▄█ █▄▄█ █
Label Loop
	Position
	' ------------------------------------------
	'CheckMotorTemperature
	' ---------------------------------------------------------
	' 0.02 sec. or more to allow time for keys and other tasks
	Wait Seconds 0.02
	' ---------------------------------------------------------
Goto Loop

' ╔════════════════════════════════════════════╗ █▀▀▀ █  █ █▀█
' ║  S U B R O U T I N E   A R E A             ║ ▀▀▀█ █  █ █▀▀█
' ╚════════════════════════════════════════════╝ ▄▄▄█ █▄▄█ █▄▄█


Label ENABLE
	s1 = "01 06 00 F0 00 01 48 39"
	COM WriteStringHEX s1
	Print "Send = " + s1 + CRLF
Return

Label DISABLE
	s1 = "01 06 00 F0 00 00 89 F9"
	COM WriteStringHEX s1
	Print "Send = " + s1 + CRLF
Return

Label StartRunning
	s1 = "01 06 00 F1 00 01 19 F9 "
	COM WriteStringHEX s1
	Print "Send = " + s1 + CRLF
Return

Label StopRunning
	s1 = "01 06 00 F2 00 00 28 39"
	COM WriteStringHEX s1
	Print "Send = " + s1 + CRLF
Return

Label StartPos
	s1 = "01 10 00 43 00 02 04 00 02 00 00 16 4A"   '131072
	COM WriteStringHEX s1
	Print "Send = " + s1 + CRLF
Return

Label StopPos
	s1 = "01 10 00 43 00 02 04 00 04 00 00 F6 4B"   '262144
	COM WriteStringHEX s1
	Print "Send = " + s1 + CRLF
Return


Label Position
	s1 = "01 03 00 66 00 02 24 14"
	COM WriteStringHEX s1
	Print "Send = " + s1 + CRLF
	Wait Seconds 0.5
	s2 = COM_ReceiveNChars(9)
    's2 = COM_ReceiveUntil(CRLF)
    's2 = COM_Received
	'Wait Seconds 0.5
	s3 = ""
	If s2 <> ""
		For v1 = 1 To Len(s2)
			s3 = s3 + Hex(Asc(Mid(s2, v1, 1))) + " "
		Next
	EndIf
	Controls ClearTextBox
	ConvertToPosition
	Button Position Text "Pos=" + s3 'CurrentPos
	Print "Received = " + CurrentPos + CRLF
Return

Variable Numeric CurrentPos
Label ConvertToPosition
	
	s2 = Chr(1) + Chr(3)+ Chr(4)+ Chr(0) + Chr(3) + Chr(255) + Chr(255) + Chr(222) + Chr(222)
	
	CurrentPos = Val(Mid(s2, 7, 1))
	CurrentPos = CurrentPos + Val(Mid(s2, 6, 1)) * 256
	CurrentPos = CurrentPos + Val(Mid(s2, 5, 1)) * 65536
	CurrentPos = CurrentPos + Val(Mid(s2, 4, 1)) * 65536 * 256
	
	CurrentPos = Val(Mid(s2, 7, 1))
	CurrentPos = CurrentPos + Val(Mid(s2, 6, 1)) * 256
	CurrentPos = CurrentPos + Val(Mid(s2, 5, 1)) * 65536
	CurrentPos = CurrentPos + Val(Mid(s2, 4, 1)) * 65536 * 256
Return





'---------------------------------------------------
'01 03 00 66 00 02 24 14       'ask position
'01 03 04 00 03 FF FF 0B 83    'current position


'WATCH COM_Status
'WATCH len(s2)
'WATCH val(
