
' -----------------------------------------------
'  LD2412 SENSOR - SIMPLE READER
' -----------------------------------------------
OpenComPort
Loop
End

' -----------------------------------------------
'  OPEN COM
' -----------------------------------------------
Label OpenComPort
	Numeric Bauds    = 256000
	String  ComPort
	Numeric ComIndex
	Numeric ComRetry1
	Numeric ComRetry2
	For ComRetry1 = 1 To 10
		For ComIndex = 0 To GetSeparatedStringCount(COM_PortNames) - 1
			ComPort = GetSeparatedString(COM_PortNames, ComIndex)
			COM Open ComPort Bauds
			If COM_Status = "OPENED"
				For ComRetry2 = 1 To 3
					COM DiscardOutBuffer
					COM DiscardInBuffer
					Wait Seconds 0.01
					If Len(COM_ReceiveUntil(Terminator)) = 21
						ComIndex = -1
						Beep "440-1,0-80,880-1"
						Return
					EndIf
				Next
			EndIf
		Next
	Next
	If ComIndex >= 0
		Print "COM PORT NOT FOUND"
		TTS SelectVoice "ENGF"
		TTS Speak "Com port not found."
		Wait Not TTSspeaking
		End
	EndIf
Return

Label Loop
	ReceivedLine = COM_ReceiveUntil(Terminator)
	If ReceivedLine <> ""
		ReceivedLineToHexLine
		Print HexLine
		
		'If Len(ReceivedLine) = 21
		'	DecodeReceivedLine
		'EndIf
	EndIf
Goto Loop


' Data received (basic data)
' F4 F3 F2 F1 0B 00 02 AA 02 00 00 00 96 00 64 55 00 F8 F7 F6 F5

' F4 F3 F2 F1  						Header
' 0B 00								Payload length (11 or 42 byte).
' 02 AA 02 00 00 00 97 00 64 55 00 	Payload
' F8 F7 F6 F5						Terminator

' 02 AA 02 00 00 00 97 00 64 55 00 	----------------- Payload (basic data)
'  |  -  -  -  -  -  -  -  -  -  -  7  (6)  (0)  01=Engineering 02=BasicData
'     |  -  -  -  -  -  -  -  -	 -  8  (7)  (1)  HEAD
'        |  -  -  -  -  -  -  -  -  9  (8)  (2)  Status
'           |  -  -  -  -  -  -  -  10 (9)  (3)  MovingDistance_LSB
'              |  -  -  -  -  -  -  11 (10) (4)  MovingDistance_MSB
'                 |  -  -  -  -  -  12 (11) (5)  MovingSignal
'                    |  -  -  -  -  13 (12) (6)  StationaryDistance_LSB
'                       |  -  -  -  14 (13) (7)  StationaryDistance_MSB
'                          |  -  -  15 (14) (8)  StationarySignal
'                             |  -  16 (15) (9)  END
'                                |  17 (16) (10) CONTROL

' Status 0 = Nothing detected
' Status 1 = Moving target detected
' Status 2 = Stationary target detected
' Status 3 = Moving and stationary target detected

String Terminator = Chr(248) + Chr(247) + Chr(246) + Chr(245)
String ReceivedLine
Numeric Status
Numeric MovingSignal
Numeric MovingDistance
Numeric StationarySignal
Numeric StationaryDistance
String Text1
String Text2
String Text3

Label DecodeReceivedLine
	Status = Asc(Mid(ReceivedLine, 9))
	' ------------------------------------------------ Status
	Text1 = "STATUS " + Status
	' ------------------------------------------------ Stationary
	'If Status = 2 Or Status = 3
	StationarySignal = Asc(Mid(ReceivedLine, 15))
	StationaryDistance = Asc(Mid(ReceivedLine, 13))
	StationaryDistance = StationaryDistance + 256 * Asc(Mid(ReceivedLine, 14))
	Text2 = "   STATIONARY " + StationarySignal + "@" + StationaryDistance + "cm"
    ' ------------------------------------------------ Moving
	MovingSignal = Asc(Mid(ReceivedLine, 12))
	If Status = 3
		MovingDistance = Asc(Mid(ReceivedLine, 10))
		MovingDistance = MovingDistance + 256 * Asc(Mid(ReceivedLine, 11))
	EndIf
	Text3 = "    MOVING " + MovingSignal + "@" + MovingDistance + "cm"
    ' ------------------------------------------------ Print
	Print HexLine + Text1 + Text2 + Text3
Return

Label ReceivedLineToHexLine
	String HexLine
	HexLine = ""
	For v1 = 1 To Len(ReceivedLine)
		HexLine = HexLine + Right("0" + Hex(Asc(Mid(ReceivedLine, v1))), 2) + " "
	Next
	HexLine = HexLine + CRLF
Return


'BOOKMARKS 

'EDITEDLINES 103 1 110 2 57 3 12 4 50 9 47 9 48 10 

'WATCH dummy
