&ACCESS RVP
&REL 237
&PARAM TEMPLATE = C:\KRC\Roboter\Template\ExpertVorgabe
&PARAM EDITMASK = *
DEF serial( )
DECL BOOL Pass
DECL INT CR, Comma1, Comma2, Comma3
DECL CHAR Comma, MiscString[50]

Pass = strclear(Result[])        ; Init receiving string
Pass = strclear (Send_Command[]) ; Init Transmission string
Pass = StrClear (MiscString[])

CR=13       ; INT ASCII value of "Carriage Return"
Comma = 44  ; INT ASCII value of a comma ","

offset=0 ; SWRITE will work from first position of string
SWRITE(Send_Command[], CH_State, Offset, "%s", VisionRequest[]) ; write command string to tx string
IF ch_State.RET1 <> #CMD_OK THEN ; check success of SWRITE
  halt  ; halt if error
ENDIF

Offset = (( StrLen (Send_Command[])) +1 ) ; find end of string
Send_command[Offset]=CR ; add [CR] to end of string

halt

COPEN (:SER_3, Handle) ; open serial port COM3

CWRITE (Handle, TX_State, Mode, "%s", Send_Command[]) ; send TX string

Mode = #ABS ; CREAD waits for data or timeout

WAIT FOR ($DATA_SER3 <> 0) ; wait for data in COM3 buffer
;wait sec 5

offset=0 ; start from string first character

CREAD (Handle, RX_State, Mode, Timeout, Offset, "%s", Result[]) ; read serial buffer into RX string

CCLOSE (Handle, CH_State) ; close serial channel

halt

Offset = 0 ; Start SREAD from first string character
SREAD (Result[], CH_State, Offset, "%2s,%10f,%10f", MiscString[], X_Result, Y_Result) ; extract X and Y values from string, MiscString serves to remove "T1,"

halt

; ================footnotes====================
; SREAD,SWRITE,CREAD,CWRITE, first string position uses Offset=0
; STRLEN, STRFIND, others, first string position is OFFSET=1, follow the same rule as an array[] variable
; Adding CR to string with SWRITE using %c doesn't work, need to research
; X and Y are both 10chars long, but need different %f sizes in SREAD to work out properly.  Need to research.

END