********************************************************************
*  PROCEDURE FILE NA MSR206
********************************************************************

**************************************
* MSR206_INI
**************************************
PROCEDURE msr206_ini
************************************************
release like cs*
pom = ""
PUBLIC inp_string, out_string,i_str,last_send,tmout_odp
PUBLIC old_date,new_date,maxpokus
inp_string = ''
out_string = ''
i_str = ''

m.last_send = secon()           && Cas naposledy odeslane zpravy
m.tmout_odp  = 0.5              && Pocet sekund timeout cekani na odpoved
m.old_date = DTOS(DATE())
m.new_date = DTOS(DATE())
m.maxpokus = 3					&& Max pocet pokusu vysilani
RETURN

**************************************
* MSR206_END
**************************************
PROCEDURE msr206_end
  parameter p_port  
  = deinit_port(p_port)                        && deinitialize the port
RETURN


**************************************
* MSR206_RST
* Provede reset kodovaciho zarizeni
**************************************
PROCEDURE MSR206_rst
PARAMETERS p_port
PRIVATE m.ret
*
  m.ret = 0
  m.out_string = chr(27)+'a'+chr(27)+'e'
  do send_cmd_rs WITH m.p_port
  m.beg_date = DTOS(DATE())
  m.last_send = secon()
  do get_stat WITH m.p_port
  if ! EMPTY(inp_string)
    inp_string = alltrim(inp_string)
    m.odezva1 = left(inp_string,1)
    m.odezva2 = substr(inp_string,2,1)
    IF (m.odezva1 = CHR(27)) .and. (m.odezva2 ='y')
      m.ret = 0
      m.out_string = chr(27)+'a'
      do send_cmd_rs WITH m.p_port
    ELSE
      *wait jaz( "MSR206: Chybna odezva od editoru") wind time 2
      m.ret = -1
    ENDIF
  ELSE
    *wait jaz( "MSR206: Zadna odezva od editoru") wind time 2
    m.ret = -2 && zadna odezva neprisla
  endif
  m.inp_string = ""
RETURN m.ret

**************************************
*; MSR206_READ
*;  Cteni karty ze zapisovacky
*;   Vystupni parametry
*;  p_track1  ... stopa 1
*;  p_track2  ... stopa 2
*;  p_track3  ... stopa 3
*;   Vraci:
*;  0  =  O.K.
*; <0  =  chyba
**************************************
PROCEDURE MSR206_read
PARAMETER p_port, p_track1,p_track2,p_track3, p_ret
PRIVAT pomstr, a1, a2, a3, a4, a5, a6
  m.p_ret = 0
  m.out_string = chr(27)+"r"
  do send_cmd WITH m.p_port
  m.beg_date = DTOS(DATE())
  m.last_send = secon()
  do get_stat WITH m.p_port
  if ! EMPTY(inp_string)
    inp_string = alltrim(inp_string)
    m.odezva1 = left(inp_string,1)
    m.odezva2 = substr(inp_string,2,1)
    IF (m.odezva1 = CHR(27)) .and. (m.odezva2 ='s')
      do get_answr WITH m.p_port
      IF ! EMPTY(inp_string)
        inp_string = alltrim(inp_string)
        pomstr = inp_string
        m.a0 = AT(chr(27)+chr(1), inp_string)
        IF m.a0 # 0
          m.a1 = AT('%', pomstr)
          IF m.a1 # 0
            m.a2 = AT('?', pomstr)
            m.p_track1 = SUBSTR(pomstr, a1+1, a2-a1-1)
            pomstr = SUBSTR(pomstr, a2+1, 400)
          ELSE
            m.p_track1 = ""
          ENDIF
        ELSE
          m.p_track1 = ""
        ENDIF
        m.a0 = AT(chr(27)+chr(2), inp_string)
        IF m.a0 # 0
          m.a1 = AT(';', pomstr)
          IF m.a1 # 0
            m.a2 = AT('?', pomstr)
            m.p_track2 = SUBSTR(pomstr, a1+1, a2-a1-1)
            pomstr = SUBSTR(pomstr, a2+1, 400)
          ELSE
            m.p_track2 = ""
          ENDIF
        ELSE
          m.p_track2 = ""
        ENDIF
        m.a0 = AT(chr(27)+chr(3), inp_string)
        IF m.a0 # 0
          m.a1 = AT(';', pomstr)
          IF m.a1 # 0
            m.a2 = AT('?', pomstr)
            m.p_track3 = SUBSTR(pomstr, a1+1, a2-a1-1)
          ELSE
            m.p_track3 = ""
          ENDIF
        ELSE
          m.p_track3 = ""
        ENDIF
        m.p_ret = 0
      ELSE
         *wait jaz( "Zrata dat pri komunikaci ") wind time 3
        m.p_ret = -2
      ENDIF
    ELSE
      m.p_ret = err_parse(m.odezva2)
    ENDIF
  ELSE
    *wait jaz( "MSR206: Zadna odezva od editoru") wind time 2
    m.p_ret = -1 && zadna odezva neprisla
  ENDIF
  m.inp_string = ""
RETURN

**************************************
*; MSR206_RBUF
*;  Cteni karty z ctecky
*;   Vystupni parametry
*;  p_track1  ... stopa 1
*;  p_track2  ... stopa 2
*;  p_track3  ... stopa 3
*;   Vraci:
*;  0  =  O.K.
*; <0  =  chyba
**************************************
PROCEDURE MSR206_rbuf
PARAMETER p_port, p_track1,p_track2,p_track3, p_ret
PRIVAT pomstr, a1, a2, a3, a4, a5, a6
  m.p_ret = 0
  = c_rxflush( p_port, 1024)
  = c_txflush( p_port, 1024)
  do get_reader WITH m.p_port
  IF ! EMPTY(inp_string)
    inp_string = alltrim(inp_string)
    pomstr = inp_string
    m.a1 = AT('%', pomstr)
    IF m.a1 # 0
      m.a2 = AT('?', pomstr)
      m.p_track1 = SUBSTR(pomstr, a1+1, a2-a1-1)
      m.a3 = AT(';', pomstr)
      IF m.a3 # 0
        m.a4 = AT('?', pomstr, 2)
        m.p_track2 = SUBSTR(pomstr, a3+1, a4-a3-1)        
        m.a5 = AT(';', pomstr, 2)
        IF m.a5 # 0
          m.a6 = AT('?', pomstr, 3)
          m.p_track3 = SUBSTR(pomstr, a5+1, a6-a5-1)
        ELSE
          m.p_track3 = ""
        ENDIF
      ELSE
        m.p_track2 = ""
      ENDIF
    ELSE
      m.p_track1 = ""
      m.a3 = AT(';', pomstr)
      IF m.a3 # 0
        m.a4 = AT('?', pomstr)
        m.p_track3 = SUBSTR(pomstr, a3+1, a4-a3-1)
        m.a5 = AT(';', pomstr, 2)
        IF m.a5 # 0
          m.a6 = AT('?', pomstr, 2)
          m.p_track2 = SUBSTR(pomstr, a5+1, a6-a5-1)
        ELSE
          m.p_track2 = ""
        ENDIF
      ELSE
        m.p_track2 = ""
        m.p_track3 = ""
      ENDIF
    ENDIF
    m.p_ret = 0
  ELSE
    *wait jaz( "Zrata dat pri komunikaci ") wind time 3
    m.p_ret = -2
  ENDIF
  m.inp_string = ""
RETURN

**************************************
*; MSR206_RBUF1
*; Karta MUZO
*;  Cteni karty z ctecky
*;   Vystupni parametry
*;  p_track1  ... stopa 1
*;  p_track2  ... stopa 2
*;  p_track3  ... stopa 3
*;   Vraci:
*;  0  =  O.K.
*; <0  =  chyba
**************************************
PROCEDURE MSR206_rbuf1
PARAMETER p_port, p_track1,p_track2,p_track3, p_ret
PRIVAT pomstr, a1, a2, a3, a4, a5, a6
  m.p_ret = 0
  = c_rxflush( p_port, 1024)
  = c_txflush( p_port, 1024)
  do get_reader WITH m.p_port
  IF ! EMPTY(inp_string)
    inp_string = alltrim(inp_string)
    m.p_track3 = inp_string
    m.p_ret = 0
  ELSE
    *wait jaz( "Zrata dat pri komunikaci ") wind time 3
    m.p_ret = -2
  ENDIF
  m.inp_string = ""
RETURN



**************************************
*; MSR206_WRITE
*;  Zapis na kartu
*;   Vtupni parametry
*;  p_track1  ... stopa 1 (max. 79 alfanum)
*;  p_track2  ... stopa 2 (max. 40 numerickych znaku)
*;  p_track3  ... stopa 3 (max. 107 numerickych znaku)
*;   Vraci:
*;  0  =  O.K.
*; <0  =  chyba
**************************************
PROCEDURE MSR206_write
PARAMETER p_port, p_track1,p_track2,p_track3
PRIVAT ret, odezva1, odezva2
  m.ret = 0
  for i=1 to len(p_track2)
    IF !INLIST(SUBSTR(p_track2,i,1),'0','1','2','3','4','5','6','7','8','9','0','=',' ')
      m.ret = -5
      exit
    ENDIF
  endfor
  IF m.ret = 0
    for i=1 to len(p_track3)
      IF !INLIST(SUBSTR(p_track3,i,1),'0','1','2','3','4','5','6','7','8','9','0','=',' ')
        m.ret = -5
        exit
      ENDIF
    ENDFOR
  ENDIF
  IF m.ret = 0
    m.out_string = chr(27)+"w"+chr(27)+'s'
    IF !EMPTY(p_track1)
      m.out_string = m.out_string + chr(27)+chr(1)+ p_track1
    ENDIF 
    IF !EMPTY(p_track2)
      m.out_string = m.out_string + chr(27)+chr(2)+ p_track2
    ENDIF
    IF !EMPTY(p_track3)
      m.out_string = m.out_string + chr(27)+chr(3)+ p_track3
    ENDIF
    susp
    m.out_string = m.out_string + '?' + chr(28)
    do send_cmd WITH m.p_port
    m.beg_date = DTOS(DATE())
    m.last_send = secon()
    m.tmout_odp = 20
    do get_stat WITH m.p_port
    m.tmout_odp = 0.5
    IF ! EMPTY(inp_string)
      inp_string = alltrim(inp_string)
      m.odezva1 = left(inp_string,1)
      m.odezva2 = substr(inp_string,2,1)
      IF (m.odezva1 = CHR(27)) .and. (m.odezva2 = chr(48))
        m.ret = 0
      ELSE
        *m.ret = err_parse(m.odezva2)
        m.ret = VAL(m.odezva2)
      ENDIF
    ELSE
      *wait jaz( "MSR206: Zadna odezva od editoru") wind time 2
      m.ret = -1 && zadna odezva neprisla
    ENDIF
    m.inp_string = ""
  ENDIF
RETURN m.ret

*;****************************************************
*;
*;   PROCEDURE MSR206_LOF
*;     Zhasne vsechny LEDky
*;
*;****************************************************
PROCEDURE MSR206_LOF
PARAMETERS p_port
  m.out_string = chr(27)+chr(129)
  do send_cmd WITH m.p_port
RETURN

*;****************************************************
*;
*;   PROCEDURE MSR206_LON
*;     Rozsviti vsechny LEDky
*;
*;****************************************************
PROCEDURE MSR206_LON
PARAMETERS p_port
  m.out_string = chr(27)+chr(130)
  do send_cmd WITH m.p_port
RETURN

*;****************************************************
*;
*;   PROCEDURE MSR206_GON
*;     Rozsviti zelenou LEDku
*;
*;****************************************************
PROCEDURE MSR206_GON
PARAMETERS p_port
  m.out_string = chr(27)+chr(131)
  do send_cmd WITH m.p_port
RETURN

*;****************************************************
*;
*;   PROCEDURE MSR206_YON
*;     Rozsviti zlutou LEDku
*;
*;****************************************************
PROCEDURE MSR206_YON
PARAMETERS p_port
  m.out_string = chr(27)+chr(132)
  do send_cmd WITH m.p_port
RETURN

*;****************************************************
*;
*;   PROCEDURE MSR206_RON
*;     Rozsviti cervenou LEDku
*;
*;****************************************************
PROCEDURE MSR206_RON
PARAMETERS p_port
  m.out_string = chr(27)+chr(133)
  do send_cmd WITH m.p_port
RETURN

**************************************
* SEND_CMD
**************************************
PROCEDURE send_cmd
PARAMETERS p_port
PRIVATE c_vys
  ***********  Posilani zpravy   **********************
  do while c_getc(p_port) # -1
  enddo
  = c_rxflush( p_port, 1024)
  = c_txflush( p_port, 1024)
  for i=1 to len(m.out_string)
    =c_putc(m.p_port, substr(m.out_string,m.i,1))
  endfor
  *? SPACE(2) + 'Sending "'+ m.out_string + '"'
RETURN

**************************************
* SEND_CMD_RS
**************************************
PROCEDURE send_cmd_rs
PARAMETERS p_port
PRIVATE c_vys
  ***********  Posilani zpravy   **********************
  do while c_getc(p_port) # -1
  enddo
  = c_rxflush( p_port, 1024)
  = c_txflush( p_port, 1024)
  for i=1 to len(m.out_string)
    =c_putc(m.p_port, substr(m.out_string,m.i,1))
    wait "" TIME 0.01
  endfor
  *? SPACE(2) + 'Sending "'+ m.out_string + '"'
RETURN


**************************************
* GET_ANSWR precti odpoved
**************************************
procedure get_answr
PARAMETERS p_port
  ***********  Cekani na odpoved od MSR206  **************
  m.beg_date = DTOS(DATE())
  m.timein = secon()
  do while .t.
    c_in = c_getc( p_port)
    m.end_date = DTOS(DATE())
    IF m.beg_date # m.end_date
      m.beg_date = m.end_date
      m.timein = secon()
    ENDIF
    do case
    case c_in = 28 && FS
      inp_string = i_str + chr(c_in)
      i_str = ''
      do while c_getc(p_port) # -1     && Zahod zbyle znaky
      enddo
      exit
    case c_in = -1
      if secon() < timein + m.tmout_odp
        loop
      else
        exit
      endif
    otherwise
      i_str = i_str + chr(c_in)
    endcase
  enddo
return

**************************************
* GET_READER precti odpoved ze ctecky
**************************************
procedure get_reader
PARAMETERS p_port
  ***********  Cekani na odpoved od MSR206  **************
  m.beg_date = DTOS(DATE())
  m.timein = secon()
  do while .t.
    c_in = c_getc( p_port)
    m.end_date = DTOS(DATE())
    IF m.beg_date # m.end_date
      m.beg_date = m.end_date
      m.timein = secon()
    ENDIF
    do case
    case c_in = 13 && m.CR
      inp_string = i_str + chr(c_in)
      i_str = ''
      do while c_getc(p_port) # -1     && Zahod zbyle znaky
      enddo
      exit
    case c_in = -1
      if secon() < timein + m.tmout_odp
        loop
      else
        exit
      endif
    otherwise
      i_str = i_str + chr(c_in)
    endcase
  enddo
return

**************************************
* GET_STAT  precti status
**************************************
procedure get_STAT
PARAMETERS p_port
  ***********  Cekani na odpoved od MSR206  **************
  m.beg_date = DTOS(DATE())
  m.timein = secon()
  do while .t.
    c_in = c_getc( p_port)
    m.end_date = DTOS(DATE())
    IF m.beg_date # m.end_date
      m.beg_date = m.end_date
      m.timein = secon()
    ENDIF
    do case
    case c_in = 27
      i_str = chr(c_in)
      c_in = c_getc( p_port)
      *wait jaz( 'Odpoved  STATUS ') + ' '+ chr(c_in)  window nowait
      inp_string = i_str + chr(c_in)
      i_str = ''
      *do while c_getc(p_port) # -1     && Zahod zbyle znaky
      *enddo
      exit
    case c_in = -1
      if secon() < timein + m.tmout_odp
        loop
      else
        exit
      endif
    endcase
  enddo
return


PROCEDURE err_parse
PARAMETER p_odezva
private ret
 m.ret = -4
return ret

**************************************
*; MSR206_RCEK
*;  Cteni karty z ctecky s nekonecnym cekanim nebo prerusenim klavesou
*;   Vystupni parametry
*;  p_track1  ... stopa 1
*;  p_track2  ... stopa 2
*;  p_track3  ... stopa 3
*;   Vraci:
*;  0  =  O.K.
*; <0  =  chyba
*; >0  = klavesa
**************************************
PROCEDURE MSR206_rcek
PARAMETER p_port, p_track1,p_track2,p_track3, p_ret
PRIVAT pomstr, a1, a2, a3, a4, a5, a6
  m.p_ret = 0
  = c_rxflush( p_port, 1024)
  = c_txflush( p_port, 1024)
  do get_reader1
  IF ! EMPTY(inp_string)
    inp_string = alltrim(inp_string)
    pomstr = inp_string
    m.a1 = AT('%', pomstr)
    IF m.a1 # 0
      m.a2 = AT('?', pomstr)
      m.p_track1 = SUBSTR(pomstr, a1+1, a2-a1-1)
      m.a3 = AT(';', pomstr)
      IF m.a3 # 0
        m.a4 = AT('?', pomstr, 2)
        m.p_track2 = SUBSTR(pomstr, a3+1, a4-a3-1)        
        m.a5 = AT(';', pomstr, 2)
        IF m.a5 # 0
          m.a6 = AT('?', pomstr, 3)
          m.p_track3 = SUBSTR(pomstr, a5+1, a6-a5-1)
        ELSE
          m.p_track3 = ""
        ENDIF
      ELSE
        m.p_track2 = ""
      ENDIF
    ELSE
      m.p_track1 = ""
      m.a3 = AT(';', pomstr)
      IF m.a3 # 0
        m.a4 = AT('?', pomstr)
        m.p_track3 = SUBSTR(pomstr, a3+1, a4-a3-1)
        m.a5 = AT(';', pomstr, 2)
        IF m.a5 # 0
          m.a6 = AT('?', pomstr, 2)
          m.p_track2 = SUBSTR(pomstr, a5+1, a6-a5-1)
        ELSE
          m.p_track2 = ""
        ENDIF
      ELSE
        m.p_track2 = ""
        m.p_track3 = ""
      ENDIF
    ENDIF
    m.p_ret = 0
  ELSE
    m.p_ret = 2
  ENDIF
  m.inp_string = ""
RETURN

**************************************
*; MSR206_RCEK2
*;  Cteni karty z ctecky s nekonecnym cekanim nebo prerusenim klavesou ctecka MUZO
*;   Vystupni parametry
*;  p_track1  ... stopa 1
*;  p_track2  ... stopa 2
*;  p_track3  ... stopa 3
*;   Vraci:
*;  0  =  O.K.
*; <0  =  chyba
*; >0  = klavesa
**************************************
PROCEDURE MSR206_rcek2
PARAMETER p_port, p_track1,p_track2,p_track3, p_ret
PRIVAT pomstr, a1, a2, a3, a4, a5, a6
  m.p_ret = 0
  = c_rxflush( p_port, 1024)
  = c_txflush( p_port, 1024)
  do get_reader1
  IF ! EMPTY(inp_string)
    m.p_track3 = m.inp_string
  ELSE
    m.p_ret = 2
  ENDIF
  m.inp_string = ""
RETURN



**************************************
* GET_READER1 precti odpoved ze ctecky
* modifikace cteni s nekonecnym cekanim, preruseni klavesou ctecka MUZO
**************************************
procedure get_reader1
PARAMETERS p_port
  ***********  Cekani na odpoved od MSR206  **************
  m.beg_date = DTOS(DATE())
  m.timein = secon()
  do while .t.
    c_in = c_getc( p_port)
    m.end_date = DTOS(DATE())
    IF m.beg_date # m.end_date
      m.beg_date = m.end_date
      m.timein = secon()
    ENDIF
    do case
    case c_in = 13 &&m.CR
      inp_string = i_str + chr(c_in)
      i_str = ''
      do while c_getc(p_port) # -1     && Zahod zbyle znaky
      enddo
      exit
    case c_in = -1      
      if empty(inkey())
        loop
      else
        m.inp_string = ""
        i_str = ''
        exit
      endif
    otherwise
      i_str = i_str + chr(c_in)
    endcase
  enddo
return


**************************************
*; MSR206_RCEK3
*;  Cteni karty z ctecky s nekonecnym cekanim nebo prerusenim klavesou ctecka MUZO
*;   Vystupni parametry
*;  p_track1  ... stopa 1
*;  p_track2  ... stopa 2
*;  p_track3  ... stopa 3
*;   Vraci:
*;  0  =  O.K.
*; <0  =  chyba
*; >0  = klavesa
**************************************
PROCEDURE MSR206_rcek3
PARAMETER p_track1,p_track2,p_track3, p_ret
PRIVAT pomstr, a1, a2, a3, a4, a5, a6
  m.p_ret = 0
  = c_rxflush( p_port, 1024)
  = c_txflush( p_port, 1024)
  do get_reader2
  IF ! EMPTY(inp_string)
    m.p_track3 = m.inp_string
  ELSE
    m.p_ret = 2
  ENDIF
  m.inp_string = ""
RETURN



**************************************
* GET_READER2 precti odpoved ze ctecky
* modifikace cteni s nekonecnym cekanim, preruseni klavesou ctecka AlIMEX
**************************************
procedure get_reader2
PARAMETERS p_port
  ***********  Cekani na odpoved od MSR206  **************
  m.beg_date = DTOS(DATE())
  m.timein = secon()
  m.len_zp = 0
  m.cit_zp = 0
  do while .t.
    c_in = c_getc( p_port)
    m.end_date = DTOS(DATE())
    IF m.beg_date # m.end_date
      m.beg_date = m.end_date
      m.timein = secon()
    ENDIF
    do case
    case c_in = 1	&& SOH
      m.len_zp = 0
      m.cit_zp = 0
      inp_string = ""
      i_str = ''
    case c_in = -1      
      if empty(inkey())
        loop
      else
        m.inp_string = ""
        i_str = ''
        exit
      endif
    otherwise
      m.cit_zp = m.cit_zp + 1
      if m.cit_zp = 1
        m.len_zp = asc(chr(c_in))
      else
        if m.cit_zp > 2
          i_str = i_str + chr(c_in)
          do case
          case m.len_zp = m.cit_zp-2 
            inp_string = i_str
            i_str = ""
            exit
          endcase
        endif
      endif 
    endcase
  enddo
return
